aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/codegen/templates/generated.gotpl
blob: 8250bc7abf4b8743439dbb6f2f4964c79bbf1a16 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.

package {{ .PackageName }}

import (
{{- range $import := .Imports }}
	{{- $import.Write }}
{{ end }}
)

// NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.
func NewExecutableSchema(cfg Config) graphql.ExecutableSchema {
	return &executableSchema{
		resolvers: cfg.Resolvers,
		directives: cfg.Directives,
		complexity: cfg.Complexity,
	}
}

type Config struct {
	Resolvers  ResolverRoot
	Directives DirectiveRoot
	Complexity ComplexityRoot
}

type ResolverRoot interface {
{{- range $object := .Objects -}}
	{{ if $object.HasResolvers -}}
		{{$object.GQLType}}() {{$object.GQLType}}Resolver
	{{ end }}
{{- end }}
}

type DirectiveRoot struct {
{{ range $directive := .Directives }}
	{{ $directive.Declaration }}
{{ end }}
}

type ComplexityRoot struct {
{{ range $object := .Objects }}
	{{ if not $object.IsReserved -}}
		{{ $object.GQLType|toCamel }} struct {
		{{ range $field := $object.Fields -}}
			{{ if not $field.IsReserved -}}
				{{ $field.GQLName|toCamel }} {{ $field.ComplexitySignature }}
			{{ end }}
		{{- end }}
		}
	{{- end }}
{{ end }}
}

{{ range $object := .Objects -}}
	{{ if $object.HasResolvers }}
		type {{$object.GQLType}}Resolver interface {
		{{ range $field := $object.Fields -}}
			{{ $field.ShortResolverDeclaration }}
		{{ end }}
		}
	{{- end }}
{{- end }}

{{ range $object := .Objects -}}
	{{ range $field := $object.Fields -}}
		{{ if $field.Args }}
			func {{ $field.ArgsFunc }}(rawArgs map[string]interface{}) (map[string]interface{}, error) {
			{{ template "args.gotpl" $field.Args }}
			}
		{{ end }}
	{{ end }}
{{- end }}

{{ range $directive := .Directives }}
	{{ if $directive.Args }}
		func {{ $directive.ArgsFunc }}(rawArgs map[string]interface{}) (map[string]interface{}, error) {
		{{ template "args.gotpl" $directive.Args }}
		}
	{{ end }}
{{ end }}

type executableSchema struct {
	resolvers  ResolverRoot
	directives DirectiveRoot
	complexity ComplexityRoot
}

func (e *executableSchema) Schema() *ast.Schema {
	return parsedSchema
}

func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) {
	switch typeName + "." + field {
	{{ range $object := .Objects }}
		{{ if not $object.IsReserved }}
			{{ range $field := $object.Fields }}
				{{ if not $field.IsReserved }}
					case "{{$object.GQLType}}.{{$field.GQLName}}":
						if e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}} == nil {
							break
						}
						{{ if $field.Args }}
							args, err := {{ $field.ArgsFunc }}(rawArgs)
							if err != nil {
								return 0, false
							}
						{{ end }}
						return e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{end}}), true
				{{ end }}
			{{ end }}
		{{ end }}
	{{ end }}
	}
	return 0, false
}

func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {
	{{- if .QueryRoot }}
		ec := executionContext{graphql.GetRequestContext(ctx), e}

		buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
			data := ec._{{.QueryRoot.GQLType}}(ctx, op.SelectionSet)
			var buf bytes.Buffer
			data.MarshalGQL(&buf)
			return buf.Bytes()
		})

		return &graphql.Response{
			Data:   buf,
			Errors: ec.Errors,
		}
	{{- else }}
		return graphql.ErrorResponse(ctx, "queries are not supported")
	{{- end }}
}

func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {
	{{- if .MutationRoot }}
		ec := executionContext{graphql.GetRequestContext(ctx), e}

		buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
			data := ec._{{.MutationRoot.GQLType}}(ctx, op.SelectionSet)
			var buf bytes.Buffer
			data.MarshalGQL(&buf)
			return buf.Bytes()
		})

		return &graphql.Response{
			Data:   buf,
			Errors: ec.Errors,
		}
	{{- else }}
		return graphql.ErrorResponse(ctx, "mutations are not supported")
	{{- end }}
}

func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response {
	{{- if .SubscriptionRoot }}
		ec := executionContext{graphql.GetRequestContext(ctx), e}

		next := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.SelectionSet)
		if ec.Errors != nil {
			return graphql.OneShot(&graphql.Response{Data: []byte("null"), Errors: ec.Errors})
		}

		var buf bytes.Buffer
		return func() *graphql.Response {
			buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
				buf.Reset()
				data := next()

				if data == nil {
					return nil
				}
				data.MarshalGQL(&buf)
				return buf.Bytes()
			})

			if buf == nil {
				return nil
			}

			return &graphql.Response{
				Data:   buf,
				Errors: ec.Errors,
			}
		}
	{{- else }}
		return graphql.OneShot(graphql.ErrorResponse(ctx, "subscriptions are not supported"))
	{{- end }}
}

type executionContext struct {
	*graphql.RequestContext
	*executableSchema
}

{{- range $object := .Objects }}
	{{ template "object.gotpl" $object }}

	{{- range $field := $object.Fields }}
		{{ template "field.gotpl" $field }}
	{{ end }}
{{- end}}

{{- range $interface := .Interfaces }}
	{{ template "interface.gotpl" $interface }}
{{- end }}

{{- range $input := .Inputs }}
	{{ template "input.gotpl" $input }}
{{- end }}

func (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{}, next graphql.Resolver) (ret interface{}) {
	defer func() {
		if r := recover(); r != nil {
			ec.Error(ctx, ec.Recover(ctx, r))
			ret = nil
		}
	}()
	{{- if .Directives }}
	rctx := graphql.GetResolverContext(ctx)
	for _, d := range rctx.Field.Definition.Directives {
		switch d.Name {
		{{- range $directive := .Directives }}
		case "{{$directive.Name}}":
			if ec.directives.{{$directive.Name|ucFirst}} != nil {
				{{- if $directive.Args }}
					rawArgs := d.ArgumentMap(ec.Variables)
					args, err := {{ $directive.ArgsFunc }}(rawArgs)
					if err != nil {
						ec.Error(ctx, err)
						return nil
					}
				{{- end }}
				n := next
				next = func(ctx context.Context) (interface{}, error) {
					return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
				}
			}
		{{- end }}
		}
	}
	{{- end }}
	res, err := ec.ResolverMiddleware(ctx, next)
	if err != nil {
		ec.Error(ctx, err)
		return nil
	}
	return res
}

func (ec *executionContext) introspectSchema() *introspection.Schema {
	return introspection.WrapSchema(parsedSchema)
}

func (ec *executionContext) introspectType(name string) *introspection.Type {
	return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name])
}

var parsedSchema = gqlparser.MustLoadSchema(
	&ast.Source{Name: {{.SchemaFilename|quote}}, Input: {{.SchemaRaw|rawQuote}}},
)