Repository navigation
Expand file tree
/
Copy pathExecutableNormalizedOperationToAstCompiler.java
More file actions
473 lines (411 loc) · 25.2 KB
/
Copy pathExecutableNormalizedOperationToAstCompiler.java
File metadata and controls
473 lines (411 loc) · 25.2 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
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
package graphql.normalized;
import com.google.common.collect.ImmutableList;
import graphql.Assert;
import graphql.Directives;
import graphql.ExperimentalApi;
import graphql.PublicApi;
import graphql.execution.directives.QueryAppliedDirective;
import graphql.execution.directives.QueryDirectives;
import graphql.introspection.Introspection;
import graphql.language.Argument;
import graphql.language.Directive;
import graphql.language.Document;
import graphql.language.Field;
import graphql.language.InlineFragment;
import graphql.language.OperationDefinition;
import graphql.language.Selection;
import graphql.language.SelectionSet;
import graphql.language.StringValue;
import graphql.language.TypeName;
import graphql.normalized.incremental.NormalizedDeferredExecution;
import graphql.schema.GraphQLCompositeType;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLUnmodifiedType;
import graphql.util.LinkedHashMapFactory;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import static graphql.collect.ImmutableKit.emptyList;
import static graphql.language.Argument.newArgument;
import static graphql.language.Field.newField;
import static graphql.language.InlineFragment.newInlineFragment;
import static graphql.language.SelectionSet.newSelectionSet;
import static graphql.language.TypeName.newTypeName;
import static graphql.normalized.ArgumentMaker.createArguments;
import static graphql.schema.GraphQLTypeUtil.unwrapAll;
/**
* This class can take a list of {@link ExecutableNormalizedField}s and compiling out a
* normalised operation {@link Document} that would represent how those fields
* may be executed.
* <p>
* This is essentially the reverse of {@link ExecutableNormalizedOperationFactory} which takes
* operation text and makes {@link ExecutableNormalizedField}s from it, this takes {@link ExecutableNormalizedField}s
* and makes operation text from it.
* <p>
* You could for example send that operation text onto to some other graphql server if it
* has the same schema as the one provided.
*/
@PublicApi
public class ExecutableNormalizedOperationToAstCompiler {
/**
* The result is a {@link Document} and a map of variables
* that would go with that document.
*/
public static class CompilerResult {
private final Document document;
private final Map<String, Object> variables;
public CompilerResult(Document document, Map<String, Object> variables) {
this.document = document;
this.variables = variables;
}
public Document getDocument() {
return document;
}
public Map<String, Object> getVariables() {
return variables;
}
}
/**
* This will compile an operation text {@link Document} with possibly variables from the given {@link ExecutableNormalizedField}s
* <p>
* The {@link VariablePredicate} is used called to decide if the given argument values should be made into a variable
* OR inlined into the operation text as a graphql literal.
*
* @param schema the graphql schema to use
* @param operationKind the kind of operation
* @param operationName the name of the operation to use
* @param topLevelFields the top level {@link ExecutableNormalizedField}s to start from
* @param variablePredicate the variable predicate that decides if arguments turn into variables or not during compilation
*
* @return a {@link CompilerResult} object
*/
public static CompilerResult compileToDocument(@NonNull GraphQLSchema schema,
OperationDefinition.@NonNull Operation operationKind,
@Nullable String operationName,
@NonNull List<ExecutableNormalizedField> topLevelFields,
@Nullable VariablePredicate variablePredicate) {
return compileToDocument(schema, operationKind, operationName, topLevelFields, LinkedHashMapFactory.of(), variablePredicate);
}
/**
* This will compile an operation text {@link Document} with possibly variables from the given {@link ExecutableNormalizedField}s
* <p>
* The {@link VariablePredicate} is used called to decide if the given argument values should be made into a variable
* OR inlined into the operation text as a graphql literal.
*
* @param schema the graphql schema to use
* @param operationKind the kind of operation
* @param operationName the name of the operation to use
* @param topLevelFields the top level {@link ExecutableNormalizedField}s to start from
* @param normalizedFieldToQueryDirectives the map of normalized field to query directives
* @param variablePredicate the variable predicate that decides if arguments turn into variables or not during compilation
*
* @return a {@link CompilerResult} object
*/
public static CompilerResult compileToDocument(@NonNull GraphQLSchema schema,
OperationDefinition.@NonNull Operation operationKind,
@Nullable String operationName,
@NonNull List<ExecutableNormalizedField> topLevelFields,
@NonNull Map<ExecutableNormalizedField, QueryDirectives> normalizedFieldToQueryDirectives,
@Nullable VariablePredicate variablePredicate) {
return compileToDocument(schema, operationKind, operationName, topLevelFields, normalizedFieldToQueryDirectives, variablePredicate, false);
}
/**
* This will compile an operation text {@link Document} with possibly variables from the given {@link ExecutableNormalizedField}s, with support for the experimental @defer directive.
* <p>
* The {@link VariablePredicate} is used called to decide if the given argument values should be made into a variable
* OR inlined into the operation text as a graphql literal.
*
* @param schema the graphql schema to use
* @param operationKind the kind of operation
* @param operationName the name of the operation to use
* @param topLevelFields the top level {@link ExecutableNormalizedField}s to start from
* @param variablePredicate the variable predicate that decides if arguments turn into variables or not during compilation
*
* @return a {@link CompilerResult} object
*
* @see ExecutableNormalizedOperationToAstCompiler#compileToDocument(GraphQLSchema, OperationDefinition.Operation, String, List, VariablePredicate)
*/
@ExperimentalApi
public static CompilerResult compileToDocumentWithDeferSupport(@NonNull GraphQLSchema schema,
OperationDefinition.@NonNull Operation operationKind,
@Nullable String operationName,
@NonNull List<ExecutableNormalizedField> topLevelFields,
@Nullable VariablePredicate variablePredicate
) {
return compileToDocumentWithDeferSupport(schema, operationKind, operationName, topLevelFields, LinkedHashMapFactory.of(), variablePredicate);
}
/**
* This will compile an operation text {@link Document} with possibly variables from the given {@link ExecutableNormalizedField}s, with support for the experimental @defer directive.
* <p>
* The {@link VariablePredicate} is used called to decide if the given argument values should be made into a variable
* OR inlined into the operation text as a graphql literal.
*
* @param schema the graphql schema to use
* @param operationKind the kind of operation
* @param operationName the name of the operation to use
* @param topLevelFields the top level {@link ExecutableNormalizedField}s to start from
* @param normalizedFieldToQueryDirectives the map of normalized field to query directives
* @param variablePredicate the variable predicate that decides if arguments turn into variables or not during compilation
*
* @return a {@link CompilerResult} object
*
* @see ExecutableNormalizedOperationToAstCompiler#compileToDocument(GraphQLSchema, OperationDefinition.Operation, String, List, Map, VariablePredicate)
*/
@ExperimentalApi
public static CompilerResult compileToDocumentWithDeferSupport(@NonNull GraphQLSchema schema,
OperationDefinition.@NonNull Operation operationKind,
@Nullable String operationName,
@NonNull List<ExecutableNormalizedField> topLevelFields,
@NonNull Map<ExecutableNormalizedField, QueryDirectives> normalizedFieldToQueryDirectives,
@Nullable VariablePredicate variablePredicate
) {
return compileToDocument(schema, operationKind, operationName, topLevelFields, normalizedFieldToQueryDirectives, variablePredicate, true);
}
private static CompilerResult compileToDocument(@NonNull GraphQLSchema schema,
OperationDefinition.@NonNull Operation operationKind,
@Nullable String operationName,
@NonNull List<ExecutableNormalizedField> topLevelFields,
@NonNull Map<ExecutableNormalizedField, QueryDirectives> normalizedFieldToQueryDirectives,
@Nullable VariablePredicate variablePredicate,
boolean deferSupport) {
GraphQLObjectType operationType = getOperationType(schema, operationKind);
VariableAccumulator variableAccumulator = new VariableAccumulator(variablePredicate);
List<Selection<?>> selections = subselectionsForNormalizedField(schema, operationType.getName(), topLevelFields, normalizedFieldToQueryDirectives, variableAccumulator, deferSupport);
SelectionSet selectionSet = new SelectionSet(selections);
OperationDefinition.Builder definitionBuilder = OperationDefinition.newOperationDefinition()
.name(operationName)
.operation(operationKind)
.selectionSet(selectionSet);
definitionBuilder.variableDefinitions(variableAccumulator.getVariableDefinitions());
return new CompilerResult(
Document.newDocument()
.definition(definitionBuilder.build())
.build(),
variableAccumulator.getVariablesMap()
);
}
private static List<Selection<?>> subselectionsForNormalizedField(GraphQLSchema schema,
@NonNull String parentOutputType,
List<ExecutableNormalizedField> executableNormalizedFields,
@NonNull Map<ExecutableNormalizedField, QueryDirectives> normalizedFieldToQueryDirectives,
VariableAccumulator variableAccumulator,
boolean deferSupport) {
if (deferSupport) {
return subselectionsForNormalizedFieldWithDeferSupport(schema, parentOutputType, executableNormalizedFields, normalizedFieldToQueryDirectives, variableAccumulator);
} else {
return subselectionsForNormalizedFieldNoDeferSupport(schema, parentOutputType, executableNormalizedFields, normalizedFieldToQueryDirectives, variableAccumulator);
}
}
private static List<Selection<?>> subselectionsForNormalizedFieldNoDeferSupport(GraphQLSchema schema,
@NonNull String parentOutputType,
List<ExecutableNormalizedField> executableNormalizedFields,
@NonNull Map<ExecutableNormalizedField, QueryDirectives> normalizedFieldToQueryDirectives,
VariableAccumulator variableAccumulator) {
ImmutableList.Builder<Selection<?>> selections = ImmutableList.builder();
// All conditional fields go here instead of directly to selections, so they can be grouped together
// in the same inline fragment in the output
Map<String, List<Field>> fieldsByTypeCondition = new LinkedHashMap<>();
for (ExecutableNormalizedField nf : executableNormalizedFields) {
if (nf.isConditional(schema)) {
selectionForNormalizedField(schema, nf, normalizedFieldToQueryDirectives, variableAccumulator, false)
.forEach((objectTypeName, field) ->
fieldsByTypeCondition
.computeIfAbsent(objectTypeName, ignored -> new ArrayList<>())
.add(field));
} else {
selections.add(selectionForNormalizedField(schema, parentOutputType, nf, normalizedFieldToQueryDirectives, variableAccumulator, false));
}
}
fieldsByTypeCondition.forEach((objectTypeName, fields) -> {
TypeName typeName = newTypeName(objectTypeName).build();
InlineFragment inlineFragment = newInlineFragment()
.typeCondition(typeName)
.selectionSet(selectionSet(fields))
.build();
selections.add(inlineFragment);
});
return selections.build();
}
private static List<Selection<?>> subselectionsForNormalizedFieldWithDeferSupport(GraphQLSchema schema,
@NonNull String parentOutputType,
List<ExecutableNormalizedField> executableNormalizedFields,
@NonNull Map<ExecutableNormalizedField, QueryDirectives> normalizedFieldToQueryDirectives,
VariableAccumulator variableAccumulator) {
ImmutableList.Builder<Selection<?>> selections = ImmutableList.builder();
// All conditional and deferred fields go here instead of directly to selections, so they can be grouped together
// in the same inline fragment in the output
//
Map<ExecutionFragmentDetails, List<Field>> fieldsByFragmentDetails = new LinkedHashMap<>();
for (ExecutableNormalizedField nf : executableNormalizedFields) {
LinkedHashSet<NormalizedDeferredExecution> deferredExecutions = nf.getDeferredExecutions();
if (nf.isConditional(schema)) {
selectionForNormalizedField(schema, nf, normalizedFieldToQueryDirectives, variableAccumulator, true)
.forEach((objectTypeName, field) -> {
if (deferredExecutions == null || deferredExecutions.isEmpty()) {
fieldsByFragmentDetails
.computeIfAbsent(new ExecutionFragmentDetails(objectTypeName, null), ignored -> new ArrayList<>())
.add(field);
} else {
deferredExecutions.forEach(deferredExecution -> {
fieldsByFragmentDetails
.computeIfAbsent(new ExecutionFragmentDetails(objectTypeName, deferredExecution), ignored -> new ArrayList<>())
.add(field);
});
}
});
} else if (deferredExecutions != null && !deferredExecutions.isEmpty()) {
Field field = selectionForNormalizedField(schema, parentOutputType, nf, normalizedFieldToQueryDirectives, variableAccumulator, true);
deferredExecutions.forEach(deferredExecution -> {
fieldsByFragmentDetails
.computeIfAbsent(new ExecutionFragmentDetails(null, deferredExecution), ignored -> new ArrayList<>())
.add(field);
});
} else {
selections.add(selectionForNormalizedField(schema, parentOutputType, nf, normalizedFieldToQueryDirectives, variableAccumulator, true));
}
}
fieldsByFragmentDetails.forEach((typeAndDeferPair, fields) -> {
InlineFragment.Builder fragmentBuilder = newInlineFragment()
.selectionSet(selectionSet(fields));
if (typeAndDeferPair.typeName != null) {
TypeName typeName = newTypeName(typeAndDeferPair.typeName).build();
fragmentBuilder.typeCondition(typeName);
}
if (typeAndDeferPair.deferredExecution != null) {
Directive.Builder deferBuilder = Directive.newDirective().name(Directives.DeferDirective.getName());
if (typeAndDeferPair.deferredExecution.getLabel() != null) {
deferBuilder.argument(newArgument().name("label").value(StringValue.of(typeAndDeferPair.deferredExecution.getLabel())).build());
}
fragmentBuilder.directive(deferBuilder.build());
}
selections.add(fragmentBuilder.build());
});
return selections.build();
}
/**
* @return Map of object type names to list of fields
*/
private static Map<String, Field> selectionForNormalizedField(GraphQLSchema schema,
ExecutableNormalizedField executableNormalizedField,
@NonNull Map<ExecutableNormalizedField, QueryDirectives> normalizedFieldToQueryDirectives,
VariableAccumulator variableAccumulator,
boolean deferSupport) {
Map<String, Field> groupedFields = new LinkedHashMap<>();
for (String objectTypeName : executableNormalizedField.getObjectTypeNames()) {
groupedFields.put(objectTypeName, selectionForNormalizedField(schema, objectTypeName, executableNormalizedField, normalizedFieldToQueryDirectives, variableAccumulator, deferSupport));
}
return groupedFields;
}
/**
* @return Map of object type names to list of fields
*/
private static Field selectionForNormalizedField(GraphQLSchema schema,
String objectTypeName,
ExecutableNormalizedField executableNormalizedField,
@NonNull Map<ExecutableNormalizedField, QueryDirectives> normalizedFieldToQueryDirectives,
VariableAccumulator variableAccumulator,
boolean deferSupport) {
final List<Selection<?>> subSelections;
if (executableNormalizedField.getChildren().isEmpty()) {
subSelections = emptyList();
} else {
GraphQLFieldDefinition fieldDef = getFieldDefinition(schema, objectTypeName, executableNormalizedField);
GraphQLUnmodifiedType fieldOutputType = unwrapAll(fieldDef.getType());
subSelections = subselectionsForNormalizedField(
schema,
fieldOutputType.getName(),
executableNormalizedField.getChildren(),
normalizedFieldToQueryDirectives,
variableAccumulator,
deferSupport
);
}
SelectionSet selectionSet = selectionSetOrNullIfEmpty(subSelections);
List<Argument> arguments = createArguments(executableNormalizedField, variableAccumulator);
QueryDirectives queryDirectives = normalizedFieldToQueryDirectives.get(executableNormalizedField);
Field.Builder builder = newField()
.name(executableNormalizedField.getFieldName())
.alias(executableNormalizedField.getAlias())
.selectionSet(selectionSet)
.arguments(arguments);
List<Directive> directives = buildDirectives(executableNormalizedField, queryDirectives, variableAccumulator);
return builder
.directives(directives)
.build();
}
private static @NonNull List<Directive> buildDirectives(ExecutableNormalizedField executableNormalizedField, QueryDirectives queryDirectives, VariableAccumulator variableAccumulator) {
if (queryDirectives == null || queryDirectives.getImmediateAppliedDirectivesByField().isEmpty()) {
return emptyList();
}
return queryDirectives.getImmediateAppliedDirectivesByField().entrySet().stream()
.flatMap(entry -> entry.getValue().stream())
.map(queryAppliedDirective -> buildDirective(executableNormalizedField, queryDirectives, queryAppliedDirective, variableAccumulator))
.collect(Collectors.toList());
}
private static Directive buildDirective(ExecutableNormalizedField executableNormalizedField, QueryDirectives queryDirectives, QueryAppliedDirective queryAppliedDirective, VariableAccumulator variableAccumulator) {
List<Argument> arguments = ArgumentMaker.createDirectiveArguments(executableNormalizedField, queryDirectives, queryAppliedDirective, variableAccumulator);
return Directive.newDirective()
.name(queryAppliedDirective.getName())
.arguments(arguments).build();
}
@Nullable
private static SelectionSet selectionSetOrNullIfEmpty(List<Selection<?>> selections) {
return selections.isEmpty() ? null : newSelectionSet().selections(selections).build();
}
private static SelectionSet selectionSet(List<Field> fields) {
return newSelectionSet().selections(fields).build();
}
@NonNull
private static GraphQLFieldDefinition getFieldDefinition(GraphQLSchema schema,
String parentType,
ExecutableNormalizedField nf) {
return Introspection.getFieldDef(schema, (GraphQLCompositeType) schema.getType(parentType), nf.getName());
}
@Nullable
private static GraphQLObjectType getOperationType(@NonNull GraphQLSchema schema,
OperationDefinition.@NonNull Operation operationKind) {
switch (operationKind) {
case QUERY:
return schema.getQueryType();
case MUTATION:
return schema.getMutationType();
case SUBSCRIPTION:
return schema.getSubscriptionType();
}
return Assert.assertShouldNeverHappen("Unknown operation kind " + operationKind);
}
/**
* Represents important execution details that can be associated with a fragment.
*/
private static class ExecutionFragmentDetails {
private final String typeName;
private final NormalizedDeferredExecution deferredExecution;
public ExecutionFragmentDetails(String typeName, NormalizedDeferredExecution deferredExecution) {
this.typeName = typeName;
this.deferredExecution = deferredExecution;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ExecutionFragmentDetails that = (ExecutionFragmentDetails) o;
return Objects.equals(typeName, that.typeName) && Objects.equals(deferredExecution, that.deferredExecution);
}
@Override
public int hashCode() {
return Objects.hash(typeName, deferredExecution);
}
}
}