aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/codegen/field.gotpl
blob: 9718a08aadf7016ec714ef450ee727c75e891842 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{{- range $object := .Objects }}{{- range $field := $object.Fields }}

{{- if $object.Stream }}
	func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Context, field graphql.CollectedField) func() graphql.Marshaler {
		ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{
			Field: field,
			Args:  nil,
		})
		{{- if $field.Args }}
			rawArgs := field.ArgumentMap(ec.Variables)
			args, err := ec.{{ $field.ArgsFunc }}(ctx,rawArgs)
			if err != nil {
				ec.Error(ctx, err)
				return nil
			}
		{{- end }}
		// FIXME: subscriptions are missing request middleware stack https://github.com/99designs/gqlgen/issues/259
		//          and Tracer stack
		rctx := ctx
		results, err := ec.resolvers.{{ $field.ShortInvocation }}
		if err != nil {
			ec.Error(ctx, err)
			return nil
		}
		return func() graphql.Marshaler {
			res, ok := <-results
			if !ok {
				return nil
			}
			return graphql.WriterFunc(func(w io.Writer) {
				w.Write([]byte{'{'})
				graphql.MarshalString(field.Alias).MarshalGQL(w)
				w.Write([]byte{':'})
				ec.{{ $field.TypeReference.MarshalFunc }}(ctx, field.Selections, res).MarshalGQL(w)
				w.Write([]byte{'}'})
			})
		}
	}
{{ else }}
	func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Context, field graphql.CollectedField{{ if not $object.Root }}, obj {{$object.Reference | ref}}{{end}}) graphql.Marshaler {
		ctx = ec.Tracer.StartFieldExecution(ctx, field)
		defer func () { ec.Tracer.EndFieldExecution(ctx) }()
		rctx := &graphql.ResolverContext{
			Object: {{$object.Name|quote}},
			Field: field,
			Args:  nil,
			IsMethod: {{or $field.IsMethod $field.IsResolver}},
		}
		ctx = graphql.WithResolverContext(ctx, rctx)
		{{- if $field.Args }}
			rawArgs := field.ArgumentMap(ec.Variables)
			args, err := ec.{{ $field.ArgsFunc }}(ctx,rawArgs)
			if err != nil {
				ec.Error(ctx, err)
				return graphql.Null
			}
			rctx.Args = args
		{{- end }}
		ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx)
		resTmp := ec.FieldMiddleware(ctx, {{if $object.Root}}nil{{else}}obj{{end}}, func(rctx context.Context) (interface{}, error) {
			ctx = rctx  // use context from middleware stack in children
			{{- if $field.IsResolver }}
				return ec.resolvers.{{ $field.ShortInvocation }}
			{{- else if $field.IsMap }}
				switch v := {{$field.GoReceiverName}}[{{$field.Name|quote}}].(type) {
				case {{$field.TypeReference.GO | ref}}:
					return v, nil
				case {{$field.TypeReference.Elem.GO | ref}}:
					return &v, nil
				case nil:
					return ({{$field.TypeReference.GO | ref}})(nil), nil
				default:
					return nil, fmt.Errorf("unexpected type %T for field %s", v, {{ $field.Name | quote}})
				}
			{{- else if $field.IsMethod }}
				{{- if $field.NoErr }}
					return {{$field.GoReceiverName}}.{{$field.GoFieldName}}({{ $field.CallArgs }}), nil
				{{- else }}
					return {{$field.GoReceiverName}}.{{$field.GoFieldName}}({{ $field.CallArgs }})
				{{- end }}
			{{- else if $field.IsVariable }}
				return {{$field.GoReceiverName}}.{{$field.GoFieldName}}, nil
			{{- end }}
		})
		if resTmp == nil {
			{{- if $field.TypeReference.GQL.NonNull }}
				if !ec.HasError(rctx) {
					ec.Errorf(ctx, "must not be null")
				}
			{{- end }}
			return graphql.Null
		}
		res := resTmp.({{$field.TypeReference.GO | ref}})
		rctx.Result = res
		ctx = ec.Tracer.StartFieldChildExecution(ctx)
		return ec.{{ $field.TypeReference.MarshalFunc }}(ctx, field.Selections, res)
	}
{{ end }}

{{- end }}{{- end}}