1{{ reserveImport "context" }}
2{{ reserveImport "fmt" }}
3{{ reserveImport "io" }}
4{{ reserveImport "strconv" }}
5{{ reserveImport "time" }}
6{{ reserveImport "sync" }}
7{{ reserveImport "sync/atomic" }}
8{{ reserveImport "errors" }}
9{{ reserveImport "bytes" }}
10
11{{ reserveImport "github.com/vektah/gqlparser" }}
12{{ reserveImport "github.com/vektah/gqlparser/ast" }}
13{{ reserveImport "github.com/99designs/gqlgen/graphql" }}
14{{ reserveImport "github.com/99designs/gqlgen/graphql/introspection" }}
15
16
17// NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.
18func NewExecutableSchema(cfg Config) graphql.ExecutableSchema {
19 return &executableSchema{
20 resolvers: cfg.Resolvers,
21 directives: cfg.Directives,
22 complexity: cfg.Complexity,
23 }
24}
25
26type Config struct {
27 Resolvers ResolverRoot
28 Directives DirectiveRoot
29 Complexity ComplexityRoot
30}
31
32type ResolverRoot interface {
33{{- range $object := .Objects -}}
34 {{ if $object.HasResolvers -}}
35 {{$object.Name}}() {{$object.Name}}Resolver
36 {{ end }}
37{{- end }}
38}
39
40type DirectiveRoot struct {
41{{ range $directive := .Directives }}
42 {{ $directive.Declaration }}
43{{ end }}
44}
45
46type ComplexityRoot struct {
47{{ range $object := .Objects }}
48 {{ if not $object.IsReserved -}}
49 {{ $object.Name|go }} struct {
50 {{ range $_, $fields := $object.UniqueFields }}
51 {{- $field := index $fields 0 -}}
52 {{ if not $field.IsReserved -}}
53 {{ $field.GoFieldName }} {{ $field.ComplexitySignature }}
54 {{ end }}
55 {{- end }}
56 }
57 {{- end }}
58{{ end }}
59}
60
61{{ range $object := .Objects -}}
62 {{ if $object.HasResolvers }}
63 type {{$object.Name}}Resolver interface {
64 {{ range $field := $object.Fields -}}
65 {{- if $field.IsResolver }}
66 {{- $field.GoFieldName}}{{ $field.ShortResolverDeclaration }}
67 {{- end }}
68 {{ end }}
69 }
70 {{- end }}
71{{- end }}
72
73type executableSchema struct {
74 resolvers ResolverRoot
75 directives DirectiveRoot
76 complexity ComplexityRoot
77}
78
79func (e *executableSchema) Schema() *ast.Schema {
80 return parsedSchema
81}
82
83func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) {
84 ec := executionContext{nil, e}
85 _ = ec
86 switch typeName + "." + field {
87 {{ range $object := .Objects }}
88 {{ if not $object.IsReserved }}
89 {{ range $_, $fields := $object.UniqueFields }}
90 {{- $len := len $fields }}
91 {{- range $i, $field := $fields }}
92 {{- $last := eq (add $i 1) $len }}
93 {{- if not $field.IsReserved }}
94 {{- if eq $i 0 }}case {{ end }}"{{$object.Name}}.{{$field.Name}}"{{ if not $last }},{{ else }}:
95 if e.complexity.{{$object.Name|go}}.{{$field.GoFieldName}} == nil {
96 break
97 }
98 {{ if $field.Args }}
99 args, err := ec.{{ $field.ArgsFunc }}(context.TODO(),rawArgs)
100 if err != nil {
101 return 0, false
102 }
103 {{ end }}
104 return e.complexity.{{$object.Name|go}}.{{$field.GoFieldName}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{ end }}), true
105 {{ end }}
106 {{- end }}
107 {{- end }}
108 {{ end }}
109 {{ end }}
110 {{ end }}
111 }
112 return 0, false
113}
114
115func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {
116 {{- if .QueryRoot }}
117 ec := executionContext{graphql.GetRequestContext(ctx), e}
118
119 buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
120 {{ if .Directives.LocationDirectives "QUERY" -}}
121 data := ec._queryMiddleware(ctx, op, func(ctx context.Context) (interface{}, error){
122 return ec._{{.QueryRoot.Name}}(ctx, op.SelectionSet), nil
123 })
124 {{- else -}}
125 data := ec._{{.QueryRoot.Name}}(ctx, op.SelectionSet)
126 {{- end }}
127 var buf bytes.Buffer
128 data.MarshalGQL(&buf)
129 return buf.Bytes()
130 })
131
132 return &graphql.Response{
133 Data: buf,
134 Errors: ec.Errors,
135 Extensions: ec.Extensions,
136 }
137 {{- else }}
138 return graphql.ErrorResponse(ctx, "queries are not supported")
139 {{- end }}
140}
141
142func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {
143 {{- if .MutationRoot }}
144 ec := executionContext{graphql.GetRequestContext(ctx), e}
145
146 buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
147 {{ if .Directives.LocationDirectives "MUTATION" -}}
148 data := ec._mutationMiddleware(ctx, op, func(ctx context.Context) (interface{}, error){
149 return ec._{{.MutationRoot.Name}}(ctx, op.SelectionSet), nil
150 })
151 {{- else -}}
152 data := ec._{{.MutationRoot.Name}}(ctx, op.SelectionSet)
153 {{- end }}
154 var buf bytes.Buffer
155 data.MarshalGQL(&buf)
156 return buf.Bytes()
157 })
158
159 return &graphql.Response{
160 Data: buf,
161 Errors: ec.Errors,
162 Extensions: ec.Extensions,
163 }
164 {{- else }}
165 return graphql.ErrorResponse(ctx, "mutations are not supported")
166 {{- end }}
167}
168
169func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response {
170 {{- if .SubscriptionRoot }}
171 ec := executionContext{graphql.GetRequestContext(ctx), e}
172
173 {{ if .Directives.LocationDirectives "SUBSCRIPTION" -}}
174 next := ec._subscriptionMiddleware(ctx, op, func(ctx context.Context) (interface{}, error){
175 return ec._{{.SubscriptionRoot.Name}}(ctx, op.SelectionSet),nil
176 })
177 {{- else -}}
178 next := ec._{{.SubscriptionRoot.Name}}(ctx, op.SelectionSet)
179 {{- end }}
180 if ec.Errors != nil {
181 return graphql.OneShot(&graphql.Response{Data: []byte("null"), Errors: ec.Errors})
182 }
183
184 var buf bytes.Buffer
185 return func() *graphql.Response {
186 buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
187 buf.Reset()
188 data := next()
189
190 if data == nil {
191 return nil
192 }
193 data.MarshalGQL(&buf)
194 return buf.Bytes()
195 })
196
197 if buf == nil {
198 return nil
199 }
200
201 return &graphql.Response{
202 Data: buf,
203 Errors: ec.Errors,
204 Extensions: ec.Extensions,
205 }
206 }
207 {{- else }}
208 return graphql.OneShot(graphql.ErrorResponse(ctx, "subscriptions are not supported"))
209 {{- end }}
210}
211
212type executionContext struct {
213 *graphql.RequestContext
214 *executableSchema
215}
216
217func (ec *executionContext) introspectSchema() (*introspection.Schema, error) {
218 if ec.DisableIntrospection {
219 return nil, errors.New("introspection disabled")
220 }
221 return introspection.WrapSchema(parsedSchema), nil
222}
223
224func (ec *executionContext) introspectType(name string) (*introspection.Type, error) {
225 if ec.DisableIntrospection {
226 return nil, errors.New("introspection disabled")
227 }
228 return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name]), nil
229}
230
231var parsedSchema = gqlparser.MustLoadSchema(
232 {{- range $filename, $schema := .SchemaStr }}
233 &ast.Source{Name: {{$filename|quote}}, Input: {{$schema|rawQuote}}},
234 {{- end }}
235)