Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import graphql.Assert;
import graphql.Directives;
import graphql.ExperimentalApi;
import graphql.GraphQLContext;
import graphql.PublicApi;
Expand Down Expand Up @@ -49,6 +50,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -532,13 +534,14 @@ private void buildEnfsRecursively(@Nullable ExecutableNormalizedField executable
}
Set<GraphQLObjectType> possibleObjects;
List<CollectedField> collectedFields;
Map<Directive, NormalizedDeferredExecution> deferredExecutions = new IdentityHashMap<>();

// special handling for the root selection Set
if (executableNormalizedField == null) {
GraphQLObjectType rootType = SchemaUtil.getOperationRootType(graphQLSchema, operationDefinition);
possibleObjects = ImmutableSet.of(rootType);
collectedFields = new ArrayList<>();
collectFromSelectionSet(operationDefinition.getSelectionSet(), collectedFields, rootType, possibleObjects, null);
collectFromSelectionSet(operationDefinition.getSelectionSet(), collectedFields, rootType, possibleObjects, null, deferredExecutions);
} else {
List<GraphQLFieldDefinition> fieldDefs = executableNormalizedField.getFieldDefinitions(graphQLSchema);
possibleObjects = resolvePossibleObjects(fieldDefs);
Expand All @@ -560,7 +563,8 @@ private void buildEnfsRecursively(@Nullable ExecutableNormalizedField executable
collectedFields,
selectionSetType,
possibleObjects,
null
null,
deferredExecutions
);
}
}
Expand Down Expand Up @@ -782,22 +786,24 @@ private void collectFromSelectionSet(SelectionSet selectionSet,
List<CollectedField> result,
GraphQLCompositeType astTypeCondition,
Set<GraphQLObjectType> possibleObjects,
NormalizedDeferredExecution deferredExecution
NormalizedDeferredExecution deferredExecution,
Map<Directive, NormalizedDeferredExecution> deferredExecutions
) {
for (Selection<?> selection : selectionSet.getSelections()) {
if (selection instanceof Field) {
collectField(result, (Field) selection, possibleObjects, astTypeCondition, deferredExecution);
} else if (selection instanceof InlineFragment) {
collectInlineFragment(result, (InlineFragment) selection, possibleObjects, astTypeCondition);
collectInlineFragment(result, (InlineFragment) selection, possibleObjects, astTypeCondition, deferredExecutions);
} else if (selection instanceof FragmentSpread) {
collectFragmentSpread(result, (FragmentSpread) selection, possibleObjects);
collectFragmentSpread(result, (FragmentSpread) selection, possibleObjects, deferredExecutions);
}
}
}

private void collectFragmentSpread(List<CollectedField> result,
FragmentSpread fragmentSpread,
Set<GraphQLObjectType> possibleObjects
Set<GraphQLObjectType> possibleObjects,
Map<Directive, NormalizedDeferredExecution> deferredExecutions
) {
if (!conditionalNodes.shouldInclude(fragmentSpread,
this.coercedVariableValues.toMap(),
Expand All @@ -818,15 +824,17 @@ private void collectFragmentSpread(List<CollectedField> result,

NormalizedDeferredExecution newDeferredExecution = buildDeferredExecution(
fragmentSpread.getDirectives(),
newPossibleObjects);
newPossibleObjects,
deferredExecutions);

collectFromSelectionSet(fragmentDefinition.getSelectionSet(), result, newAstTypeCondition, newPossibleObjects, newDeferredExecution);
collectFromSelectionSet(fragmentDefinition.getSelectionSet(), result, newAstTypeCondition, newPossibleObjects, newDeferredExecution, deferredExecutions);
}

private void collectInlineFragment(List<CollectedField> result,
InlineFragment inlineFragment,
Set<GraphQLObjectType> possibleObjects,
GraphQLCompositeType astTypeCondition
GraphQLCompositeType astTypeCondition,
Map<Directive, NormalizedDeferredExecution> deferredExecutions
) {
if (!conditionalNodes.shouldInclude(inlineFragment, this.coercedVariableValues.toMap(), this.graphQLSchema, this.options.graphQLContext)) {
return;
Expand All @@ -842,24 +850,39 @@ private void collectInlineFragment(List<CollectedField> result,

NormalizedDeferredExecution newDeferredExecution = buildDeferredExecution(
inlineFragment.getDirectives(),
newPossibleObjects
newPossibleObjects,
deferredExecutions
);

collectFromSelectionSet(inlineFragment.getSelectionSet(), result, newAstTypeCondition, newPossibleObjects, newDeferredExecution);
collectFromSelectionSet(inlineFragment.getSelectionSet(), result, newAstTypeCondition, newPossibleObjects, newDeferredExecution, deferredExecutions);
}

private @Nullable NormalizedDeferredExecution buildDeferredExecution(
List<Directive> directives,
Set<GraphQLObjectType> newPossibleObjects) {
Set<GraphQLObjectType> newPossibleObjects,
Map<Directive, NormalizedDeferredExecution> deferredExecutions) {
if (!options.deferSupport) {
return null;
}
Directive deferDirective = NodeUtil.findNodeByName(directives, Directives.DeferDirective.getName());
if (deferDirective == null) {
return null;
}
NormalizedDeferredExecution deferredExecution = deferredExecutions.get(deferDirective);
if (deferredExecution != null) {
deferredExecution.getPossibleTypes().addAll(newPossibleObjects);
return deferredExecution;
}

return IncrementalUtils.createDeferredExecution(
deferredExecution = IncrementalUtils.createDeferredExecution(
this.coercedVariableValues.toMap(),
directives,
(label) -> new NormalizedDeferredExecution(label, newPossibleObjects)
(label) -> new NormalizedDeferredExecution(label, new LinkedHashSet<>(newPossibleObjects))
);
if (deferredExecution != null) {
deferredExecutions.put(deferDirective, deferredExecution);
}
return deferredExecution;
}

private void collectField(List<CollectedField> result,
Expand Down
Loading
Loading