forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphQLDirective.java
319 lines (267 loc) · 9.94 KB
/
GraphQLDirective.java
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
package graphql.schema;
import com.google.common.collect.ImmutableList;
import graphql.PublicApi;
import graphql.language.DirectiveDefinition;
import graphql.util.TraversalControl;
import graphql.util.TraverserContext;
import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import static graphql.Assert.assertNotEmpty;
import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertValidName;
import static graphql.introspection.Introspection.DirectiveLocation;
import static graphql.util.FpKit.getByName;
/**
* A directive can be used to modify the behavior of a graphql field or type.
* <p>
* See <a href="https://graphql.org/learn/queries/#directives">https://graphql.org/learn/queries/#directives</a> for more details on the concept.
* <p>
* A directive has a definition, that is what arguments it takes, and it can also be applied
* to other schema elements. Originally graphql-java re-used the {@link GraphQLDirective} and {@link GraphQLArgument}
* classes to do both purposes. This was a modelling mistake. New {@link GraphQLAppliedDirective} and {@link GraphQLAppliedDirectiveArgument}
* classes have been introduced to better model when a directive is applied to a schema element,
* as opposed to its schema definition itself.
*/
@PublicApi
public class GraphQLDirective implements GraphQLNamedSchemaElement {
private final String name;
private final boolean repeatable;
private final String description;
private final EnumSet<DirectiveLocation> locations;
private final ImmutableList<GraphQLArgument> arguments;
private final DirectiveDefinition definition;
public static final String CHILD_ARGUMENTS = "arguments";
private GraphQLDirective(String name,
String description,
boolean repeatable,
EnumSet<DirectiveLocation> locations,
List<GraphQLArgument> arguments,
DirectiveDefinition definition) {
assertValidName(name);
assertNotNull(arguments, () -> "arguments can't be null");
assertNotEmpty(locations, () -> "locations can't be empty");
this.name = name;
this.description = description;
this.repeatable = repeatable;
this.locations = locations;
this.arguments = ImmutableList.copyOf(arguments);
this.definition = definition;
}
@Override
public String getName() {
return name;
}
public boolean isRepeatable() {
return repeatable;
}
public boolean isNonRepeatable() {
return !repeatable;
}
public List<GraphQLArgument> getArguments() {
return arguments;
}
public GraphQLArgument getArgument(String name) {
for (GraphQLArgument argument : arguments) {
if (argument.getName().equals(name)) {
return argument;
}
}
return null;
}
public EnumSet<DirectiveLocation> validLocations() {
return EnumSet.copyOf(locations);
}
public String getDescription() {
return description;
}
public DirectiveDefinition getDefinition() {
return definition;
}
@Override
public String toString() {
return "GraphQLDirective{" +
"name='" + name + '\'' +
", repeatable='" + repeatable + '\'' +
", arguments=" + arguments +
", locations=" + locations +
'}';
}
/**
* This helps you transform the current GraphQLDirective into another one by starting a builder with all
* the current values and allows you to transform it how you want.
*
* @param builderConsumer the consumer code that will be given a builder to transform
*
* @return a new field based on calling build on that builder
*/
public GraphQLDirective transform(Consumer<Builder> builderConsumer) {
Builder builder = newDirective(this);
builderConsumer.accept(builder);
return builder.build();
}
@Override
public GraphQLSchemaElement copy() {
return newDirective(this).build();
}
@Override
public TraversalControl accept(TraverserContext<GraphQLSchemaElement> context, GraphQLTypeVisitor visitor) {
return visitor.visitGraphQLDirective(this, context);
}
@Override
public List<GraphQLSchemaElement> getChildren() {
return ImmutableList.copyOf(arguments);
}
@Override
public SchemaElementChildrenContainer getChildrenWithTypeReferences() {
return SchemaElementChildrenContainer.newSchemaElementChildrenContainer()
.children(CHILD_ARGUMENTS, arguments)
.build();
}
@Override
public GraphQLDirective withNewChildren(SchemaElementChildrenContainer newChildren) {
return transform(builder ->
builder.replaceArguments(newChildren.getChildren(CHILD_ARGUMENTS))
);
}
/**
* This method can be used to turn a directive that was being use as an applied directive into one.
* @return an {@link GraphQLAppliedDirective}
*/
public GraphQLAppliedDirective toAppliedDirective() {
GraphQLAppliedDirective.Builder builder = GraphQLAppliedDirective.newDirective();
builder.name(this.name);
for (GraphQLArgument argument : arguments) {
builder.argument(argument.toAppliedArgument());
}
return builder.build();
}
/**
* {@inheritDoc}
*/
@Override
public final boolean equals(Object o) {
return super.equals(o);
}
/**
* {@inheritDoc}
*/
@Override
public final int hashCode() {
return super.hashCode();
}
public static Builder newDirective() {
return new Builder();
}
public static Builder newDirective(GraphQLDirective existing) {
return new Builder(existing);
}
public static class Builder extends GraphqlTypeBuilder<Builder> {
private EnumSet<DirectiveLocation> locations = EnumSet.noneOf(DirectiveLocation.class);
private final Map<String, GraphQLArgument> arguments = new LinkedHashMap<>();
private DirectiveDefinition definition;
private boolean repeatable = false;
public Builder() {
}
public Builder(GraphQLDirective existing) {
this.name = existing.getName();
this.description = existing.getDescription();
this.repeatable = existing.isRepeatable();
this.locations = existing.validLocations();
this.arguments.putAll(getByName(existing.getArguments(), GraphQLArgument::getName));
}
public Builder repeatable(boolean repeatable) {
this.repeatable = repeatable;
return this;
}
public Builder validLocations(DirectiveLocation... validLocations) {
Collections.addAll(locations, validLocations);
return this;
}
public Builder validLocation(DirectiveLocation validLocation) {
locations.add(validLocation);
return this;
}
public Builder clearValidLocations() {
locations = EnumSet.noneOf(DirectiveLocation.class);
return this;
}
public Builder argument(GraphQLArgument argument) {
assertNotNull(argument, () -> "argument must not be null");
arguments.put(argument.getName(), argument);
return this;
}
public Builder replaceArguments(List<GraphQLArgument> arguments) {
assertNotNull(arguments, () -> "arguments must not be null");
this.arguments.clear();
for (GraphQLArgument argument : arguments) {
this.arguments.put(argument.getName(), argument);
}
return this;
}
/**
* Take an argument builder in a function definition and apply. Can be used in a jdk8 lambda
* e.g.:
* <pre>
* {@code
* argument(a -> a.name("argumentName"))
* }
* </pre>
*
* @param builderFunction a supplier for the builder impl
*
* @return this
*/
public Builder argument(UnaryOperator<GraphQLArgument.Builder> builderFunction) {
GraphQLArgument.Builder builder = GraphQLArgument.newArgument();
builder = builderFunction.apply(builder);
return argument(builder);
}
/**
* Same effect as the argument(GraphQLArgument). Builder.build() is called
* from within
*
* @param builder an un-built/incomplete GraphQLArgument
*
* @return this
*/
public Builder argument(GraphQLArgument.Builder builder) {
return argument(builder.build());
}
/**
* This is used to clear all the arguments in the builder so far.
*
* @return the builder
*/
public Builder clearArguments() {
arguments.clear();
return this;
}
public Builder definition(DirectiveDefinition definition) {
this.definition = definition;
return this;
}
// -- the following are repeated to avoid a binary incompatibility problem --
@Override
public Builder name(String name) {
return super.name(name);
}
@Override
public Builder description(String description) {
return super.description(description);
}
public GraphQLDirective build() {
return new GraphQLDirective(
name,
description,
repeatable,
locations,
sort(arguments, GraphQLDirective.class, GraphQLArgument.class),
definition);
}
}
}