aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/vektah/gqlgen/codegen/templates/generated.gotpl
blob: cc1dc459b0701f875a46c1dfc0c5504a6b47bf7e (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
// Code generated by github.com/vektah/gqlgen, DO NOT EDIT.

package {{ .PackageName }}

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

// MakeExecutableSchema creates an ExecutableSchema from the Resolvers interface.
func MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema {
	return &executableSchema{resolvers: resolvers}
}

// NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.
func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema {
	return MakeExecutableSchema(shortMapper{r: resolvers})
}

type Resolvers interface {
{{- range $object := .Objects -}}
	{{ range $field := $object.Fields -}}
		{{ $field.ResolverDeclaration }}
	{{ end }}
{{- end }}
}

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

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

type shortMapper struct {
	r ResolverRoot
}

{{- range $object := .Objects -}}
	{{ range $field := $object.Fields -}}
		{{- if $field.IsResolver }}
			func (s shortMapper) {{ $field.ResolverDeclaration }} {
				return s.r.{{$field.ShortInvocation}}
			}
		{{- end }}
	{{ end }}
{{- end }}

type executableSchema struct {
	resolvers      Resolvers
}

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

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

		buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
			data := ec._{{.QueryRoot.GQLType}}(ctx, op.Selections)
			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 *query.Operation) *graphql.Response {
	{{- if .MutationRoot }}
		ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers}

		buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
			data := ec._{{.MutationRoot.GQLType}}(ctx, op.Selections)
			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 *query.Operation) func() *graphql.Response {
	{{- if .SubscriptionRoot }}
		ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers}

		next := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.Selections)
		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()
			})

			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

	resolvers Resolvers
}

{{- 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) introspectSchema() *introspection.Schema {
	return introspection.WrapSchema(parsedSchema)
}

func (ec *executionContext) introspectType(name string) *introspection.Type {
	t := parsedSchema.Resolve(name)
	if t == nil {
		return nil
	}
	return introspection.WrapType(t)
}

var parsedSchema = schema.MustParse({{.SchemaRaw|rawQuote}})