-
Notifications
You must be signed in to change notification settings - Fork 0
/
directives.go
231 lines (188 loc) · 5.34 KB
/
directives.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
package gql
import (
"context"
"github.com/rigglo/gql/pkg/language/ast"
)
type Directives []Directive
type Directive interface {
GetName() string
GetDescription() string
GetArguments() Arguments
GetLocations() []DirectiveLocation
}
/*
type ExecutableDirective interface {
// any Executable Directive specific function comes here
}
*/
type TypeSystemDirective interface {
Directive
GetValues() map[string]interface{}
}
func (ds TypeSystemDirectives) ast() []*ast.Directive {
out := []*ast.Directive{}
for _, d := range ds {
od := ast.Directive{
Name: d.GetName(),
Arguments: make([]*ast.Argument, 0),
}
for an, a := range d.GetArguments() {
od.Arguments = append(od.Arguments, &ast.Argument{
Name: an,
Value: toAstValue(a.Type, d.GetValues()[an]),
})
}
out = append(out, &od)
}
return out
}
type TypeSystemDirectives []TypeSystemDirective
type DirectiveLocation string
const (
// Executable directive locations
QueryLoc DirectiveLocation = "QUERY"
MutationLoc DirectiveLocation = "MUTATION"
SubscriptionLoc DirectiveLocation = "SUBSCRIPTION"
FieldLoc DirectiveLocation = "FIELD"
FragmentDefinitionLoc DirectiveLocation = "FRAGMENT_DEFINITION"
FragmentSpreadLoc DirectiveLocation = "FRAGMENT_SPREAD"
InlineFragmentLoc DirectiveLocation = "INLINE_FRAGMENT"
// Type System directive locations
SchemaLoc DirectiveLocation = "SCHEMA"
ScalarLoc DirectiveLocation = "SCALAR"
ObjectLoc DirectiveLocation = "OBJECT"
FieldDefinitionLoc DirectiveLocation = "FIELD_DEFINITION"
ArgumentDefinitionLoc DirectiveLocation = "ARGUMENT_DEFINITION"
InterfaceLoc DirectiveLocation = "INTERFACE"
UnionLoc DirectiveLocation = "UNION"
EnumLoc DirectiveLocation = "ENUM"
EnumValueLoc DirectiveLocation = "ENUM_VALUE"
InputObjectLoc DirectiveLocation = "INPUT_OBJECT"
InputFieldDefinitionLoc DirectiveLocation = "INPUT_FIELD_DEFINITION"
)
type SchemaDirective interface {
VisitSchema(context.Context, Schema) *Schema
}
type ScalarDirective interface {
VisitScalar(context.Context, Scalar) *Scalar
}
type ObjectDirective interface {
VisitObject(context.Context, Object) *Object
}
type FieldDefinitionDirective interface {
VisitFieldDefinition(context.Context, Field, Resolver) Resolver
}
type ArgumentDirective interface {
VisitArgument(context.Context, Argument)
}
type InterfaceDirective interface {
VisitInterface(context.Context, Interface) *Interface
}
type UnionDirective interface {
VisitUnion(context.Context, Union) *Union
}
type EnumDirective interface {
VisitEnum(context.Context, Enum) *Enum
}
type EnumValueDirective interface {
VisitEnumValue(context.Context, EnumValue) *EnumValue
}
type InputObjectDirective interface {
VisitInputObject(context.Context, InputObject) *InputObject
}
type InputFieldDirective interface {
VisitInputField(context.Context, InputField) *InputField
}
type skip struct{}
func (s *skip) GetName() string {
return "skip"
}
func (s *skip) GetDescription() string {
return "The @skip directive may be provided for fields, fragment spreads, and inline fragments, and allows for conditional exclusion during execution as described by the if argument"
}
func (s *skip) GetArguments() Arguments {
return Arguments{
"if": &Argument{
Type: NewNonNull(Boolean),
},
}
}
func (s *skip) GetLocations() []DirectiveLocation {
return []DirectiveLocation{
FieldLoc,
FragmentSpreadLoc,
InlineFragmentLoc,
}
}
func (s *skip) Skip(args []*ast.Argument) bool {
if args[0].Value.GetValue().(string) == "true" {
return true
}
return false
}
type include struct{}
func (s *include) GetName() string {
return "include"
}
func (s *include) GetDescription() string {
return "The @skip directive may be provided for fields, fragment spreads, and inline fragments, and allows for conditional exclusion during execution as described by the if argument"
}
func (s *include) GetArguments() Arguments {
return Arguments{
"if": &Argument{
Type: NewNonNull(Boolean),
},
}
}
func (s *include) GetLocations() []DirectiveLocation {
return []DirectiveLocation{
FieldLoc,
FragmentSpreadLoc,
InlineFragmentLoc,
}
}
func (s *include) Include(args []*ast.Argument) bool {
if args[0].Value.GetValue().(string) == "true" {
return true
}
return false
}
func Deprecate(reason string) TypeSystemDirective {
return &deprecated{reason}
}
type deprecated struct {
reason string
}
func (d *deprecated) GetName() string {
return "deprecated"
}
func (d *deprecated) GetDescription() string {
return "The `@deprecated` directive is used within the type system definition language to indicate deprecated portions of a GraphQL service’s schema, such as deprecated fields on a type or deprecated enum values"
}
func (d *deprecated) GetArguments() Arguments {
return Arguments{
"reason": &Argument{
Type: String,
DefaultValue: "",
},
}
}
func (d *deprecated) GetLocations() []DirectiveLocation {
return []DirectiveLocation{
FieldDefinitionLoc,
EnumValueLoc,
}
}
func (d *deprecated) Reason() string {
return d.reason
}
func (d *deprecated) GetValues() map[string]interface{} {
return map[string]interface{}{
"reason": d.reason,
}
}
var (
skipDirective = &skip{}
includeDirective = &include{}
deprecatedDirective = &deprecated{}
)