diff options
author | Michael Muré <batolettre@gmail.com> | 2018-08-14 14:48:41 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-08-14 14:48:41 +0200 |
commit | 5c568a362b73cf163b06bc7371982f9a7ceaaf29 (patch) | |
tree | 755b61f969913b5290f3d56f4cfba4070b8155bc /vendor/github.com/vektah/gqlgen/codegen/templates/object.gotpl | |
parent | ef0d8fa108fbdef24997a84a3c6ecbf21cbc0a9e (diff) | |
download | git-bug-5c568a362b73cf163b06bc7371982f9a7ceaaf29.tar.gz |
gqlgen: add a small program to go:generate the code
Diffstat (limited to 'vendor/github.com/vektah/gqlgen/codegen/templates/object.gotpl')
-rw-r--r-- | vendor/github.com/vektah/gqlgen/codegen/templates/object.gotpl | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/vendor/github.com/vektah/gqlgen/codegen/templates/object.gotpl b/vendor/github.com/vektah/gqlgen/codegen/templates/object.gotpl new file mode 100644 index 00000000..b531d5fe --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/templates/object.gotpl @@ -0,0 +1,52 @@ +{{ $object := . }} + +var {{ $object.GQLType|lcFirst}}Implementors = {{$object.Implementors}} + +// nolint: gocyclo, errcheck, gas, goconst +{{- if .Stream }} +func (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel []query.Selection) func() graphql.Marshaler { + fields := graphql.CollectFields(ec.Doc, sel, {{$object.GQLType|lcFirst}}Implementors, ec.Variables) + ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ + Object: {{$object.GQLType|quote}}, + }) + if len(fields) != 1 { + ec.Errorf(ctx, "must subscribe to exactly one stream") + return nil + } + + switch fields[0].Name { + {{- range $field := $object.Fields }} + case "{{$field.GQLName}}": + return ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, fields[0]) + {{- end }} + default: + panic("unknown field " + strconv.Quote(fields[0].Name)) + } +} +{{- else }} +func (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel []query.Selection{{if not $object.Root}}, obj *{{$object.FullName}} {{end}}) graphql.Marshaler { + fields := graphql.CollectFields(ec.Doc, sel, {{$object.GQLType|lcFirst}}Implementors, ec.Variables) + {{if $object.Root}} + ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ + Object: {{$object.GQLType|quote}}, + }) + {{end}} + out := graphql.NewOrderedMap(len(fields)) + for i, field := range fields { + out.Keys[i] = field.Alias + + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString({{$object.GQLType|quote}}) + {{- range $field := $object.Fields }} + case "{{$field.GQLName}}": + out.Values[i] = ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, field{{if not $object.Root}}, obj{{end}}) + {{- end }} + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} +{{- end }} |