aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/codegen/generated!.gotpl
blob: 5753f1d1360962860ec126adeabc5133e026672b (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
{{ reserveImport "context"  }}
{{ reserveImport "fmt"  }}
{{ reserveImport "io"  }}
{{ reserveImport "strconv"  }}
{{ reserveImport "time"  }}
{{ reserveImport "sync"  }}
{{ reserveImport "sync/atomic" }}
{{ reserveImport "errors"  }}
{{ reserveImport "bytes"  }}

{{ reserveImport "github.com/vektah/gqlparser" }}
{{ reserveImport "github.com/vektah/gqlparser/ast" }}
{{ reserveImport "github.com/99designs/gqlgen/graphql" }}
{{ reserveImport "github.com/99designs/gqlgen/graphql/introspection" }}


// 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.Name}}() {{$object.Name}}Resolver
	{{ end }}
{{- end }}
}

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

type ComplexityRoot struct {
{{ range $object := .Objects }}
	{{ if not $object.IsReserved -}}
		{{ $object.Name|go }} struct {
		{{ range $_, $fields := $object.UniqueFields }}
			{{- $field := index $fields 0 -}}
			{{ if not $field.IsReserved -}}
				{{ $field.GoFieldName }} {{ $field.ComplexitySignature }}
			{{ end }}
		{{- end }}
		}
	{{- end }}
{{ end }}
}

{{ range $object := .Objects -}}
	{{ if $object.HasResolvers }}
		type {{$object.Name}}Resolver interface {
		{{ range $field := $object.Fields -}}
			{{- if $field.IsResolver }}
				{{- $field.GoFieldName}}{{ $field.ShortResolverDeclaration }}
			{{- end }}
		{{ end }}
		}
	{{- 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) {
	ec := executionContext{nil, e}
	_ = ec
	switch typeName + "." + field {
	{{ range $object := .Objects }}
		{{ if not $object.IsReserved }}
			{{ range $_, $fields := $object.UniqueFields }}
				{{- $len := len $fields }}
				{{- range $i, $field := $fields }}
					{{- $last := eq (add $i 1) $len }}
					{{- if not $field.IsReserved }}
						{{- if eq $i 0 }}case {{ end }}"{{$object.Name}}.{{$field.Name}}"{{ if not $last }},{{ else }}:
							if e.complexity.{{$object.Name|go}}.{{$field.GoFieldName}} == nil {
								break
							}
							{{ if $field.Args }}
								args, err := ec.{{ $field.ArgsFunc }}(context.TODO(),rawArgs)
								if err != nil {
									return 0, false
								}
							{{ end }}
							return e.complexity.{{$object.Name|go}}.{{$field.GoFieldName}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{ end }}), true
						{{ end }}
					{{- end }}
				{{- 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.Name}}(ctx, op.SelectionSet)
			var buf bytes.Buffer
			data.MarshalGQL(&buf)
			return buf.Bytes()
		})

		return &graphql.Response{
			Data:       buf,
			Errors:     ec.Errors,
			Extensions: ec.Extensions,
		}
	{{- 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.Name}}(ctx, op.SelectionSet)
			var buf bytes.Buffer
			data.MarshalGQL(&buf)
			return buf.Bytes()
		})

		return &graphql.Response{
			Data:       buf,
			Errors:     ec.Errors,
			Extensions: ec.Extensions,
		}
	{{- 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.Name}}(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,
				Extensions: ec.Extensions,
			}
		}
	{{- else }}
		return graphql.OneShot(graphql.ErrorResponse(ctx, "subscriptions are not supported"))
	{{- end }}
}

type executionContext struct {
	*graphql.RequestContext
	*executableSchema
}

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 := ec.{{ $directive.ArgsFunc }}(ctx,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, error) {
	if ec.DisableIntrospection {
		return nil, errors.New("introspection disabled")
	}
	return introspection.WrapSchema(parsedSchema), nil
}

func (ec *executionContext) introspectType(name string) (*introspection.Type, error) {
	if ec.DisableIntrospection {
		return nil, errors.New("introspection disabled")
	}
	return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name]), nil
}

var parsedSchema = gqlparser.MustLoadSchema(
	{{- range $filename, $schema := .SchemaStr }}
		&ast.Source{Name: {{$filename|quote}}, Input: {{$schema|rawQuote}}},
	{{- end }}
)