github.com/kerryoscer/gqlgen@v0.17.29/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(ctx context.Context) 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                  {{if $object.Root}}
    84                      out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) {
    85                          return ec._{{$object.Name}}_{{$field.Name}}(ctx, field)
    86                      })
    87                  {{else}}
    88                      out.Values[i] = ec._{{$object.Name}}_{{$field.Name}}(ctx, field, obj)
    89                  {{end}}
    90  
    91                  {{- if $field.TypeReference.GQL.NonNull }}
    92                      if out.Values[i] == graphql.Null {
    93                          {{- if $object.IsConcurrent }}
    94                              atomic.AddUint32(&invalids, 1)
    95                          {{- else }}
    96                              invalids++
    97                          {{- end }}
    98                      }
    99                  {{- end }}
   100              {{- end }}
   101          {{- end }}
   102          default:
   103              panic("unknown field " + strconv.Quote(field.Name))
   104          }
   105  	}
   106  	out.Dispatch()
   107  	if invalids > 0 { return graphql.Null }
   108  	return out
   109  }
   110  {{- end }}
   111  
   112  {{- end }}