github.com/pouriasharifi/gqlgen@v0.7.2/codegen/templates/field.gotpl (about)

     1  {{ $field := . }}
     2  {{ $object := $field.Object }}
     3  
     4  {{- if $object.Stream }}
     5  	func (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField) func() graphql.Marshaler {
     6  		{{- if $field.Args }}
     7  			rawArgs := field.ArgumentMap(ec.Variables)
     8  			args, err := {{ $field.ArgsFunc }}(rawArgs)
     9  			if err != nil {
    10  				ec.Error(ctx, err)
    11  				return nil
    12  			}
    13  		{{- end }}
    14  		ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{
    15  			Field: field,
    16  		})
    17  		// FIXME: subscriptions are missing request middleware stack https://github.com/99designs/gqlgen/issues/259
    18  		//          and Tracer stack
    19  		rctx := ctx
    20  		results, err := ec.resolvers.{{ $field.ShortInvocation }}
    21  		if err != nil {
    22  			ec.Error(ctx, err)
    23  			return nil
    24  		}
    25  		return func() graphql.Marshaler {
    26  			res, ok := <-results
    27  			if !ok {
    28  				return nil
    29  			}
    30  			var out graphql.OrderedMap
    31  			out.Add(field.Alias, func() graphql.Marshaler { {{ $field.WriteJson }} }())
    32  			return &out
    33  		}
    34  	}
    35  {{ else }}
    36  	// nolint: vetshadow
    37  	func (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField, {{if not $object.Root}}obj *{{$object.FullName}}{{end}}) graphql.Marshaler {
    38  		ctx = ec.Tracer.StartFieldExecution(ctx, field)
    39  		defer func () { ec.Tracer.EndFieldExecution(ctx) }()
    40  		{{- if $field.Args }}
    41  			rawArgs := field.ArgumentMap(ec.Variables)
    42  			args, err := {{ $field.ArgsFunc }}(rawArgs)
    43  			if err != nil {
    44  				ec.Error(ctx, err)
    45  				return graphql.Null
    46  			}
    47  		{{- end }}
    48  		rctx := &graphql.ResolverContext{
    49  			Object: {{$object.GQLType|quote}},
    50  			Args: {{if $field.Args }}args{{else}}nil{{end}},
    51  			Field: field,
    52  		}
    53  		ctx = graphql.WithResolverContext(ctx, rctx)
    54  		ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx)
    55  		resTmp := ec.FieldMiddleware(ctx, {{if $object.Root}}nil{{else}}obj{{end}}, func(rctx context.Context) (interface{}, error) {
    56  			ctx = rctx  // use context from middleware stack in children
    57  			{{- if $field.IsResolver }}
    58  				return ec.resolvers.{{ $field.ShortInvocation }}
    59  			{{- else if $field.IsMethod }}
    60  				{{- if $field.NoErr }}
    61  					return {{$field.GoReceiverName}}.{{$field.GoFieldName}}({{ $field.CallArgs }}), nil
    62  				{{- else }}
    63  					return {{$field.GoReceiverName}}.{{$field.GoFieldName}}({{ $field.CallArgs }})
    64  				{{- end }}
    65  			{{- else if $field.IsVariable }}
    66  				return {{$field.GoReceiverName}}.{{$field.GoFieldName}}, nil
    67  			{{- end }}
    68  		})
    69  		if resTmp == nil {
    70  			{{- if $field.ASTType.NonNull }}
    71  				if !ec.HasError(rctx) {
    72  					ec.Errorf(ctx, "must not be null")
    73  				}
    74  			{{- end }}
    75  			return graphql.Null
    76  		}
    77  		res := resTmp.({{$field.Signature}})
    78  		rctx.Result = res
    79  		ctx = ec.Tracer.StartFieldChildExecution(ctx)
    80  		{{ $field.WriteJson }}
    81  	}
    82  {{ end }}