github.com/apipluspower/gqlgen@v0.15.2/codegen/object.gotpl (about) 1 {{- range $object := .Objects }} 2 3 var {{ $object.Name|lcFirst}}Implementors = {{$object.Implementors}} 4 5 {{- if .Stream }} 6 func (ec *executionContext) _{{$object.Name}}(ctx context.Context, sel ast.SelectionSet) func() graphql.Marshaler { 7 fields := graphql.CollectFields(ec.OperationContext, sel, {{$object.Name|lcFirst}}Implementors) 8 ctx = graphql.WithFieldContext(ctx, &graphql.FieldContext{ 9 Object: {{$object.Name|quote}}, 10 }) 11 if len(fields) != 1 { 12 ec.Errorf(ctx, "must subscribe to exactly one stream") 13 return nil 14 } 15 16 switch fields[0].Name { 17 {{- range $field := $object.Fields }} 18 case "{{$field.Name}}": 19 return ec._{{$object.Name}}_{{$field.Name}}(ctx, fields[0]) 20 {{- end }} 21 default: 22 panic("unknown field " + strconv.Quote(fields[0].Name)) 23 } 24 } 25 {{- else }} 26 func (ec *executionContext) _{{$object.Name}}(ctx context.Context, sel ast.SelectionSet{{ if not $object.Root }},obj {{$object.Reference | ref }}{{ end }}) graphql.Marshaler { 27 fields := graphql.CollectFields(ec.OperationContext, sel, {{$object.Name|lcFirst}}Implementors) 28 {{- if $object.Root }} 29 ctx = graphql.WithFieldContext(ctx, &graphql.FieldContext{ 30 Object: {{$object.Name|quote}}, 31 }) 32 {{end}} 33 out := graphql.NewFieldSet(fields) 34 var invalids uint32 35 for i, field := range fields { 36 {{- if $object.Root }} 37 innerCtx := graphql.WithRootFieldContext(ctx, &graphql.RootFieldContext{ 38 Object: field.Name, 39 Field: field, 40 }) 41 {{end}} 42 switch field.Name { 43 case "__typename": 44 out.Values[i] = graphql.MarshalString({{$object.Name|quote}}) 45 {{- range $field := $object.Fields }} 46 case "{{$field.Name}}": 47 {{- if $field.IsConcurrent }} 48 field := field 49 50 innerFunc := func(ctx context.Context) (res graphql.Marshaler) { 51 defer func() { 52 if r := recover(); r != nil { 53 ec.Error(ctx, ec.Recover(ctx, r)) 54 } 55 }() 56 res = ec._{{$object.Name}}_{{$field.Name}}(ctx, field{{if not $object.Root}}, obj{{end}}) 57 {{- if $field.TypeReference.GQL.NonNull }} 58 if res == graphql.Null { 59 {{- if $object.IsConcurrent }} 60 atomic.AddUint32(&invalids, 1) 61 {{- else }} 62 invalids++ 63 {{- end }} 64 } 65 {{- end }} 66 return res 67 } 68 69 {{if $object.Root}} 70 rrm := func(ctx context.Context) graphql.Marshaler { 71 return ec.OperationContext.RootResolverMiddleware(ctx, innerFunc) 72 } 73 {{end}} 74 75 out.Concurrently(i, func() graphql.Marshaler { 76 {{- if $object.Root -}} 77 return rrm(innerCtx) 78 {{- else -}} 79 return innerFunc(ctx) 80 {{end}} 81 }) 82 {{- else }} 83 innerFunc := func(ctx context.Context) (res graphql.Marshaler) { 84 return ec._{{$object.Name}}_{{$field.Name}}(ctx, field{{if not $object.Root}}, obj{{end}}) 85 } 86 {{if $object.Root}} 87 out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, innerFunc) 88 {{else}} 89 out.Values[i] = innerFunc(ctx) 90 {{end}} 91 92 {{- if $field.TypeReference.GQL.NonNull }} 93 if out.Values[i] == graphql.Null { 94 {{- if $object.IsConcurrent }} 95 atomic.AddUint32(&invalids, 1) 96 {{- else }} 97 invalids++ 98 {{- end }} 99 } 100 {{- end }} 101 {{- end }} 102 {{- end }} 103 default: 104 panic("unknown field " + strconv.Quote(field.Name)) 105 } 106 } 107 out.Dispatch() 108 if invalids > 0 { return graphql.Null } 109 return out 110 } 111 {{- end }} 112 113 {{- end }}