aboutsummaryrefslogblamecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/codegen/field.gotpl
blob: 1970614be279239942f7b4ad920378ec58dc8a49 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                                                                     

                        




                                                                                                                                                   

                                                                  
                                                                          




                                                  


                                                                                                                           









                                                                         






                                                                                                                   


                 
                                                                                                                                                                                                                   
                                                               






                                                                 






                                                                           

                                                                  
                                                                          



                                                   
                                        
                          
                                                                      












                                                                                                                                                      
                                  
                                                                  





                                                                          
                                                                 
                                 
                                                             
                                                                                            

         

                   














                                                                                    




                                                   






























                                                                                                                   
{{- 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}}) (ret graphql.Marshaler) {
		ctx = ec.Tracer.StartFieldExecution(ctx, field)
		defer func () {
			if r := recover(); r != nil {
				ec.Error(ctx, ec.Recover(ctx, r))
				ret = graphql.Null
			}
			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)
		{{- if  $.Directives.LocationDirectives "FIELD" }}
			resTmp := ec._fieldMiddleware(ctx, {{if $object.Root}}nil{{else}}obj{{end}}, func(rctx context.Context) (interface{}, error) {
				{{ template "field" $field }}
			})
		{{ else }}
			resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
				{{ template "field" $field }}
			})
			if err != nil {
				ec.Error(ctx, err)
				return graphql.Null
			}
		{{- 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}}

{{ define "field" }}
	{{- if .HasDirectives -}}
		directive0 := func(rctx context.Context) (interface{}, error) {
			ctx = rctx  // use context from middleware stack in children
			{{ template "fieldDefinition" . }}
		}
		{{ template "implDirectives" . }}
		tmp, err := directive{{.ImplDirectives|len}}(rctx)
		if err != nil {
			return nil, err
		}
		if data, ok := tmp.({{ .TypeReference.GO | ref }}) ; ok {
			return data, nil
		}
		{{- if .TypeReference.IsNilable -}}
			else if tmp == nil {
				return nil, nil
			}
		{{- end }}
		return nil, fmt.Errorf(`unexpected type %T from directive, should be {{ .TypeReference.GO }}`, tmp)
	{{- else -}}
		ctx = rctx  // use context from middleware stack in children
		{{ template "fieldDefinition" . }}
	{{- end -}}
{{ end }}

{{ define "fieldDefinition" }}
	{{- if .IsResolver -}}
		return ec.resolvers.{{ .ShortInvocation }}
	{{- else if .IsMap -}}
		switch v := {{.GoReceiverName}}[{{.Name|quote}}].(type) {
		case {{.TypeReference.GO | ref}}:
			return v, nil
		case {{.TypeReference.Elem.GO | ref}}:
			return &v, nil
		case nil:
			return ({{.TypeReference.GO | ref}})(nil), nil
		default:
			return nil, fmt.Errorf("unexpected type %T for field %s", v, {{ .Name | quote}})
		}
	{{- else if .IsMethod -}}
		{{- if .NoErr -}}
			return {{.GoReceiverName}}.{{.GoFieldName}}({{ .CallArgs }}), nil
		{{- else -}}
			return {{.GoReceiverName}}.{{.GoFieldName}}({{ .CallArgs }})
		{{- end -}}
	{{- else if .IsVariable -}}
		return {{.GoReceiverName}}.{{.GoFieldName}}, nil
	{{- end }}
{{- end }}