-
Notifications
You must be signed in to change notification settings - Fork 48
/
template.go
383 lines (358 loc) · 12 KB
/
template.go
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main
var goTemplate = `
// Code generated by proroc-gen-graphql, DO NOT EDIT.
package {{ .RootPackage.Name }}
import (
{{- if .Services }}
"context"
"github.com/ysugimoto/grpc-graphql-gateway/runtime"
"google.golang.org/grpc"
"github.com/pkg/errors"
{{- end }}
"github.com/graphql-go/graphql"
{{- range .Packages }}
{{ if .Path }}{{ .Name }} "{{ .Path }}"{{ end }}
{{ end }}
)
var (
{{- range .Enums }}
gql__enum_{{ .Name }} *graphql.Enum // enum {{ .Name }} in {{ .Filename }}
{{- end }}
{{- range .Interfaces }}
gql__interface_{{ .Name }} *graphql.Interface // message {{ .Name }} in {{ .Filename }}
{{- end }}
{{- range .Types }}
gql__type_{{ .TypeName }} *graphql.Object // message {{ .Name }} in {{ .Filename }}
{{- end }}
{{- range .Inputs }}
gql__input_{{ .TypeName }} *graphql.InputObject // message {{ .Name }} in {{ .Filename }}
{{- end }}
)
{{ range $enum := .Enums -}}
func Gql__enum_{{ .Name }}() *graphql.Enum {
if gql__enum_{{ .Name }} == nil {
gql__enum_{{ .Name }} = graphql.NewEnum(graphql.EnumConfig{
Name: "{{ $.RootPackage.CamelName }}_Enum_{{ .Name }}",
Values: graphql.EnumValueConfigMap{
{{- range .Values }}
"{{ .Name }}": &graphql.EnumValueConfig{
{{- if .Comment }}
Description: ` + "`" + `{{ .Comment }}` + "`" + `,
{{- end }}
Value: {{ $enum.Name }}({{ .Number }}),
},
{{- end }}
},
})
}
return gql__enum_{{ .Name }}
}
{{ end }}
{{ range .Interfaces -}}
func Gql__interface_{{ .TypeName }}() *graphql.Interface {
if gql__interface_{{ .TypeName }} == nil {
gql__interface_{{ .TypeName }} = graphql.NewInterface(graphql.InterfaceConfig{
Name: "{{ $.RootPackage.CamelName }}_Interface_{{ .TypeName }}",
{{- if .Comment }}
Description: ` + "`" + `{{ .Comment }}` + "`" + `,
{{- end }}
Fields: graphql.Fields{
{{- range .Fields }}
{{- if not .IsCyclic }}
"{{ .FieldName }}": &graphql.Field{
Type: {{ .FieldType $.RootPackage.Name }},
{{- if .Comment }}
Description: ` + "`" + `{{ .Comment }}` + "`" + `,
{{- end }}
},
{{- end }}
{{- end }}
},
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
return Gql__type_{{ .TypeName }}()
},
})
}
return gql__interface_{{ .TypeName }}
}
{{ end }}
{{ range .Types -}}
func Gql__type_{{ .TypeName }}() *graphql.Object {
if gql__type_{{ .TypeName }} == nil {
gql__type_{{ .TypeName }} = graphql.NewObject(graphql.ObjectConfig{
Name: "{{ $.RootPackage.CamelName }}_Type_{{ .TypeName }}",
{{- if .Comment }}
Description: ` + "`" + `{{ .Comment }}` + "`" + `,
{{- end }}
Fields: graphql.Fields {
{{- range .Fields }}
{{- if .IsResolve }}
{{ $query := .ResolveSubField $.Services }}
"{{ .FieldName }}": &graphql.Field{
Type: {{ $query.QueryType }},
{{- if $query.Comment }}
Description: ` + "`" + `{{ $query.Comment }}` + "`" + `,
{{- end }}
Args: graphql.FieldConfigArgument{
{{- range $query.Args }}
"{{ .FieldName }}": &graphql.ArgumentConfig{
Type: {{ .FieldTypeInput $.RootPackage.Name }},
{{- if .Comment }}
Description: ` + "`" + `{{ .Comment }}` + "`" + `,
{{- end }}
{{- if .DefaultValue }}
DefaultValue: {{ .DefaultValue }},
{{- end }}
},
{{- end }}
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
var req {{ $query.InputType }}
if err := runtime.MarshalRequest(p.Source, &req, {{ if $query.IsCamel }}true{{ else }}false{{ end }}); err != nil {
return nil, errors.Wrap(err, "Failed to marshal resolver source for {{ $query.QueryName }}")
} else if err = runtime.MarshalRequest(p.Args, &req, {{ if $query.IsCamel }}true{{ else }}false{{ end }}); err != nil {
return nil, errors.Wrap(err, "Failed to marshal resolver request for {{ $query.QueryName }}")
}
{{ $s := index $.Services 0 }}
x := new_graphql_resolver_{{ $s.Name }}(nil)
conn, closer, err := x.CreateConnection(p.Context)
if err != nil {
return nil, errors.Wrap(err, "Failed to create gRPC connection for nested resolver")
}
defer closer()
client := {{ $query.Package }}New{{ $query.Method.Service.Name }}Client(conn)
resp, err := client.{{ $query.Method.Name }}(p.Context, &req)
if err != nil {
return nil, errors.Wrap(err, "Failed to call RPC {{ $query.Method.Name }}")
}
{{- if $query.IsPluckResponse }}
{{- if $query.IsCamel }}
return runtime.MarshalResponse(resp.Get{{ $query.PluckResponseFieldName }}()), nil
{{- else }}
return resp.Get{{ $query.PluckResponseFieldName }}(), nil
{{- end }}
{{- else }}
{{- if $query.IsCamel }}
return runtime.MarshalResponse(resp), nil
{{- else }}
return resp, nil
{{- end }}
{{- end }}
},
},
{{- else }}
"{{ .FieldName }}": &graphql.Field{
Type: {{ .FieldType $.RootPackage.Name }},
{{- if .Comment }}
Description: ` + "`" + `{{ .Comment }}` + "`" + `,
{{- end }}
},
{{- end }}
{{- end }}
},
{{- if .Interfaces }}
Interfaces: []*graphql.Interface{
{{- range .Interfaces }}
Gql__interface_{{ .TypeName }}(),
{{- end }}
},
{{- end }}
})
}
return gql__type_{{ .TypeName }}
}
{{ end }}
{{ range .Inputs -}}
func Gql__input_{{ .TypeName }}() *graphql.InputObject {
if gql__input_{{ .TypeName }} == nil {
gql__input_{{ .TypeName }} = graphql.NewInputObject(graphql.InputObjectConfig{
Name: "{{ $.RootPackage.CamelName }}_Input_{{ .TypeName }}",
Fields: graphql.InputObjectConfigFieldMap{
{{- range .Fields }}
"{{ .FieldName }}": &graphql.InputObjectFieldConfig{
{{- if .Comment }}
Description: ` + "`" + `{{ .Comment }}` + "`" + `,
{{- end }}
Type: {{ .FieldTypeInput $.RootPackage.Name }},
},
{{- end }}
},
})
}
return gql__input_{{ .TypeName }}
}
{{ end }}
{{ range $_, $service := .Services -}}
// graphql__resolver_{{ $service.Name }} is a struct for making query, mutation and resolve fields.
// This struct must be implemented runtime.SchemaBuilder interface.
type graphql__resolver_{{ $service.Name }} struct {
// Automatic connection host
host string
// grpc dial options
dialOptions []grpc.DialOption
// grpc client connection.
// this connection may be provided by user
conn *grpc.ClientConn
}
// new_graphql_resolver_{{ $service.Name }} creates pointer of service struct
func new_graphql_resolver_{{ $service.Name }}(conn *grpc.ClientConn) *graphql__resolver_{{ $service.Name }} {
return &graphql__resolver_{{ .Name }}{
conn: conn,
host: "{{ if .Host }}{{ .Host }}{{ else }}localhost:50051{{ end }}",
dialOptions: []grpc.DialOption{
{{- if .Insecure }}
grpc.WithInsecure(),
{{- end }}
},
}
}
// CreateConnection() returns grpc connection which user specified or newly connected and closing function
func (x *graphql__resolver_{{ $service.Name }}) CreateConnection(ctx context.Context) (*grpc.ClientConn, func(), error) {
// If x.conn is not nil, user injected their own connection
if x.conn != nil {
return x.conn, func() {}, nil
}
// Otherwise, this handler opens connection with specified host
conn, err := grpc.DialContext(ctx, x.host, x.dialOptions...)
if err != nil {
return nil, nil, err
}
return conn, func() { conn.Close() }, nil
}
// GetQueries returns acceptable graphql.Fields for Query.
func (x *graphql__resolver_{{ $service.Name }}) GetQueries(conn *grpc.ClientConn) graphql.Fields {
return graphql.Fields{
{{- range .Queries }}
{{- if not .IsResolver }}
"{{ .QueryName }}": &graphql.Field{
Type: {{ .QueryType }},
{{- if .Comment }}
Description: ` + "`" + `{{ .Comment }}` + "`" + `,
{{- end }}
Args: graphql.FieldConfigArgument{
{{- range .Args }}
"{{ .FieldName }}": &graphql.ArgumentConfig{
Type: {{ .FieldTypeInput $.RootPackage.Name }},
{{- if .Comment }}
Description: ` + "`" + `{{ .Comment }}` + "`" + `,
{{- end }}
{{- if .DefaultValue }}
DefaultValue: {{ .DefaultValue }},
{{- end }}
},
{{- end }}
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
var req {{ .InputType }}
if err := runtime.MarshalRequest(p.Args, &req, {{ if .IsCamel }}true{{ else }}false{{ end }}); err != nil {
return nil, errors.Wrap(err, "Failed to marshal request for {{ .QueryName }}")
}
client := New{{ .Method.Service.Name }}Client(conn)
resp, err := client.{{ .Method.Name }}(p.Context, &req)
if err != nil {
return nil, errors.Wrap(err, "Failed to call RPC {{ .Method.Name }}")
}
{{- if .IsPluckResponse }}
{{- if .IsCamel }}
return runtime.MarshalResponse(resp.Get{{ .PluckResponseFieldName }}()), nil
{{- else }}
return resp.Get{{ .PluckResponseFieldName }}(), nil
{{- end }}
{{- else }}
{{- if .IsCamel }}
return runtime.MarshalResponse(resp), nil
{{- else }}
return resp, nil
{{- end }}
{{- end }}
},
},
{{- end }}
{{- end }}
}
}
// GetMutations returns acceptable graphql.Fields for Mutation.
func (x *graphql__resolver_{{ $service.Name }}) GetMutations(conn *grpc.ClientConn) graphql.Fields {
return graphql.Fields{
{{- range .Mutations }}
"{{ .MutationName }}": &graphql.Field{
Type: {{ .MutationType }},
{{- if .Comment }}
Description: ` + "`" + `{{ .Comment }}` + "`" + `,
{{ end }}
Args: graphql.FieldConfigArgument{
{{- if .InputName }}
"{{ .InputName }}": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(Gql__input_{{ .Input.TypeName }}()),
},
{{- else }}
{{- range .Args }}
"{{ .FieldName }}": &graphql.ArgumentConfig{
Type: {{ .FieldTypeInput $.RootPackage.Name }},
{{- if .Comment }}
Description: ` + "`" + `{{ .Comment }}` + "`" + `,
{{- end }}
{{- if .DefaultValue }}
DefaultValue: {{ .DefaultValue }},
{{- end }}
},
{{- end }}
{{- end }}
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
var req {{ .InputType }}
{{- if .InputName }}
if err := runtime.MarshalRequest(p.Args["{{ .InputName }}"], &req, {{ if .IsCamel }}true{{ else }}false{{ end }}); err != nil {
{{- else }}
if err := runtime.MarshalRequest(p.Args, &req, {{ if .IsCamel }}true{{ else }}false{{ end }}); err != nil {
{{- end }}
return nil, errors.Wrap(err, "Failed to marshal request for {{ .MutationName }}")
}
client := New{{ $service.Name }}Client(conn)
resp, err := client.{{ .Method.Name }}(p.Context, &req)
if err != nil {
return nil, errors.Wrap(err, "Failed to call RPC {{ .Method.Name }}")
}
{{- if .IsPluckResponse }}
{{- if .IsCamel }}
return runtime.MarshalResponse(resp.Get{{ .PluckResponseFieldName }}()), nil
{{- else }}
return resp.Get{{ .PluckResponseFieldName }}(), nil
{{- end }}
{{- else }}
{{- if .IsCamel }}
return runtime.MarshalResponse(resp), nil
{{- else }}
return resp, nil
{{- end }}
{{- end }}
},
},
{{ end }}
}
}
// Register package divided graphql handler "without" *grpc.ClientConn,
// therefore gRPC connection will be opened and closed automatically.
// Occasionally you may worry about open/close performance for each handling graphql request,
// then you can call Register{{ .Name }}GraphqlHandler with *grpc.ClientConn manually.
func Register{{ .Name }}Graphql(mux *runtime.ServeMux) error {
return Register{{ .Name }}GraphqlHandler(mux, nil)
}
// Register package divided graphql handler "with" *grpc.ClientConn.
// this function accepts your defined grpc connection, so that we reuse that and never close connection inside.
// You need to close it maunally when application will terminate.
// Otherwise, you can specify automatic opening connection with ServiceOption directive:
//
// service {{ .Name }} {
// option (graphql.service) = {
// host: "host:port"
// insecure: true or false
// };
//
// ...with RPC definitions
// }
func Register{{ .Name }}GraphqlHandler(mux *runtime.ServeMux, conn *grpc.ClientConn) error {
return mux.AddHandler(new_graphql_resolver_{{ .Name }}(conn))
}
{{ end }}
`