8000 Defer execution refactoring after first merge by felipe-gdr · Pull Request #3450 · graphql-java/graphql-java · GitHub
[go: up one dir, main page]

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
8000
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package graphql.execution.incremental;

import graphql.ExperimentalApi;
import graphql.normalized.incremental.DeferExecution;
import graphql.normalized.incremental.NormalizedDeferredExecution;

import javax.annotation.Nullable;

/**
* Represents details about the defer execution that can be associated with a {@link graphql.execution.MergedField}.
* <p>
* This representation is used during graphql execution. Check {@link DeferExecution}
* This representation is used during graphql execution. Check {@link NormalizedDeferredExecution}
* for the normalized representation of @defer.
*/
@ExperimentalApi
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/normalized/ENFMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ && isFieldInSharedInterface(field, fieldInGroup, schema)

if (deferSupport) {
// Move defer executions from removed field into the merged field's entry
first.addDeferExecutions(next.getDeferExecutions());
first.addDeferredExecutions(next.getDeferredExecutions());
}
}
first.setObjectTypeNames(mergedObjects);
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/graphql/normalized/ExecutableNormalizedField.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import graphql.collect.ImmutableKit;
import graphql.introspection.Introspection;
import graphql.language.Argument;
import graphql.normalized.incremental.DeferExecution;
import graphql.normalized.incremental.NormalizedDeferredExecution;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLNamedOutputType;
Expand Down Expand Up @@ -66,7 +66,7 @@ public class ExecutableNormalizedField {
private final int level;

// Mutable List on purpose: it is modified after creation
private final LinkedHashSet<DeferExecution> deferExecutions;
private final LinkedHashSet<NormalizedDeferredExecution> deferredExecutions;

private ExecutableNormalizedField(Builder builder) {
this.alias = builder.alias;
Expand All @@ -78,7 +78,7 @@ private ExecutableNormalizedField(Builder builder) {
this.children = builder.children;
this.level = builder.level;
this.parent = builder.parent;
this.deferExecutions = builder.deferExecutions;
this.deferredExecutions = builder.deferredExecutions;
}

/**
Expand Down Expand Up @@ -262,13 +262,13 @@ public void clearChildren() {
}

@Internal
public void setDeferExecutions(Collection<DeferExecution> deferExecutions) {
this.deferExecutions.clear();
this.deferExecutions.addAll(deferExecutions);
public void setDeferredExecutions(Collection<NormalizedDeferredExecution> deferredExecutions) {
this.deferredExecutions.clear();
this.deferredExecutions.addAll(deferredExecutions);
}

public void addDeferExecutions(Collection<DeferExecution> deferExecutions) {
this.deferExecutions.addAll(deferExecutions);
public void addDeferredExecutions(Collection<NormalizedDeferredExecution> deferredExecutions) {
this.deferredExecutions.addAll(deferredExecutions);
}

/**
Expand Down Expand Up @@ -477,12 +477,12 @@ public ExecutableNormalizedField getParent() {
}

/**
* @return the {@link DeferExecution}s associated with this {@link ExecutableNormalizedField}.
* @see DeferExecution
* @return the {@link NormalizedDeferredExecution}s associated with this {@link ExecutableNormalizedField}.
* @see NormalizedDeferredExecution
*/
@ExperimentalApi
public LinkedHashSet<DeferExecution> getDeferExecutions() {
return deferExecutions;
public LinkedHashSet<NormalizedDeferredExecution> getDeferredExecutions() {
return deferredExecutions;
}

@Internal
Expand Down Expand Up @@ -612,7 +612,7 @@ public static class Builder {
private LinkedHashMap<String, Object> resolvedArguments = new LinkedHashMap<>();
private ImmutableList<Argument> astArguments = ImmutableKit.emptyList();

private LinkedHashSet<DeferExecution> deferExecutions = new LinkedHashSet<>();
private LinkedHashSet<NormalizedDeferredExecution> deferredExecutions = new LinkedHashSet<>();

private Builder() {
}
Expand All @@ -627,7 +627,7 @@ private Builder(ExecutableNormalizedField existing) {
this.children = new ArrayList<>(existing.children);
this.level = existing.getLevel();
this.parent = existing.getParent();
this.deferExecutions = existing.getDef 8000 erExecutions();
this.deferredExecutions = existing.getDeferredExecutions();
}

public Builder clearObjectTypesNames() {
Expand Down Expand Up @@ -683,8 +683,8 @@ public Builder parent(ExecutableNormalizedField parent) {
return this;
}

public Builder deferExecutions(LinkedHashSet<DeferExecution> deferExecutions) {
this.deferExecutions = deferExecutions;
public Builder deferredExecutions(LinkedHashSet<NormalizedDeferredExecution> deferredExecutions) {
this.deferredExecutions = deferredExecutions;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import graphql.execution.conditional.ConditionalNodes;
import graphql.execution.directives.QueryDirectives;
import graphql.execution.directives.QueryDirectivesImpl;
import graphql.execution.incremental.IncrementalUtils;
import graphql.introspection.Introspection;
import graphql.language.Directive;
import graphql.language.Document;
Expand All @@ -28,10 +29,8 @@
import graphql.language.OperationDefinition;
import graphql.language.Selection;
import graphql.language.SelectionSet;
import graphql.language.TypeName;
import graphql.language.VariableDefinition;
import graphql.normalized.incremental.DeferExecution;
import graphql.normalized.incremental.IncrementalNodes;
import graphql.normalized.incremental.NormalizedDeferredExecution;
import graphql.schema.FieldCoordinates;
import graphql.schema.GraphQLCompositeType;
import graphql.schema.GraphQLFieldDefinition;
Expand Down Expand Up @@ -191,7 +190,6 @@ public boolean getDeferSupport() {
}

private static final ConditionalNodes conditionalNodes = new ConditionalNodes();
private static final IncrementalNodes incrementalNodes = new IncrementalNodes();

private ExecutableNormalizedOperationFactory() {

Expand Down Expand Up @@ -568,7 +566,7 @@ private void createNFs(ImmutableList.Builder<ExecutableNormalizedField> nfListBu
nfListBuilder.add(nf);

if (this.options.deferSupport) {
nf.addDeferExecutions(fieldGroup.deferExecutions);
nf.addDeferredExecutions(fieldGroup.deferredExecutions);
}
}
if (commonParentsGroups.size() > 1) {
Expand Down Expand Up @@ -633,22 +631,22 @@ private List<CollectedFieldGroup> groupByCommonParentsNoDeferSupport(Collection<

private List<CollectedFieldGroup> groupByCommonParentsWithDeferSupport(Collection<CollectedField> fields) {
ImmutableSet.Builder<GraphQLObjectType> objectTypes = ImmutableSet.builder();
ImmutableSet.Builder<DeferExecution> deferExecutionsBuilder = ImmutableSet.builder();
ImmutableSet.Builder<NormalizedDeferredExecution> deferredExecutionsBuilder = ImmutableSet.builder();

for (CollectedField collectedField : fields) {
objectTypes.addAll(collectedField.objectTypes);

DeferExecution collectedDeferExecution = collectedField.deferExecution;
NormalizedDeferredExecution collectedDeferredExecution = collectedField.deferredExecution;

if (collectedDeferExecution != null) {
deferExecutionsBuilder.add(collectedDeferExecution);
if (collectedDeferredExecution != null) {
deferredExecutionsBuilder.add(collectedDeferredExecution);
}
}

Set<GraphQLObjectType> allRelevantObjects = objectTypes.build();
Set<DeferExecution> deferExecutions = deferExecutionsBuilder.build();
Set<NormalizedDeferredExecution> deferredExecutions = deferredExecutionsBuilder.build();

Set<String> duplicatedLabels = listDuplicatedLabels(deferExecutions);
Set<String> duplicatedLabels = listDuplicatedLabels(deferredExecutions);

if (!duplicatedLabels.isEmpty()) {
// Query validation should pick this up
Expand All @@ -657,33 +655,33 @@ private List<CollectedFieldGroup> groupByCommonParentsWithDeferSupport(Collectio

Map<GraphQLType, ImmutableList<CollectedField>> groupByAstParent = groupingBy(fields, fieldAndType -> fieldAndType.astTypeCondition);
if (groupByAstParent.size() == 1) {
return singletonList(new CollectedFieldGroup(ImmutableSet.copyOf(fields), allRelevantObjects, deferExecutions));
return singletonList(new CollectedFieldGroup(ImmutableSet.copyOf(fields), allRelevantObjects, deferredExecutions));
}

ImmutableList.Builder<CollectedFieldGroup> result = ImmutableList.builder();
for (GraphQLObjectType objectType : allRelevantObjects) {
Set<CollectedField> relevantFields = filterSet(fields, field -> field.objectTypes.contains(objectType));

Set<DeferExecution> filteredDeferExecutions = deferExecutions.stream()
Set<NormalizedDeferredExecution> filteredDeferredExecutions = deferredExecutions.stream()
.filter(filterExecutionsFromType(objectType))
.collect(toCollection(LinkedHashSet::new));

result.add(new CollectedFieldGroup(relevantFields, singleton(objectType), filteredDeferExecutions));
result.add(new CollectedFieldGroup(relevantFields, singleton(objectType), filteredDeferredExecutions));
}
return result.build();
}

private static Predicate<DeferExecution> filterExecutionsFromType(GraphQLObjectType objectType) {
private static Predicate<NormalizedDeferredExecution> filterExecutionsFromType(GraphQLObjectType objectType) {
String objectTypeName = objectType.getName();
return deferExecution -> deferExecution.getPossibleTypes()
return deferredExecution -> deferredExecution.getPossibleTypes()
.stream()
.map(GraphQLObjectType::getName)
.anyMatch(objectTypeName::equals);
}

private Set<String> listDuplicatedLabels(Collection<DeferExecution> deferExecutions) {
return deferExecutions.stream()
.map(DeferExecution::getLabel)
private Set<String> listDuplicatedLabels(Collection<NormalizedDeferredExecution> deferredExecutions) {
return deferredExecutions.stream()
.map(NormalizedDeferredExecution::getLabel)
.filter(Objects::nonNull)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
Expand All @@ -697,11 +695,11 @@ private void collectFromSelectionSet(SelectionSet selectionSet,
List<CollectedField> result,
GraphQLCompositeType astTypeCondition,
Set<GraphQLObjectType> possibleObjects,
DeferExecution deferExecution
NormalizedDeferredExecution deferredExecution
) {
for (Selection<?> selection : selectionSet.getSelections()) {
if (selection instanceof Field) {
collectField(result, (Field) selection, possibleObjects, astTypeCondition, deferExecution);
collectField(result, (Field) selection, possibleObjects, astTypeCondition, deferredExecution);
} else if (selection instanceof InlineFragment) {
collectInlineFragment(result, (InlineFragment) selection, possibleObjects, astTypeCondition);
} else if (selection instanceof FragmentSpread) {
Expand Down Expand Up @@ -731,12 +729,11 @@ private void collectFragmentSpread(List<CollectedField> result,
GraphQLCompositeType newAstTypeCondition = (GraphQLCompositeType) assertNotNull(this.graphQLSchema.getType(fragmentDefinition.getTypeCondition().getName()));
Set<GraphQLObjectType> newPossibleObjects = narrowDownPossibleObjects(possibleObjects, newAstTypeCondition);

DeferExecution newDeferExecution = buildDeferExecution(
NormalizedDeferredExecution newDeferredExecution = buildDeferredExecution(
fragmentSpread.getDirectives(),
fragmentDefinition.getTypeCondition(),
newPossibleObjects);

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

private void collectInlineFragment(List<CollectedField> result,
Expand All @@ -756,36 +753,33 @@ private void collectInlineFragment(List<CollectedField> result,

}

DeferExecution newDeferExecution = buildDeferExecution(
NormalizedDeferredExecution newDeferredExecution = buildDeferredExecution(
inlineFragment.getDirectives(),
inlineFragment.getTypeCondition(),
newPossibleObjects
);

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

private DeferExecution buildDeferExecution(
private NormalizedDeferredExecution buildDeferredExecution(
List<Directive> directives,
TypeName typeCondition,
Set<GraphQLObjectType> newPossibleObjects) {
if(!options.deferSupport) {
return null;
}

return incrementalNodes.createDeferExecution(
return IncrementalUtils.createDeferredExecution(
this.coercedVariableValues.toMap(),
directives,
typeCondition,
newPossibleObjects
(label) -> new NormalizedDeferredExecution(label, newPossibleObjects)
);
}

private void collectField(List<CollectedField> result,
Field field,
Set<GraphQLObjectType> possibleObjectTypes,
GraphQLCompositeType astTypeCondition,
DeferExecution deferExecution
NormalizedDeferredExecution deferredExecution
) {
if (!conditionalNodes.shouldInclude(field,
this.coercedVariableValues.toMap(),
Expand All @@ -797,7 +791,7 @@ private void collectField(List<CollectedField> result,
if (possibleObjectTypes.isEmpty()) {
return;
}
result.add(new CollectedField(field, possibleObjectTypes, astTypeCondition, deferExecution));
result.add(new CollectedField(field, possibleObjectTypes, astTypeCondition, deferredExecution));
}

private Set<GraphQLObjectType> narrowDownPossibleObjects(Set<GraphQLObjectType> currentOnes,
Expand Down Expand Up @@ -852,13 +846,13 @@ private static class CollectedField {
Field field;
Set<GraphQLObjectType> objectTypes;
GraphQLCompositeType astTypeCondition;
DeferExecution deferExecution;
NormalizedDeferredExecution deferredExecution;

public CollectedField(Field field, Set<GraphQLObjectType> objectTypes, GraphQLCompositeType astTypeCondition, DeferExecution deferExecution) {
public CollectedField(Field field, Set<GraphQLObjectType> objectTypes, GraphQLCompositeType astTypeCondition, NormalizedDeferredExecution deferredExecution) {
this.field = field;
this.objectTypes = objectTypes;
this.astTypeCondition = astTypeCondition;
this.deferExecution = deferExecution;
this.deferredExecution = deferredExecution;
}
}

Expand All @@ -885,12 +879,12 @@ private FieldAndAstParent(Field field, GraphQLCompositeType astParentType) {
private static class CollectedFieldGroup {
Set<GraphQLObjectType> objectTypes;
Set<CollectedField> fields;
Set<DeferExecution> deferExecutions;
Set<NormalizedDeferredExecution> deferredExecutions;

public CollectedFieldGroup(Set<CollectedField> fields, Set<GraphQLObjectType> objectTypes, Set<DeferExecution> deferExecutions) {
public CollectedFieldGroup(Set<CollectedField> fields, Set<GraphQLObjectType> objectTypes, Set<NormalizedDeferredExecution> deferredExecutions) {
this.fields = fields;
this.objectTypes = objectTypes;
this.deferExecutions = deferExecutions;
this.deferredExecutions = deferredExecutions;
}
}
}
Expand Down
Loading
0