-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathDirectives.java
More file actions
333 lines (301 loc) · 16.8 KB
/
Directives.java
File metadata and controls
333 lines (301 loc) · 16.8 KB
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
package graphql;
import graphql.language.BooleanValue;
import graphql.language.Description;
import graphql.language.DirectiveDefinition;
import graphql.language.StringValue;
import graphql.schema.GraphQLDirective;
import org.jspecify.annotations.NullMarked;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import static graphql.Scalars.GraphQLBoolean;
import static graphql.Scalars.GraphQLString;
import static graphql.introspection.Introspection.DirectiveLocation.ARGUMENT_DEFINITION;
import static graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE;
import static graphql.introspection.Introspection.DirectiveLocation.FIELD;
import static graphql.introspection.Introspection.DirectiveLocation.FIELD_DEFINITION;
import static graphql.introspection.Introspection.DirectiveLocation.FRAGMENT_SPREAD;
import static graphql.introspection.Introspection.DirectiveLocation.INLINE_FRAGMENT;
import static graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION;
import static graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT;
import static graphql.introspection.Introspection.DirectiveLocation.MUTATION;
import static graphql.introspection.Introspection.DirectiveLocation.QUERY;
import static graphql.introspection.Introspection.DirectiveLocation.SCALAR;
import static graphql.introspection.Introspection.DirectiveLocation.SUBSCRIPTION;
import static graphql.language.DirectiveLocation.newDirectiveLocation;
import static graphql.language.InputValueDefinition.newInputValueDefinition;
import static graphql.language.NonNullType.newNonNullType;
import static graphql.language.TypeName.newTypeName;
import static graphql.schema.GraphQLArgument.newArgument;
import static graphql.schema.GraphQLNonNull.nonNull;
/**
* The directives that are understood by graphql-java
*/
@PublicApi
@NullMarked
public class Directives {
private static final String DEPRECATED = "deprecated";
private static final String INCLUDE = "include";
private static final String SKIP = "skip";
private static final String SPECIFIED_BY = "specifiedBy";
private static final String ONE_OF = "oneOf";
private static final String DEFER = "defer";
private static final String EXPERIMENTAL_DISABLE_ERROR_PROPAGATION = "experimental_disableErrorPropagation";
public static final DirectiveDefinition DEPRECATED_DIRECTIVE_DEFINITION;
public static final DirectiveDefinition INCLUDE_DIRECTIVE_DEFINITION;
public static final DirectiveDefinition SKIP_DIRECTIVE_DEFINITION;
public static final DirectiveDefinition SPECIFIED_BY_DIRECTIVE_DEFINITION;
@ExperimentalApi
public static final DirectiveDefinition ONE_OF_DIRECTIVE_DEFINITION;
@ExperimentalApi
public static final DirectiveDefinition DEFER_DIRECTIVE_DEFINITION;
@ExperimentalApi
public static final DirectiveDefinition EXPERIMENTAL_DISABLE_ERROR_PROPAGATION_DIRECTIVE_DEFINITION;
public static final String BOOLEAN = "Boolean";
public static final String STRING = "String";
public static final String NO_LONGER_SUPPORTED = "No longer supported";
static {
DEPRECATED_DIRECTIVE_DEFINITION = DirectiveDefinition.newDirectiveDefinition()
.name(DEPRECATED)
.directiveLocation(newDirectiveLocation().name(FIELD_DEFINITION.name()).build())
.directiveLocation(newDirectiveLocation().name(ENUM_VALUE.name()).build())
.directiveLocation(newDirectiveLocation().name(ARGUMENT_DEFINITION.name()).build())
.directiveLocation(newDirectiveLocation().name(INPUT_FIELD_DEFINITION.name()).build())
.description(createDescription("Marks the field, argument, input field or enum value as deprecated"))
.inputValueDefinition(
newInputValueDefinition()
.name("reason")
.description(createDescription("The reason for the deprecation"))
.type(newNonNullType(newTypeName().name(STRING).build()).build())
.defaultValue(StringValue.newStringValue().value(NO_LONGER_SUPPORTED).build())
.build())
.build();
INCLUDE_DIRECTIVE_DEFINITION = DirectiveDefinition.newDirectiveDefinition()
.name(INCLUDE)
.directiveLocation(newDirectiveLocation().name(FRAGMENT_SPREAD.name()).build())
.directiveLocation(newDirectiveLocation().name(INLINE_FRAGMENT.name()).build())
.directiveLocation(newDirectiveLocation().name(FIELD.name()).build())
.description(createDescription("Directs the executor to include this field or fragment only when the `if` argument is true"))
.inputValueDefinition(
newInputValueDefinition()
.name("if")
.description(createDescription("Included when true."))
.type(newNonNullType(newTypeName().name(BOOLEAN).build()).build())
.build())
.build();
SKIP_DIRECTIVE_DEFINITION = DirectiveDefinition.newDirectiveDefinition()
.name(SKIP)
.directiveLocation(newDirectiveLocation().name(FRAGMENT_SPREAD.name()).build())
.directiveLocation(newDirectiveLocation().name(INLINE_FRAGMENT.name()).build())
.directiveLocation(newDirectiveLocation().name(FIELD.name()).build())
.description(createDescription("Directs the executor to skip this field or fragment when the `if` argument is true."))
.inputValueDefinition(
newInputValueDefinition()
.name("if")
.description(createDescription("Skipped when true."))
.type(newNonNullType(newTypeName().name(BOOLEAN).build()).build())
.build())
.build();
SPECIFIED_BY_DIRECTIVE_DEFINITION = DirectiveDefinition.newDirectiveDefinition()
.name(SPECIFIED_BY)
.directiveLocation(newDirectiveLocation().name(SCALAR.name()).build())
.description(createDescription("Exposes a URL that specifies the behaviour of this scalar."))
.inputValueDefinition(
newInputValueDefinition()
.name("url")
.description(createDescription("The URL that specifies the behaviour of this scalar."))
.type(newNonNullType(newTypeName().name(STRING).build()).build())
.build())
.build();
ONE_OF_DIRECTIVE_DEFINITION = DirectiveDefinition.newDirectiveDefinition()
.name(ONE_OF)
.directiveLocation(newDirectiveLocation().name(INPUT_OBJECT.name()).build())
.description(createDescription("Indicates an Input Object is a OneOf Input Object."))
.build();
DEFER_DIRECTIVE_DEFINITION = DirectiveDefinition.newDirectiveDefinition()
.name(DEFER)
.directiveLocation(newDirectiveLocation().name(FRAGMENT_SPREAD.name()).build())
.directiveLocation(newDirectiveLocation().name(INLINE_FRAGMENT.name()).build())
.description(createDescription("This directive allows results to be deferred during execution"))
.inputValueDefinition(
newInputValueDefinition()
.name("if")
.description(createDescription("Deferred behaviour is controlled by this argument"))
.type(newNonNullType(newTypeName().name(BOOLEAN).build()).build())
.defaultValue(BooleanValue.newBooleanValue(true).build())
.build())
.inputValueDefinition(
newInputValueDefinition()
.name("label")
.description(createDescription("A unique label that represents the fragment being deferred"))
.type(newTypeName().name(STRING).build())
.build())
.build();
EXPERIMENTAL_DISABLE_ERROR_PROPAGATION_DIRECTIVE_DEFINITION = DirectiveDefinition.newDirectiveDefinition()
.name(EXPERIMENTAL_DISABLE_ERROR_PROPAGATION)
.directiveLocation(newDirectiveLocation().name(QUERY.name()).build())
.directiveLocation(newDirectiveLocation().name(MUTATION.name()).build())
.directiveLocation(newDirectiveLocation().name(SUBSCRIPTION.name()).build())
.description(createDescription("This directive allows returning null in non-null positions that have an associated error"))
.build();
}
/**
* The @defer directive can be used to defer sending data for a fragment until later in the query.
* This is an opt-in directive that is not available unless it is explicitly put into the schema.
* <p>
* This implementation is based on the state of <a href="https://github.com/graphql/graphql-spec/pull/742">Defer/Stream PR</a>
* More specifically at the state of this
* <a href="https://github.com/graphql/graphql-spec/commit/c630301560d9819d33255d3ba00f548e8abbcdc6">commit</a>
* <p>
* The execution behaviour should match what we get from running Apollo Server 4.9.5 with graphql-js v17.0.0-alpha.2
*/
@ExperimentalApi
public static final GraphQLDirective DeferDirective = GraphQLDirective.newDirective()
.name(DEFER)
.description("This directive allows results to be deferred during execution")
.validLocations(FRAGMENT_SPREAD, INLINE_FRAGMENT)
.argument(newArgument()
.name("if")
.type(nonNull(GraphQLBoolean))
.description("Deferred behaviour is controlled by this argument")
.defaultValueLiteral(BooleanValue.newBooleanValue(true).build())
)
.argument(newArgument()
.name("label")
.type(GraphQLString)
.description("A unique label that represents the fragment being deferred")
)
.definition(DEFER_DIRECTIVE_DEFINITION)
.build();
public static final GraphQLDirective IncludeDirective = GraphQLDirective.newDirective()
.name(INCLUDE)
.description("Directs the executor to include this field or fragment only when the `if` argument is true")
.argument(newArgument()
.name("if")
.type(nonNull(GraphQLBoolean))
.description("Included when true."))
.validLocations(FRAGMENT_SPREAD, INLINE_FRAGMENT, FIELD)
.definition(INCLUDE_DIRECTIVE_DEFINITION)
.build();
public static final GraphQLDirective SkipDirective = GraphQLDirective.newDirective()
.name(SKIP)
.description("Directs the executor to skip this field or fragment when the `if` argument is true.")
.argument(newArgument()
.name("if")
.type(nonNull(GraphQLBoolean))
.description("Skipped when true."))
.validLocations(FRAGMENT_SPREAD, INLINE_FRAGMENT, FIELD)
.definition(SKIP_DIRECTIVE_DEFINITION)
.build();
/**
* The "deprecated" directive is special and is always available in a graphql schema
* <p>
* See <a href="https://spec.graphql.org/draft/#sec--deprecated">the GraphQL specification for @deprecated</a>
*/
public static final GraphQLDirective DeprecatedDirective = GraphQLDirective.newDirective()
.name(DEPRECATED)
.description("Marks the field, argument, input field or enum value as deprecated")
.argument(newArgument()
.name("reason")
.type(nonNull(GraphQLString))
.defaultValueProgrammatic(NO_LONGER_SUPPORTED)
.description("The reason for the deprecation"))
.validLocations(FIELD_DEFINITION, ENUM_VALUE, ARGUMENT_DEFINITION, INPUT_FIELD_DEFINITION)
.definition(DEPRECATED_DIRECTIVE_DEFINITION)
.build();
/**
* The "specifiedBy" directive allows to provide a specification URL for a Scalar
*/
public static final GraphQLDirective SpecifiedByDirective = GraphQLDirective.newDirective()
.name(SPECIFIED_BY)
.description("Exposes a URL that specifies the behaviour of this scalar.")
.argument(newArgument()
.name("url")
.type(nonNull(GraphQLString))
.description("The URL that specifies the behaviour of this scalar."))
.validLocations(SCALAR)
.definition(SPECIFIED_BY_DIRECTIVE_DEFINITION)
.build();
@ExperimentalApi
public static final GraphQLDirective OneOfDirective = GraphQLDirective.newDirective()
.name(ONE_OF)
.description("Indicates an Input Object is a OneOf Input Object.")
.validLocations(INPUT_OBJECT)
.definition(ONE_OF_DIRECTIVE_DEFINITION)
.build();
@ExperimentalApi
public static final GraphQLDirective ExperimentalDisableErrorPropagationDirective = GraphQLDirective.newDirective()
.name(EXPERIMENTAL_DISABLE_ERROR_PROPAGATION)
.description("This directive disables error propagation when a non nullable field returns null for the given operation.")
.validLocations(QUERY, MUTATION, SUBSCRIPTION)
.definition(EXPERIMENTAL_DISABLE_ERROR_PROPAGATION_DIRECTIVE_DEFINITION)
.build();
/**
* The set of all built-in directives that are always present in a graphql schema.
* The iteration order is stable and meaningful.
*/
public static final Set<GraphQLDirective> BUILT_IN_DIRECTIVES;
/**
* A map from directive name to directive for all built-in directives.
*/
public static final Map<String, GraphQLDirective> BUILT_IN_DIRECTIVES_MAP;
static {
LinkedHashSet<GraphQLDirective> directives = new LinkedHashSet<>();
directives.add(IncludeDirective);
directives.add(SkipDirective);
directives.add(DeprecatedDirective);
directives.add(SpecifiedByDirective);
directives.add(OneOfDirective);
directives.add(DeferDirective);
directives.add(ExperimentalDisableErrorPropagationDirective);
BUILT_IN_DIRECTIVES = Collections.unmodifiableSet(directives);
LinkedHashMap<String, GraphQLDirective> map = new LinkedHashMap<>();
for (GraphQLDirective d : BUILT_IN_DIRECTIVES) {
map.put(d.getName(), d);
}
BUILT_IN_DIRECTIVES_MAP = Collections.unmodifiableMap(map);
}
/**
* Returns true if a directive with the provided name is a built-in directive.
*
* @param directiveName the name of the directive in question
*
* @return true if the directive is built-in, false otherwise
*/
public static boolean isBuiltInDirective(String directiveName) {
return BUILT_IN_DIRECTIVES_MAP.containsKey(directiveName);
}
/**
* Returns true if the provided directive is a built-in directive.
*
* @param directive the directive in question
*
* @return true if the directive is built-in, false otherwise
*/
public static boolean isBuiltInDirective(GraphQLDirective directive) {
return isBuiltInDirective(directive.getName());
}
private static Description createDescription(String s) {
return new Description(s, null, false);
}
private static final AtomicBoolean EXPERIMENTAL_DISABLE_ERROR_PROPAGATION_DIRECTIVE_ENABLED = new AtomicBoolean(true);
/**
* This can be used to get the state the `@experimental_disableErrorPropagation` directive support on a JVM wide basis .
* @return true if the `@experimental_disableErrorPropagation` directive will be respected
*/
public static boolean isExperimentalDisableErrorPropagationDirectiveEnabled() {
return EXPERIMENTAL_DISABLE_ERROR_PROPAGATION_DIRECTIVE_ENABLED.get();
}
/**
* This can be used to disable the `@experimental_disableErrorPropagation` directive support on a JVM wide basis in case your server
* implementation does NOT want to act on this directive ever.
*
* @param flag the desired state of the flag
*/
public static void setExperimentalDisableErrorPropagationEnabled(boolean flag) {
EXPERIMENTAL_DISABLE_ERROR_PROPAGATION_DIRECTIVE_ENABLED.set(flag);
}
}