/
annotation.go
565 lines (517 loc) · 15.3 KB
/
annotation.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// Copyright 2019-present Facebook
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package entgql
import (
"encoding/json"
"entgo.io/ent/entc/gen"
"entgo.io/ent/schema"
"github.com/vektah/gqlparser/v2/ast"
)
type (
// Annotation annotates fields and edges with metadata for templates.
Annotation struct {
// OrderField is the ordering field as defined in graphql schema.
OrderField string `json:"OrderField,omitempty"`
// MultiOrder indicates that orderBy should accept a list of OrderField terms.
MultiOrder bool `json:"MultiOrder,omitempty"`
// Unbind implies the edge field name in GraphQL schema is not equivalent
// to the name used in ent schema. That means, by default, edges with this
// annotation will not be eager-loaded on Paginate calls. See the `MapsTo`
// option in order to load edges be different name mapping.
Unbind bool `json:"Unbind,omitempty"`
// Mapping is the edge field names as defined in graphql schema.
Mapping []string `json:"Mapping,omitempty"`
// Type is the underlying GraphQL type name (e.g. Boolean).
Type string `json:"Type,omitempty"`
// Skip exclude the type
Skip SkipMode `json:"Skip,omitempty"`
// RelayConnection enables the Relay Connection specification for the entity.
// It's also can apply on an edge to create the Relay-style filter.
RelayConnection bool `json:"RelayConnection,omitempty"`
// Implements defines a list of interfaces implemented by the type.
Implements []string `json:"Implements,omitempty"`
// Directives to add on the field/type.
Directives []Directive `json:"Directives,omitempty"`
// QueryField exposes the generated type with the given string under the Query object.
QueryField *FieldConfig `json:"QueryField,omitempty"`
// MutationInputs defines the input types for the mutation.
MutationInputs []MutationConfig `json:"MutationInputs,omitempty"`
}
// Directive to apply on the field/type.
Directive struct {
Name string `json:"name,omitempty"`
Arguments []*ast.Argument `json:"arguments,omitempty"`
}
// SkipMode is a bit flag for the Skip annotation.
SkipMode int
FieldConfig struct {
// Name is the name of the field in the Query object.
Name string `json:"Name,omitempty"`
// Description is the description of the field.
Description string `json:"Description,omitempty"`
// Directives to add on the field
Directives []Directive `json:"Directives,omitempty"`
}
// MutationConfig hold config for mutation
MutationConfig struct {
IsCreate bool `json:"IsCreate,omitempty"`
Description string `json:"Description,omitempty"`
}
)
const (
// SkipType skips generating GraphQL types or fields in the schema.
SkipType SkipMode = 1 << iota
// SkipEnumField skips generating GraphQL enums for enum fields in the schema.
SkipEnumField
// SkipOrderField skips generating GraphQL order inputs and enums for ordered-fields in the schema.
SkipOrderField
// SkipWhereInput skips generating GraphQL WhereInput types.
// If defined on a field, the type will be generated without the field.
SkipWhereInput
// SkipMutationCreateInput skips generating GraphQL Create<Type>Input types.
// If defined on a field, the type will be generated without the field.
SkipMutationCreateInput
// SkipMutationUpdateInput skips generating GraphQL Update<Type>Input types.
// If defined on a field, the type will be generated without the field.
SkipMutationUpdateInput
// SkipAll is default mode to skip all.
SkipAll = SkipType |
SkipEnumField |
SkipOrderField |
SkipWhereInput |
SkipMutationCreateInput |
SkipMutationUpdateInput
)
// Name implements ent.Annotation interface.
func (Annotation) Name() string {
return "EntGQL"
}
// OrderField enables ordering in GraphQL for the annotated Ent field
// with the given name. Note that, the field type must be comparable.
//
// field.Time("created_at").
// Default(time.Now).
// Immutable().
// Annotations(
// entgql.OrderField("CREATED_AT"),
// )
//
// For enum fields, values must be uppercase or mapped using the NamedValues
// option:
//
// field.Enum("status").
// NamedValues(
// "InProgress", "IN_PROGRESS",
// "Completed", "COMPLETED",
// ).
// Default("IN_PROGRESS").
// Annotations(
// entgql.OrderField("STATUS"),
// )
func OrderField(name string) Annotation {
return Annotation{OrderField: name}
}
// MultiOrder indicates that orderBy should accept a list of OrderField terms.
func MultiOrder() Annotation {
return Annotation{MultiOrder: true}
}
// Bind returns a binding annotation.
//
// No-op function to avoid breaking the existing schema.
// You can safely remove this function from your scheme.
//
// Deprecated: the Bind option predates the Unbind option, and it is planned
// to be removed in future versions. Users should not use this annotation as it
// is a no-op call, or use `Unbind` in order to disable `Bind`.
func Bind() Annotation {
return Annotation{}
}
// Unbind implies the edge field name in GraphQL schema is not equivalent
// to the name used in ent schema. That means, by default, edges with this
// annotation will not be eager-loaded on Paginate calls. See the `MapsTo`
// option in order to load edges with different name mapping.
//
// func (Todo) Edges() []ent.Edge {
// return []ent.Edge{
// edge.To("parent", Todo.Type).
// Annotations(entgql.Unbind()).
// Unique().
// From("children").
// Annotations(entgql.Unbind()),
// }
// }
func Unbind() Annotation {
return Annotation{Unbind: true}
}
// MapsTo returns a mapping annotation.
func MapsTo(names ...string) Annotation {
return Annotation{
Mapping: names,
Unbind: true, // Unbind because it cant be used with mapping names.
}
}
// Type returns a type mapping annotation.
// The Type() annotation is used to map the underlying
// GraphQL type to the type.
//
// To change the GraphQL type for a type:
//
// func (User) Annotations() []schema.Annotation {
// return []schema.Annotation{
// entgql.Type("MasterUser"),
// }
// }
//
// To change the GraphQL type for a field (rename enum type):
//
// field.Enum("status").
// NamedValues(
// "InProgress", "IN_PROGRESS",
// "Completed", "COMPLETED",
// ).
// Default("IN_PROGRESS").
// Annotations(
// entgql.Type("TodoStatus"),
// )
func Type(name string) Annotation {
return Annotation{Type: name}
}
// Skip returns a skip annotation.
// The Skip() annotation is used to skip
// generating the type or the field from GraphQL schema.
//
// It gives you the flexibility to skip generating
// the type or the field based on the SkipMode flags.
//
// For example, if you don't want to expose a field on the <T>WhereInput type
// you can use the following:
//
// field.String("name").
// Annotations(
// entgql.Skip(entgql.SkipWhereInput),
// )
//
// Since SkipMode is a bit flag, it's possible to skip multiple modes using
// the bitwise OR operator as follows:
//
// entgql.Skip(entgql.SkipWhereInput | entgql.SkipEnumField)
//
// To skip everything except the type, use the bitwise NOT operator:
//
// entgql.Skip(^entgql.SkipType)
//
// You can also skip all modes with the `entgql.SkipAll` constant which is the default mode.
func Skip(flags ...SkipMode) Annotation {
if len(flags) == 0 {
return Annotation{Skip: SkipAll}
}
skip := SkipMode(0)
for _, f := range flags {
skip |= f
}
return Annotation{Skip: skip}
}
// RelayConnection returns an annotation indicating that the node/edge should support pagination.
// Hence,the returned result is a Relay connection rather than a list of nodes.
//
// Setting this annotation on schema `T` (reside in ent/schema), enables pagination for this
// type and therefore, Ent will generate all Relay types for this schema, such as: `<T>Edge`,
// `<T>Connection`, and PageInfo. For example:
//
// func (Todo) Annotations() []schema.Annotation {
// return []schema.Annotation{
// entgql.RelayConnection(),
// entgql.QueryField(),
// }
// }
//
// Setting this annotation on an Ent edge indicates that the GraphQL field for this edge
// should support nested pagination and the returned type is a Relay connection type rather
// than the actual nodes. For example:
//
// func (Todo) Edges() []ent.Edge {
// return []ent.Edge{
// edge.To("parent", Todo.Type).
// Unique().
// From("children").
// Annotation(entgql.RelayConnection()),
// }
// }
//
// The generated GraphQL schema will be:
//
// children(first: Int, last: Int, after: Cursor, before: Cursor): TodoConnection!
//
// Rather than:
//
// children: [Todo!]!
func RelayConnection() Annotation {
return Annotation{RelayConnection: true}
}
// Implements returns an Implements annotation.
// The Implements() annotation is used to
// add implements interfaces to a GraphQL type.
//
// For example, to add the `Entity` to the `Todo` type:
//
// type Todo implements Node {
// id: ID!
// ...
// }
//
// Add the entgql.Implements("Entity") to the Todo's annotations:
//
// func (Todo) Annotations() []schema.Annotation {
// return []schema.Annotation{
// entgql.Implements("Entity"),
// }
// }
//
// and the GraphQL type will be generated with the implements interface.
//
// type Todo implements Node & Entity {
// id: ID!
// ...
// }
func Implements(interfaces ...string) Annotation {
return Annotation{Implements: interfaces}
}
// Directives returns a Directives annotation.
// The Directives() annotation is used to
// add directives to a GraphQL type or on the field.
//
// For example, to add the `@deprecated` directive to the `text` field:
//
// type Todo {
// id: ID
// text: String
// ...
// }
//
// Add the entgql.Directives() to the text field's annotations:
//
// field.Text("text").
// NotEmpty().
// Annotations(
// entgql.Directives(entgql.Deprecated("Use `description` instead.")),
// ),
//
// and the GraphQL type will be generated with the directive.
//
// type Todo {
// id: ID
// text: String @deprecated(reason: "Use `description` instead.")
// ...
// }
func Directives(directives ...Directive) Annotation {
return Annotation{Directives: directives}
}
type queryFieldAnnotation struct {
Annotation
}
// QueryField returns an annotation for expose the field on the Query type.
func QueryField(name ...string) queryFieldAnnotation {
a := Annotation{QueryField: &FieldConfig{}}
if len(name) > 0 {
a.QueryField.Name = name[0]
}
return queryFieldAnnotation{Annotation: a}
}
// Directives allows you to apply directives to the field.
func (a queryFieldAnnotation) Directives(directives ...Directive) queryFieldAnnotation {
a.QueryField.Directives = directives
return a
}
// Description allows you to set the description for the field.
func (a queryFieldAnnotation) Description(text string) queryFieldAnnotation {
a.QueryField.Description = text
return a
}
type MutationOption interface {
IsCreate() bool
GetDescription() string
// Description allows you to customize the comment of the auto-generated Mutation Input
//
// For example,
//
// entgql.Mutations(
// entgql.MutationCreate().
// Description("The fields used in the creation of a TodoItem"),
// ),
//
// Creates
//
// """The fields used in the creation of a TodoItem"""
// input CreateTodoItem {
// """fields omitted"""
// }
Description(string) MutationOption
}
type builtinMutation struct {
description string
isCreate bool
}
func (v builtinMutation) IsCreate() bool { return v.isCreate }
func (v builtinMutation) GetDescription() string { return v.description }
func (v builtinMutation) Description(desc string) MutationOption {
v.description = desc
return v
}
func MutationCreate() MutationOption {
return builtinMutation{isCreate: true}
}
func MutationUpdate() MutationOption {
return builtinMutation{isCreate: false}
}
// Mutations returns an annotation for generate input types for mutation.
func Mutations(inputs ...MutationOption) Annotation {
if len(inputs) == 0 {
inputs = []MutationOption{MutationCreate(), MutationUpdate()}
}
a := []MutationConfig{}
for _, f := range inputs {
a = append(a, MutationConfig{
IsCreate: f.IsCreate(),
Description: f.GetDescription(),
})
}
return Annotation{MutationInputs: a}
}
// Merge implements the schema.Merger interface.
func (a Annotation) Merge(other schema.Annotation) schema.Annotation {
var ant Annotation
switch other := other.(type) {
case Annotation:
ant = other
case *Annotation:
if other != nil {
ant = *other
}
case queryFieldAnnotation:
ant = other.Annotation
case *queryFieldAnnotation:
if other != nil {
ant = other.Annotation
}
default:
return a
}
if ant.OrderField != "" {
a.OrderField = ant.OrderField
}
if ant.MultiOrder {
a.MultiOrder = true
}
if ant.Unbind {
a.Unbind = true
}
if len(ant.Mapping) != 0 {
a.Mapping = ant.Mapping
}
if ant.Type != "" {
a.Type = ant.Type
}
if ant.Skip.Any() {
a.Skip |= ant.Skip
}
if len(ant.MutationInputs) > 0 {
a.MutationInputs = append(a.MutationInputs, ant.MutationInputs...)
}
if ant.RelayConnection {
a.RelayConnection = true
}
if len(ant.Implements) > 0 {
a.Implements = append(a.Implements, ant.Implements...)
}
if len(ant.Directives) > 0 {
a.Directives = append(a.Directives, ant.Directives...)
}
if ant.QueryField != nil {
if a.QueryField == nil {
a.QueryField = &FieldConfig{}
}
a.QueryField.merge(ant.QueryField)
}
return a
}
// Decode unmarshalls the annotation.
func (a *Annotation) Decode(annotation interface{}) error {
buf, err := json.Marshal(annotation)
if err != nil {
return err
}
return json.Unmarshal(buf, a)
}
// Any returns true if the skip annotation was set.
func (f SkipMode) Any() bool {
return f != 0
}
// Is checks if the skip annotation has a specific flag.
func (f SkipMode) Is(mode SkipMode) bool {
return f&mode != 0
}
func (c FieldConfig) fieldName(gqlType string) string {
if c.Name != "" {
return c.Name
}
return camel(snake(plural(gqlType)))
}
func (c *FieldConfig) merge(ant *FieldConfig) {
if ant == nil {
return
}
if ant.Name != "" {
c.Name = ant.Name
}
if ant.Description != "" {
c.Description = ant.Description
}
c.Directives = append(c.Directives, ant.Directives...)
}
// annotation extracts the entgql.Annotation or returns its empty value.
func annotation(ants gen.Annotations) (*Annotation, error) {
ant := &Annotation{}
if ants != nil && ants[ant.Name()] != nil {
if err := ant.Decode(ants[ant.Name()]); err != nil {
return nil, err
}
}
return ant, nil
}
var (
_ schema.Annotation = (*Annotation)(nil)
_ schema.Merger = (*Annotation)(nil)
)
// NewDirective returns a GraphQL directive
// to use with the entgql.Directives annotation.
func NewDirective(name string, args ...*ast.Argument) Directive {
return Directive{
Name: name,
Arguments: args,
}
}
// Deprecated create `@deprecated` directive to apply on the field/type
func Deprecated(reason string) Directive {
var args []*ast.Argument
if reason != "" {
args = append(args, &ast.Argument{
Name: "reason",
Value: &ast.Value{
Raw: reason,
Kind: ast.StringValue,
},
})
}
return NewDirective("deprecated", args...)
}