Repository navigation
Expand file tree
/
Copy pathExecutableNormalizedOperationToAstCompiler.java
More file actions
196 lines (172 loc) · 8.52 KB
/
Copy pathExecutableNormalizedOperationToAstCompiler.java
File metadata and controls
196 lines (172 loc) · 8.52 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
package graphql.normalized;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import graphql.Internal;
import graphql.language.Argument;
import graphql.language.ArrayValue;
import graphql.language.Document;
import graphql.language.Field;
import graphql.language.InlineFragment;
import graphql.language.NullValue;
import graphql.language.ObjectField;
import graphql.language.ObjectValue;
import graphql.language.OperationDefinition;
import graphql.language.Selection;
import graphql.language.SelectionSet;
import graphql.language.TypeName;
import graphql.language.Value;
import graphql.schema.GraphQLSchema;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static graphql.collect.ImmutableKit.map;
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;
@Internal
public class ExecutableNormalizedOperationToAstCompiler {
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;
}
}
public static CompilerResult compileToDocument(
GraphQLSchema schema,
OperationDefinition.Operation operationKind,
String operationName,
List<ExecutableNormalizedField> topLevelFields,
VariablePredicate variablePredicate
) {
VariableAccumulator variableAccumulator = new VariableAccumulator(variablePredicate);
List<Selection<?>> selections = selectionsForNormalizedFields(schema, topLevelFields, variableAccumulator);
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<?>> selectionsForNormalizedFields(
GraphQLSchema schema,
List<ExecutableNormalizedField> executableNormalizedFields,
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 fragement in the output
Map<String, List<Field>> conditionalFieldsByObjectTypeName = new LinkedHashMap<>();
for (ExecutableNormalizedField nf : executableNormalizedFields) {
Map<String, List<Field>> groupFieldsForChild = selectionForNormalizedField(schema, nf, variableAccumulator);
if (nf.isConditional(schema)) {
groupFieldsForChild.forEach((objectTypeName, fields) -> {
List<Field> fieldList = conditionalFieldsByObjectTypeName.computeIfAbsent(objectTypeName, ignored -> new ArrayList<>());
fieldList.addAll(fields);
});
} else {
List<Field> fields = groupFieldsForChild.values().iterator().next();
selections.addAll(fields);
}
}
conditionalFieldsByObjectTypeName.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 Map<String, List<Field>> selectionForNormalizedField(
GraphQLSchema schema,
ExecutableNormalizedField executableNormalizedField,
VariableAccumulator variableAccumulator
) {
Map<String, List<Field>> groupedFields = new LinkedHashMap<>();
for (String objectTypeName : executableNormalizedField.getObjectTypeNames()) {
List<Selection<?>> subSelections = selectionsForNormalizedFields(schema, executableNormalizedField.getChildren(), variableAccumulator);
SelectionSet selectionSet = null;
if (subSelections.size() > 0) {
selectionSet = newSelectionSet()
.selections(subSelections)
.build();
}
List<Argument> arguments = createArguments(executableNormalizedField, variableAccumulator);
Field field = newField()
.name(executableNormalizedField.getFieldName())
.alias(executableNormalizedField.getAlias())
.selectionSet(selectionSet)
.arguments(arguments)
.build();
groupedFields.computeIfAbsent(objectTypeName, ignored -> new ArrayList<>()).add(field);
}
return groupedFields;
}
private static SelectionSet selectionSet(List<Field> fields) {
return newSelectionSet().selections(fields).build();
}
private static List<Argument> createArguments(ExecutableNormalizedField executableNormalizedField, VariableAccumulator variableAccumulator) {
ImmutableList.Builder<Argument> result = ImmutableList.builder();
ImmutableMap<String, NormalizedInputValue> normalizedArguments = executableNormalizedField.getNormalizedArguments();
for (String argName : normalizedArguments.keySet()) {
NormalizedInputValue normalizedInputValue = normalizedArguments.get(argName);
Value<?> value = argValue(executableNormalizedField, argName, normalizedInputValue, variableAccumulator);
Argument argument = newArgument()
.name(argName)
.value(value)
.build();
result.add(argument);
}
return result.build();
}
@SuppressWarnings("unchecked")
private static Value<?> argValue(ExecutableNormalizedField executableNormalizedField, String argName, @Nullable Object value, VariableAccumulator variableAccumulator) {
if (value instanceof List) {
ArrayValue.Builder arrayValue = ArrayValue.newArrayValue();
arrayValue.values(map((List<Object>) value, val -> argValue(executableNormalizedField, argName, val, variableAccumulator)));
return arrayValue.build();
}
if (value instanceof Map) {
ObjectValue.Builder objectValue = ObjectValue.newObjectValue();
Map<String, Object> map = (Map<String, Object>) value;
for (String fieldName : map.keySet()) {
Value<?> fieldValue = argValue(executableNormalizedField, argName, (NormalizedInputValue) map.get(fieldName), variableAccumulator);
objectValue.objectField(ObjectField.newObjectField().name(fieldName).value(fieldValue).build());
}
return objectValue.build();
}
if (value == null) {
return NullValue.newNullValue().build();
}
return (Value<?>) value;
}
@NotNull
private static Value<?> argValue(ExecutableNormalizedField executableNormalizedField, String argName, NormalizedInputValue normalizedInputValue, VariableAccumulator variableAccumulator) {
if (variableAccumulator.shouldMakeVariable(executableNormalizedField, argName, normalizedInputValue)) {
VariableValueWithDefinition variableWithDefinition = variableAccumulator.accumulateVariable(normalizedInputValue);
return variableWithDefinition.getVariableReference();
} else {
return argValue(executableNormalizedField, argName, normalizedInputValue.getValue(), variableAccumulator);
}
}
}