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

     1  {{ $object := . }}
     2  
     3  var {{ $object.GQLType|lcFirst}}Implementors = {{$object.Implementors}}
     4  
     5  // nolint: gocyclo, errcheck, gas, goconst
     6  {{- if .Stream }}
     7  func (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel ast.SelectionSet) func() graphql.Marshaler {
     8  	fields := graphql.CollectFields(ctx, sel, {{$object.GQLType|lcFirst}}Implementors)
     9  	ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{
    10  		Object: {{$object.GQLType|quote}},
    11  	})
    12  	if len(fields) != 1 {
    13  		ec.Errorf(ctx, "must subscribe to exactly one stream")
    14  		return nil
    15  	}
    16  
    17  	switch fields[0].Name {
    18  	{{- range $field := $object.Fields }}
    19  	case "{{$field.GQLName}}":
    20  		return ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, fields[0])
    21  	{{- end }}
    22  	default:
    23  		panic("unknown field " + strconv.Quote(fields[0].Name))
    24  	}
    25  }
    26  {{- else }}
    27  func (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel ast.SelectionSet{{if not $object.Root}}, obj *{{$object.FullName}} {{end}}) graphql.Marshaler {
    28  	fields := graphql.CollectFields(ctx, sel, {{$object.GQLType|lcFirst}}Implementors)
    29  	{{if $object.Root}}
    30  		ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{
    31  			Object: {{$object.GQLType|quote}},
    32  		})
    33  	{{end}}
    34  
    35  	{{if $object.IsConcurrent}} var wg sync.WaitGroup {{end}}
    36  	out := graphql.NewOrderedMap(len(fields))
    37  	invalid := false
    38  	for i, field := range fields {
    39  		out.Keys[i] = field.Alias
    40  
    41  		switch field.Name {
    42  		case "__typename":
    43  			out.Values[i] = graphql.MarshalString({{$object.GQLType|quote}})
    44  		{{- range $field := $object.Fields }}
    45  		case "{{$field.GQLName}}":
    46  			{{- if $field.IsConcurrent }}
    47  				wg.Add(1)
    48  				go func(i int, field graphql.CollectedField) {
    49  			{{- end }}
    50  				out.Values[i] = ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, field{{if not $object.Root}}, obj{{end}})
    51  				{{- if $field.ASTType.NonNull }}
    52  					if out.Values[i] == graphql.Null {
    53  						invalid = true
    54  					}
    55  				{{- end }}
    56  			{{- if $field.IsConcurrent }}
    57  					wg.Done()
    58  				}(i, field)
    59  			{{- end }}
    60  		{{- end }}
    61  		default:
    62  			panic("unknown field " + strconv.Quote(field.Name))
    63  		}
    64  	}
    65  	{{if $object.IsConcurrent}} wg.Wait() {{end}}
    66  	if invalid { return graphql.Null }
    67  	return out
    68  }
    69  {{- end }}