...

Text file src/github.com/99designs/gqlgen/codegen/object.gotpl

Documentation: github.com/99designs/gqlgen/codegen

     1{{- range $object := .Objects }}
     2
     3var {{ $object.Name|lcFirst}}Implementors = {{$object.Implementors}}
     4
     5{{- if .Stream }}
     6func (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 }}
    26func (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
    34	out := graphql.NewFieldSet(fields)
    35	deferred := make(map[string]*graphql.FieldSet)
    36	for i, field := range fields {
    37		{{- if $object.Root }}
    38			innerCtx := graphql.WithRootFieldContext(ctx, &graphql.RootFieldContext{
    39				Object: field.Name,
    40				Field: field,
    41			})
    42		{{end}}
    43		switch field.Name {
    44		case "__typename":
    45			out.Values[i] = graphql.MarshalString({{$object.Name|quote}})
    46		{{- range $field := $object.Fields }}
    47		case "{{$field.Name}}":
    48			{{- if $field.IsConcurrent }}
    49				field := field
    50
    51				innerFunc := func(ctx context.Context, fs *graphql.FieldSet) (res graphql.Marshaler) {
    52					defer func() {
    53						if r := recover(); r != nil {
    54							ec.Error(ctx, ec.Recover(ctx, r))
    55						}
    56					}()
    57					res = ec._{{$object.Name}}_{{$field.Name}}(ctx, field{{if not $object.Root}}, obj{{end}})
    58					{{- if $field.TypeReference.GQL.NonNull }}
    59						if res == graphql.Null {
    60							{{- if $object.IsConcurrent }}
    61								atomic.AddUint32(&fs.Invalids, 1)
    62							{{- else }}
    63								fs.Invalids++
    64							{{- end }}
    65						}
    66					{{- end }}
    67					return res
    68				}
    69
    70				{{if $object.Root}}
    71					rrm := func(ctx context.Context) graphql.Marshaler {
    72						return ec.OperationContext.RootResolverMiddleware(ctx,
    73							func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) })
    74					}
    75				{{end}}
    76
    77				{{if not $object.Root}}
    78					if field.Deferrable != nil {
    79						dfs, ok := deferred[field.Deferrable.Label]
    80						di := 0
    81						if ok {
    82							dfs.AddField(field)
    83							di = len(dfs.Values) - 1
    84						} else {
    85							dfs = graphql.NewFieldSet([]graphql.CollectedField{field})
    86							deferred[field.Deferrable.Label] = dfs
    87						}
    88						dfs.Concurrently(di, func(ctx context.Context) graphql.Marshaler {
    89							return innerFunc(ctx, dfs)
    90						})
    91
    92						// don't run the out.Concurrently() call below
    93						out.Values[i] = graphql.Null
    94						continue
    95					}
    96				{{end}}
    97
    98				out.Concurrently(i, func(ctx context.Context) graphql.Marshaler {
    99					{{- if $object.Root -}}
   100						return rrm(innerCtx)
   101					{{- else -}}
   102						return innerFunc(ctx, out)
   103					{{- end -}}
   104				})
   105			{{- else }}
   106				{{- if $object.Root -}}
   107					out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) {
   108						return ec._{{$object.Name}}_{{$field.Name}}(ctx, field)
   109					})
   110				{{- else -}}
   111					out.Values[i] = ec._{{$object.Name}}_{{$field.Name}}(ctx, field, obj)
   112				{{- end -}}
   113
   114				{{- if $field.TypeReference.GQL.NonNull }}
   115					if out.Values[i] == graphql.Null {
   116						{{- if $object.IsConcurrent }}
   117							atomic.AddUint32(&out.Invalids, 1)
   118						{{- else }}
   119							out.Invalids++
   120						{{- end }}
   121					}
   122				{{- end }}
   123			{{- end }}
   124		{{- end }}
   125		default:
   126			panic("unknown field " + strconv.Quote(field.Name))
   127		}
   128	}
   129	out.Dispatch(ctx)
   130	if out.Invalids > 0 { return graphql.Null }
   131
   132	atomic.AddInt32(&ec.deferred, int32(len(deferred)))
   133
   134	for label, dfs := range deferred {
   135		ec.processDeferredGroup(graphql.DeferredGroup{
   136			Label:    label,
   137			Path:     graphql.GetPath(ctx),
   138			FieldSet: dfs,
   139			Context:  ctx,
   140		})
   141	}
   142
   143	return out
   144}
   145{{- end }}
   146
   147{{- end }}

View as plain text