-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
GraphQLInterfaceType.java
468 lines (397 loc) · 17.2 KB
/
GraphQLInterfaceType.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
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
package graphql.schema;
import com.google.common.collect.ImmutableList;
import graphql.Assert;
import graphql.DirectivesUtil;
import graphql.Internal;
import graphql.PublicApi;
import graphql.language.InterfaceTypeDefinition;
import graphql.language.InterfaceTypeExtensionDefinition;
import graphql.util.FpKit;
import graphql.util.TraversalControl;
import graphql.util.TraverserContext;
import java.util.ArrayList;
import java.util.Comparator;
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.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
import static graphql.Assert.assertValidName;
import static graphql.collect.ImmutableKit.emptyList;
import static graphql.schema.GraphqlTypeComparators.sortTypes;
import static graphql.util.FpKit.getByName;
import static graphql.util.FpKit.valuesToList;
/**
* In graphql, an interface is an abstract type that defines the set of fields that a type must include to
* implement that interface.
* <p>
* At runtime a {@link TypeResolver} is used to take an interface object value and decide what {@link GraphQLObjectType}
* represents this interface type.
* <p>
* See <a href="https://graphql.org/learn/schema/#interfaces">https://graphql.org/learn/schema/#interfaces</a> for more details on the concept.
*/
@PublicApi
public class GraphQLInterfaceType implements GraphQLNamedType, GraphQLCompositeType, GraphQLUnmodifiedType, GraphQLNullableType, GraphQLDirectiveContainer, GraphQLImplementingType {
private final String name;
private final String description;
private final Map<String, GraphQLFieldDefinition> fieldDefinitionsByName;
private final TypeResolver typeResolver;
private final InterfaceTypeDefinition definition;
private final ImmutableList<InterfaceTypeExtensionDefinition> extensionDefinitions;
private final DirectivesUtil.DirectivesHolder directivesHolder;
private final ImmutableList<GraphQLNamedOutputType> originalInterfaces;
private final Comparator<? super GraphQLSchemaElement> interfaceComparator;
private ImmutableList<GraphQLNamedOutputType> replacedInterfaces;
public static final String CHILD_FIELD_DEFINITIONS = "fieldDefinitions";
public static final String CHILD_INTERFACES = "interfaces";
@Internal
private GraphQLInterfaceType(String name,
String description,
List<GraphQLFieldDefinition> fieldDefinitions,
TypeResolver typeResolver,
List<GraphQLDirective> directives,
List<GraphQLAppliedDirective> appliedDirectives,
InterfaceTypeDefinition definition,
List<InterfaceTypeExtensionDefinition> extensionDefinitions,
List<GraphQLNamedOutputType> interfaces,
Comparator<? super GraphQLSchemaElement> interfaceComparator) {
assertValidName(name);
assertNotNull(fieldDefinitions, () -> "fieldDefinitions can't null");
assertNotNull(directives, () -> "directives cannot be null");
this.name = name;
this.description = description;
this.typeResolver = typeResolver;
this.definition = definition;
this.interfaceComparator = interfaceComparator;
this.originalInterfaces = ImmutableList.copyOf(sortTypes(interfaceComparator, interfaces));
this.extensionDefinitions = ImmutableList.copyOf(extensionDefinitions);
this.directivesHolder = new DirectivesUtil.DirectivesHolder(directives, appliedDirectives);
this.fieldDefinitionsByName = buildDefinitionMap(fieldDefinitions);
}
private Map<String, GraphQLFieldDefinition> buildDefinitionMap(List<GraphQLFieldDefinition> fieldDefinitions) {
return FpKit.getByName(fieldDefinitions, GraphQLFieldDefinition::getName,
(fld1, fld2) -> assertShouldNeverHappen("Duplicated definition for field '%s' in interface '%s'", fld1.getName(), this.name));
}
@Override
public GraphQLFieldDefinition getFieldDefinition(String name) {
return fieldDefinitionsByName.get(name);
}
@Override
public List<GraphQLFieldDefinition> getFieldDefinitions() {
return ImmutableList.copyOf(fieldDefinitionsByName.values());
}
@Override
public String getName() {
return name;
}
public String getDescription() {
return description;
}
// to be removed in a future version when all code is in the code registry
@Internal
@Deprecated(since = "2018-12-03")
TypeResolver getTypeResolver() {
return typeResolver;
}
public InterfaceTypeDefinition getDefinition() {
return definition;
}
public List<InterfaceTypeExtensionDefinition> getExtensionDefinitions() {
return extensionDefinitions;
}
@Override
public List<GraphQLDirective> getDirectives() {
return directivesHolder.getDirectives();
}
@Override
public Map<String, GraphQLDirective> getDirectivesByName() {
return directivesHolder.getDirectivesByName();
}
@Override
public Map<String, List<GraphQLDirective>> getAllDirectivesByName() {
return directivesHolder.getAllDirectivesByName();
}
@Override
public GraphQLDirective getDirective(String directiveName) {
return directivesHolder.getDirective(directiveName);
}
@Override
public List<GraphQLAppliedDirective> getAppliedDirectives() {
return directivesHolder.getAppliedDirectives();
}
@Override
public Map<String, List<GraphQLAppliedDirective>> getAllAppliedDirectivesByName() {
return directivesHolder.getAllAppliedDirectivesByName();
}
@Override
public GraphQLAppliedDirective getAppliedDirective(String directiveName) {
return directivesHolder.getAppliedDirective(directiveName);
}
@Override
public String toString() {
return "GraphQLInterfaceType{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
", fieldDefinitionsByName=" + fieldDefinitionsByName.keySet() +
", typeResolver=" + typeResolver +
'}';
}
/**
* This helps you transform the current GraphQLInterfaceType 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 object based on calling build on that builder
*/
public GraphQLInterfaceType transform(Consumer<Builder> builderConsumer) {
Builder builder = newInterface(this);
builderConsumer.accept(builder);
return builder.build();
}
@Override
public GraphQLSchemaElement copy() {
return newInterface(this).build();
}
@Override
public TraversalControl accept(TraverserContext<GraphQLSchemaElement> context, GraphQLTypeVisitor visitor) {
return visitor.visitGraphQLInterfaceType(this, context);
}
@Override
public List<GraphQLSchemaElement> getChildren() {
List<GraphQLSchemaElement> children = new ArrayList<>(fieldDefinitionsByName.values());
children.addAll(getInterfaces());
children.addAll(directivesHolder.getDirectives());
children.addAll(directivesHolder.getAppliedDirectives());
return children;
}
@Override
public SchemaElementChildrenContainer getChildrenWithTypeReferences() {
return SchemaElementChildrenContainer.newSchemaElementChildrenContainer()
.children(CHILD_FIELD_DEFINITIONS, fieldDefinitionsByName.values())
.children(CHILD_INTERFACES, originalInterfaces)
.children(CHILD_DIRECTIVES, directivesHolder.getDirectives())
.children(CHILD_APPLIED_DIRECTIVES, directivesHolder.getAppliedDirectives())
.build();
}
@Override
public GraphQLInterfaceType withNewChildren(SchemaElementChildrenContainer newChildren) {
return transform(builder ->
builder.replaceDirectives(newChildren.getChildren(CHILD_DIRECTIVES))
.replaceFields(newChildren.getChildren(CHILD_FIELD_DEFINITIONS))
.replaceInterfacesOrReferences(newChildren.getChildren(CHILD_INTERFACES))
.replaceDirectives(newChildren.getChildren(CHILD_DIRECTIVES))
.replaceAppliedDirectives(newChildren.getChildren(CHILD_APPLIED_DIRECTIVES))
);
}
@Override
public List<GraphQLNamedOutputType> getInterfaces() {
if (replacedInterfaces != null) {
return ImmutableList.copyOf(replacedInterfaces);
}
return ImmutableList.copyOf(originalInterfaces);
}
void replaceInterfaces(List<GraphQLNamedOutputType> interfaces) {
this.replacedInterfaces = ImmutableList.copyOf(sortTypes(interfaceComparator, interfaces));
}
/**
* {@inheritDoc}
*/
@Override
public final boolean equals(Object o) {
return super.equals(o);
}
/**
* {@inheritDoc}
*/
@Override
public final int hashCode() {
return super.hashCode();
}
public static Builder newInterface() {
return new Builder();
}
public static Builder newInterface(GraphQLInterfaceType existing) {
return new Builder(existing);
}
@PublicApi
public static class Builder extends GraphqlDirectivesContainerTypeBuilder<Builder, Builder> {
private TypeResolver typeResolver;
private InterfaceTypeDefinition definition;
private List<InterfaceTypeExtensionDefinition> extensionDefinitions = emptyList();
private final Map<String, GraphQLFieldDefinition> fields = new LinkedHashMap<>();
private final Map<String, GraphQLNamedOutputType> interfaces = new LinkedHashMap<>();
public Builder() {
}
public Builder(GraphQLInterfaceType existing) {
this.name = existing.getName();
this.description = existing.getDescription();
this.typeResolver = existing.getTypeResolver();
this.definition = existing.getDefinition();
this.extensionDefinitions = existing.getExtensionDefinitions();
this.fields.putAll(getByName(existing.getFieldDefinitions(), GraphQLFieldDefinition::getName));
this.interfaces.putAll(getByName(existing.originalInterfaces, GraphQLNamedType::getName));
copyExistingDirectives(existing);
}
public Builder definition(InterfaceTypeDefinition definition) {
this.definition = definition;
return this;
}
public Builder extensionDefinitions(List<InterfaceTypeExtensionDefinition> extensionDefinitions) {
this.extensionDefinitions = extensionDefinitions;
return this;
}
public Builder field(GraphQLFieldDefinition fieldDefinition) {
assertNotNull(fieldDefinition, () -> "fieldDefinition can't be null");
this.fields.put(fieldDefinition.getName(), fieldDefinition);
return this;
}
/**
* Take a field builder in a function definition and apply. Can be used in a jdk8 lambda
* e.g.:
* <pre>
* {@code
* field(f -> f.name("fieldName"))
* }
* </pre>
*
* @param builderFunction a supplier for the builder impl
*
* @return this
*/
public Builder field(UnaryOperator<GraphQLFieldDefinition.Builder> builderFunction) {
assertNotNull(builderFunction, () -> "builderFunction can't be null");
GraphQLFieldDefinition.Builder builder = GraphQLFieldDefinition.newFieldDefinition();
builder = builderFunction.apply(builder);
return field(builder);
}
/**
* Same effect as the field(GraphQLFieldDefinition). Builder.build() is called
* from within
*
* @param builder an un-built/incomplete GraphQLFieldDefinition
*
* @return this
*/
public Builder field(GraphQLFieldDefinition.Builder builder) {
return field(builder.build());
}
public Builder fields(List<GraphQLFieldDefinition> fieldDefinitions) {
assertNotNull(fieldDefinitions, () -> "fieldDefinitions can't be null");
fieldDefinitions.forEach(this::field);
return this;
}
public Builder replaceFields(List<GraphQLFieldDefinition> fieldDefinitions) {
assertNotNull(fieldDefinitions, () -> "fieldDefinitions can't be null");
this.fields.clear();
fieldDefinitions.forEach(this::field);
return this;
}
public boolean hasField(String fieldName) {
return fields.containsKey(fieldName);
}
/**
* This is used to clear all the fields in the builder so far.
*
* @return the builder
*/
public Builder clearFields() {
fields.clear();
return this;
}
/**
* @param typeResolver the type resolver
*
* @return this builder
*
* @deprecated use {@link graphql.schema.GraphQLCodeRegistry.Builder#typeResolver(GraphQLInterfaceType, TypeResolver)} instead
*/
@Deprecated(since = "2018-12-03")
public Builder typeResolver(TypeResolver typeResolver) {
this.typeResolver = typeResolver;
return this;
}
public Builder replaceInterfaces(List<GraphQLInterfaceType> interfaces) {
return replaceInterfacesOrReferences(interfaces);
}
public Builder replaceInterfacesOrReferences(List<? extends GraphQLNamedOutputType> interfacesOrReferences) {
assertNotNull(interfacesOrReferences, () -> "interfaces can't be null");
this.interfaces.clear();
for (GraphQLNamedOutputType schemaElement : interfacesOrReferences) {
if (schemaElement instanceof GraphQLInterfaceType || schemaElement instanceof GraphQLTypeReference) {
this.interfaces.put(schemaElement.getName(), schemaElement);
} else {
Assert.assertShouldNeverHappen("Unexpected type " + (schemaElement != null ? schemaElement.getClass() : "null"));
}
}
return this;
}
public Builder withInterface(GraphQLInterfaceType interfaceType) {
assertNotNull(interfaceType, () -> "interfaceType can't be null");
this.interfaces.put(interfaceType.getName(), interfaceType);
return this;
}
public Builder withInterface(GraphQLTypeReference reference) {
assertNotNull(reference, () -> "reference can't be null");
this.interfaces.put(reference.getName(), reference);
return this;
}
public Builder withInterfaces(GraphQLInterfaceType... interfaceType) {
for (GraphQLInterfaceType type : interfaceType) {
withInterface(type);
}
return this;
}
public Builder withInterfaces(GraphQLTypeReference... references) {
for (GraphQLTypeReference reference : references) {
withInterface(reference);
}
return this;
}
// -- the following are repeated to avoid a binary incompatibility problem --
@Override
public Builder replaceDirectives(List<GraphQLDirective> directives) {
return super.replaceDirectives(directives);
}
@Override
public Builder withDirectives(GraphQLDirective... directives) {
return super.withDirectives(directives);
}
@Override
public Builder withDirective(GraphQLDirective directive) {
return super.withDirective(directive);
}
@Override
public Builder withDirective(GraphQLDirective.Builder builder) {
return super.withDirective(builder);
}
@Override
public Builder clearDirectives() {
return super.clearDirectives();
}
@Override
public Builder name(String name) {
return super.name(name);
}
@Override
public Builder description(String description) {
return super.description(description);
}
public GraphQLInterfaceType build() {
return new GraphQLInterfaceType(
name,
description,
sort(fields, GraphQLInterfaceType.class, GraphQLFieldDefinition.class),
typeResolver,
sort(directives, GraphQLInterfaceType.class, GraphQLDirective.class),
sort(appliedDirectives, GraphQLScalarType.class, GraphQLAppliedDirective.class),
definition,
extensionDefinitions,
valuesToList(interfaces),
getComparator(GraphQLInterfaceType.class, GraphQLInterfaceType.class)
);
}
}
}