Skip to content

Commit eee23e1

Browse files
committed
The ability to get query directives in ENF land - code refactor
1 parent 117fa41 commit eee23e1

1 file changed

Lines changed: 63 additions & 24 deletions

File tree

src/main/java/graphql/normalized/ExecutableNormalizedOperationFactory.java

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import graphql.schema.GraphQLCompositeType;
3030
import graphql.schema.GraphQLFieldDefinition;
3131
import graphql.schema.GraphQLInterfaceType;
32+
import graphql.schema.GraphQLNamedOutputType;
3233
import graphql.schema.GraphQLObjectType;
3334
import graphql.schema.GraphQLSchema;
3435
import graphql.schema.GraphQLType;
@@ -50,7 +51,6 @@
5051
import static graphql.Assert.assertNotNull;
5152
import static graphql.Assert.assertShouldNeverHappen;
5253
import static graphql.collect.ImmutableKit.map;
53-
import static graphql.execution.MergedField.newMergedField;
5454
import static graphql.schema.GraphQLTypeUtil.unwrapAll;
5555
import static graphql.util.FpKit.filterSet;
5656
import static graphql.util.FpKit.groupingBy;
@@ -68,21 +68,38 @@ public static ExecutableNormalizedOperation createExecutableNormalizedOperation(
6868
String operationName,
6969
CoercedVariables coercedVariableValues) {
7070
NodeUtil.GetOperationResult getOperationResult = NodeUtil.getOperation(document, operationName);
71-
return new ExecutableNormalizedOperationFactory().createNormalizedQueryImpl(graphQLSchema, getOperationResult.operationDefinition, getOperationResult.fragmentsByName, coercedVariableValues, null, GraphQLContext.getDefault(), Locale.getDefault());
71+
return new ExecutableNormalizedOperationFactory().createNormalizedQueryImpl(graphQLSchema,
72+
getOperationResult.operationDefinition,
73+
getOperationResult.fragmentsByName,
74+
coercedVariableValues,
75+
null,
76+
GraphQLContext.getDefault(),
77+
Locale.getDefault());
7278
}
7379

7480
public static ExecutableNormalizedOperation createExecutableNormalizedOperation(GraphQLSchema graphQLSchema,
7581
OperationDefinition operationDefinition,
7682
Map<String, FragmentDefinition> fragments,
7783
CoercedVariables coercedVariableValues) {
78-
return new ExecutableNormalizedOperationFactory().createNormalizedQueryImpl(graphQLSchema, operationDefinition, fragments, coercedVariableValues, null, GraphQLContext.getDefault(), Locale.getDefault());
84+
return new ExecutableNormalizedOperationFactory().createNormalizedQueryImpl(graphQLSchema,
85+
operationDefinition,
86+
fragments,
87+
coercedVariableValues,
88+
null,
89+
GraphQLContext.getDefault(),
90+
Locale.getDefault());
7991
}
8092

8193
public static ExecutableNormalizedOperation createExecutableNormalizedOperationWithRawVariables(GraphQLSchema graphQLSchema,
8294
Document document,
8395
String operationName,
8496
RawVariables rawVariables) {
85-
return createExecutableNormalizedOperationWithRawVariables(graphQLSchema, document, operationName, rawVariables, GraphQLContext.getDefault(), Locale.getDefault());
97+
return createExecutableNormalizedOperationWithRawVariables(graphQLSchema,
98+
document,
99+
operationName,
100+
rawVariables,
101+
GraphQLContext.getDefault(),
102+
Locale.getDefault());
86103
}
87104

88105
public static ExecutableNormalizedOperation createExecutableNormalizedOperationWithRawVariables(GraphQLSchema graphQLSchema,
@@ -92,7 +109,12 @@ public static ExecutableNormalizedOperation createExecutableNormalizedOperationW
92109
GraphQLContext graphQLContext,
93110
Locale locale) {
94111
NodeUtil.GetOperationResult getOperationResult = NodeUtil.getOperation(document, operationName);
95-
return new ExecutableNormalizedOperationFactory().createExecutableNormalizedOperationImplWithRawVariables(graphQLSchema, getOperationResult.operationDefinition, getOperationResult.fragmentsByName, rawVariables, graphQLContext, locale);
112+
return new ExecutableNormalizedOperationFactory().createExecutableNormalizedOperationImplWithRawVariables(graphQLSchema,
113+
getOperationResult.operationDefinition,
114+
getOperationResult.fragmentsByName,
115+
rawVariables,
116+
graphQLContext,
117+
locale);
96118
}
97119

98120
private ExecutableNormalizedOperation createExecutableNormalizedOperationImplWithRawVariables(GraphQLSchema graphQLSchema,
@@ -103,9 +125,23 @@ private ExecutableNormalizedOperation createExecutableNormalizedOperationImplWit
103125
Locale locale) {
104126

105127
List<VariableDefinition> variableDefinitions = operationDefinition.getVariableDefinitions();
106-
CoercedVariables coercedVariableValues = ValuesResolver.coerceVariableValues(graphQLSchema, variableDefinitions, rawVariables, graphQLContext, locale);
107-
Map<String, NormalizedInputValue> normalizedVariableValues = ValuesResolver.getNormalizedVariableValues(graphQLSchema, variableDefinitions, rawVariables, graphQLContext, locale);
108-
return createNormalizedQueryImpl(graphQLSchema, operationDefinition, fragments, coercedVariableValues, normalizedVariableValues, graphQLContext, locale);
128+
CoercedVariables coercedVariableValues = ValuesResolver.coerceVariableValues(graphQLSchema,
129+
variableDefinitions,
130+
rawVariables,
131+
graphQLContext,
132+
locale);
133+
Map<String, NormalizedInputValue> normalizedVariableValues = ValuesResolver.getNormalizedVariableValues(graphQLSchema,
134+
variableDefinitions,
135+
rawVariables,
136+
graphQLContext,
137+
locale);
138+
return createNormalizedQueryImpl(graphQLSchema,
139+
operationDefinition,
140+
fragments,
141+
coercedVariableValues,
142+
normalizedVariableValues,
143+
graphQLContext,
144+
locale);
109145
}
110146

111147
/**
@@ -143,7 +179,7 @@ private ExecutableNormalizedOperation createNormalizedQueryImpl(GraphQLSchema gr
143179

144180
for (ExecutableNormalizedField topLevel : collectFromOperationResult.children) {
145181
ImmutableList<FieldAndAstParent> fieldAndAstParents = collectFromOperationResult.normalizedFieldToAstFields.get(topLevel);
146-
MergedField mergedField = newMergedField(map(fieldAndAstParents, fieldAndAstParent -> fieldAndAstParent.field)).build();
182+
MergedField mergedField = newMergedField(fieldAndAstParents);
147183

148184
captureMergedField.accept(topLevel, mergedField);
149185

@@ -175,26 +211,26 @@ private ExecutableNormalizedOperation createNormalizedQueryImpl(GraphQLSchema gr
175211
}
176212

177213

178-
private void buildFieldWithChildren(ExecutableNormalizedField field,
214+
private void buildFieldWithChildren(ExecutableNormalizedField executableNormalizedField,
179215
ImmutableList<FieldAndAstParent> fieldAndAstParents,
180216
FieldCollectorNormalizedQueryParams fieldCollectorNormalizedQueryParams,
181217
ImmutableListMultimap.Builder<Field, ExecutableNormalizedField> fieldNormalizedField,
182218
BiConsumer<ExecutableNormalizedField, MergedField> captureMergedField,
183219
ImmutableListMultimap.Builder<FieldCoordinates, ExecutableNormalizedField> coordinatesToNormalizedFields,
184220
int curLevel) {
185-
CollectNFResult nextLevel = collectFromMergedField(fieldCollectorNormalizedQueryParams, field, fieldAndAstParents, curLevel + 1);
221+
CollectNFResult nextLevel = collectFromMergedField(fieldCollectorNormalizedQueryParams, executableNormalizedField, fieldAndAstParents, curLevel + 1);
186222

187-
for (ExecutableNormalizedField child : nextLevel.children) {
188-
field.addChild(child);
189-
ImmutableList<FieldAndAstParent> childFieldAndAstParents = nextLevel.normalizedFieldToAstFields.get(child);
223+
for (ExecutableNormalizedField childENF : nextLevel.children) {
224+
executableNormalizedField.addChild(childENF);
225+
ImmutableList<FieldAndAstParent> childFieldAndAstParents = nextLevel.normalizedFieldToAstFields.get(childENF);
190226

191-
MergedField mergedField = newMergedField(map(childFieldAndAstParents, fieldAndAstParent -> fieldAndAstParent.field)).build();
192-
captureMergedField.accept(child, mergedField);
227+
MergedField mergedField = newMergedField(childFieldAndAstParents);
228+
captureMergedField.accept(childENF, mergedField);
193229

194-
updateFieldToNFMap(child, childFieldAndAstParents, fieldNormalizedField);
195-
updateCoordinatedToNFMap(coordinatesToNormalizedFields, child);
230+
updateFieldToNFMap(childENF, childFieldAndAstParents, fieldNormalizedField);
231+
updateCoordinatedToNFMap(coordinatesToNormalizedFields, childENF);
196232

197-
buildFieldWithChildren(child,
233+
buildFieldWithChildren(childENF,
198234
childFieldAndAstParents,
199235
fieldCollectorNormalizedQueryParams,
200236
fieldNormalizedField,
@@ -204,6 +240,10 @@ private void buildFieldWithChildren(ExecutableNormalizedField field,
204240
}
205241
}
206242

243+
private static MergedField newMergedField(ImmutableList<FieldAndAstParent> fieldAndAstParents) {
244+
return MergedField.newMergedField(map(fieldAndAstParents, fieldAndAstParent -> fieldAndAstParent.field)).build();
245+
}
246+
207247
private void updateFieldToNFMap(ExecutableNormalizedField executableNormalizedField,
208248
ImmutableList<FieldAndAstParent> mergedField,
209249
ImmutableListMultimap.Builder<Field, ExecutableNormalizedField> fieldToNormalizedField) {
@@ -395,7 +435,7 @@ private void collectFromSelectionSet(FieldCollectorNormalizedQueryParams paramet
395435
} else if (selection instanceof InlineFragment) {
396436
collectInlineFragment(parameters, result, (InlineFragment) selection, possibleObjects, astTypeCondition);
397437
} else if (selection instanceof FragmentSpread) {
398-
collectFragmentSpread(parameters, result, (FragmentSpread) selection, possibleObjects, astTypeCondition);
438+
collectFragmentSpread(parameters, result, (FragmentSpread) selection, possibleObjects);
399439
}
400440
}
401441
}
@@ -423,8 +463,7 @@ public boolean isConcrete() {
423463
private void collectFragmentSpread(FieldCollectorNormalizedQueryParams parameters,
424464
List<CollectedField> result,
425465
FragmentSpread fragmentSpread,
426-
Set<GraphQLObjectType> possibleObjects,
427-
GraphQLCompositeType astTypeCondition
466+
Set<GraphQLObjectType> possibleObjects
428467
) {
429468
if (!conditionalNodes.shouldInclude(parameters.getCoercedVariableValues(), fragmentSpread.getDirectives())) {
430469
return;
@@ -508,8 +547,8 @@ private ImmutableSet<GraphQLObjectType> resolvePossibleObjects(GraphQLCompositeT
508547
} else if (type instanceof GraphQLInterfaceType) {
509548
return ImmutableSet.copyOf(graphQLSchema.getImplementations((GraphQLInterfaceType) type));
510549
} else if (type instanceof GraphQLUnionType) {
511-
List types = ((GraphQLUnionType) type).getTypes();
512-
return ImmutableSet.copyOf(types);
550+
List<GraphQLNamedOutputType> unionTypes = ((GraphQLUnionType) type).getTypes();
551+
return ImmutableSet.copyOf(ImmutableKit.map(unionTypes, GraphQLObjectType.class::cast));
513552
} else {
514553
return assertShouldNeverHappen();
515554
}

0 commit comments

Comments
 (0)