Skip to content

Commit 14ca8c5

Browse files
committed
Dedup some code and add comment explaining some isConditional code
1 parent d24b0d0 commit 14ca8c5

5 files changed

Lines changed: 366 additions & 78 deletions

File tree

‎src/main/java/graphql/execution/Execution.java‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static graphql.execution.ExecutionContextBuilder.newExecutionContextBuilder;
3131
import static graphql.execution.ExecutionStepInfo.newExecutionStepInfo;
3232
import static graphql.execution.ExecutionStrategyParameters.newParameters;
33+
import static graphql.execution.nextgen.Common.getOperationRootType;
3334
import static graphql.language.OperationDefinition.Operation.MUTATION;
3435
import static graphql.language.OperationDefinition.Operation.QUERY;
3536
import static graphql.language.OperationDefinition.Operation.SUBSCRIPTION;
@@ -175,30 +176,4 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe
175176

176177
return result;
177178
}
178-
179-
180-
private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition operationDefinition) {
181-
OperationDefinition.Operation operation = operationDefinition.getOperation();
182-
if (operation == MUTATION) {
183-
GraphQLObjectType mutationType = graphQLSchema.getMutationType();
184-
if (mutationType == null) {
185-
throw new MissingRootTypeException("Schema is not configured for mutations.", operationDefinition.getSourceLocation());
186-
}
187-
return mutationType;
188-
} else if (operation == QUERY) {
189-
GraphQLObjectType queryType = graphQLSchema.getQueryType();
190-
if (queryType == null) {
191-
throw new MissingRootTypeException("Schema does not define the required query root type.", operationDefinition.getSourceLocation());
192-
}
193-
return queryType;
194-
} else if (operation == SUBSCRIPTION) {
195-
GraphQLObjectType subscriptionType = graphQLSchema.getSubscriptionType();
196-
if (subscriptionType == null) {
197-
throw new MissingRootTypeException("Schema is not configured for subscriptions.", operationDefinition.getSourceLocation());
198-
}
199-
return subscriptionType;
200-
} else {
201-
return assertShouldNeverHappen("Unhandled case. An extra operation enum has been added without code support");
202-
}
203-
}
204179
}

‎src/main/java/graphql/execution/nextgen/Common.java‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import graphql.Internal;
44
import graphql.execution.MissingRootTypeException;
55
import graphql.language.OperationDefinition;
6+
import graphql.language.SourceLocation;
67
import graphql.schema.GraphQLObjectType;
78
import graphql.schema.GraphQLSchema;
9+
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
811

912
import java.util.Optional;
1013

@@ -19,21 +22,30 @@
1922
@Deprecated
2023
@Internal
2124
public class Common {
22-
2325
public static GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition operationDefinition) {
24-
OperationDefinition.Operation operation = operationDefinition.getOperation();
26+
return getOperationRootType(graphQLSchema, operationDefinition.getOperation(), operationDefinition.getSourceLocation());
27+
}
28+
29+
public static GraphQLObjectType getOperationRootType(@NotNull GraphQLSchema graphQLSchema,
30+
@NotNull OperationDefinition.Operation operation) {
31+
return getOperationRootType(graphQLSchema, operation, null);
32+
}
33+
34+
public static GraphQLObjectType getOperationRootType(@NotNull GraphQLSchema graphQLSchema,
35+
@NotNull OperationDefinition.Operation operation,
36+
@Nullable SourceLocation sourceLocation) {
2537
if (operation == MUTATION) {
2638
GraphQLObjectType mutationType = graphQLSchema.getMutationType();
2739
return Optional.ofNullable(mutationType)
28-
.orElseThrow(() -> new MissingRootTypeException("Schema is not configured for mutations.", operationDefinition.getSourceLocation()));
40+
.orElseThrow(() -> new MissingRootTypeException("Schema is not configured for mutations.", sourceLocation));
2941
} else if (operation == QUERY) {
3042
GraphQLObjectType queryType = graphQLSchema.getQueryType();
3143
return Optional.ofNullable(queryType)
32-
.orElseThrow(() -> new MissingRootTypeException("Schema does not define the required query root type.", operationDefinition.getSourceLocation()));
44+
.orElseThrow(() -> new MissingRootTypeException("Schema does not define the required query root type.", sourceLocation));
3345
} else if (operation == SUBSCRIPTION) {
3446
GraphQLObjectType subscriptionType = graphQLSchema.getSubscriptionType();
3547
return Optional.ofNullable(subscriptionType)
36-
.orElseThrow(() -> new MissingRootTypeException("Schema is not configured for subscriptions.", operationDefinition.getSourceLocation()));
48+
.orElseThrow(() -> new MissingRootTypeException("Schema is not configured for subscriptions.", sourceLocation));
3749
} else {
3850
return assertShouldNeverHappen("Unhandled case. An extra operation enum has been added without code support");
3951
}

‎src/main/java/graphql/normalized/ExecutableNormalizedField.java‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.collect.ImmutableList;
44
import com.google.common.collect.ImmutableMap;
5+
import graphql.Assert;
56
import graphql.Internal;
67
import graphql.Mutable;
78
import graphql.collect.ImmutableKit;
@@ -15,6 +16,7 @@
1516
import graphql.schema.GraphQLType;
1617
import graphql.schema.GraphQLUnionType;
1718
import graphql.util.FpKit;
19+
import org.dataloader.impl.Assertions;
1820
import org.jetbrains.annotations.NotNull;
1921
import org.jetbrains.annotations.Nullable;
2022

@@ -132,27 +134,34 @@ public boolean isConditional(GraphQLSchema schema, @Nullable String parentOutput
132134

133135
GraphQLType parentOutputType = schema.getType(parentOutputTypeName);
134136

135-
// Is not conditional if field exists in interface type, and we have specified all impls
136137
if (parentOutputType instanceof GraphQLInterfaceType) {
137138
GraphQLInterfaceType parentOutputTypeAsInterface = (GraphQLInterfaceType) parentOutputType;
138139
List<GraphQLObjectType> interfaceImpls = schema.getImplementations(parentOutputTypeAsInterface);
139-
if (interfaceImpls.size() == objectTypeNames.size()) {
140-
return parentOutputTypeAsInterface.getField(this.fieldName) == null
141-
&& !this.fieldName.equals(Introspection.TypeNameMetaFieldDef.getName());
142-
} else {
143-
return true;
140+
141+
// __typename
142+
if (this.fieldName.equals(Introspection.TypeNameMetaFieldDef.getName()) && interfaceImpls.size() == objectTypeNames.size()) {
143+
return false; // Not conditional
144+
}
145+
146+
// If field does not exist on the output type, it is conditional
147+
if (parentOutputTypeAsInterface.getField(fieldName) == null) {
148+
return true; // Conditional
144149
}
150+
151+
return interfaceImpls.size() != objectTypeNames.size();
145152
} else if (parentOutputType instanceof GraphQLUnionType) {
146153
GraphQLUnionType parentOutputTypeAsUnion = (GraphQLUnionType) parentOutputType;
147-
if (objectTypeNames.size() == parentOutputTypeAsUnion.getTypes().size()) {
148-
return !this.fieldName.equals(Introspection.TypeNameMetaFieldDef.getName());
154+
155+
// __typename is the only field in a union type that CAN be NOT conditional
156+
if (this.fieldName.equals(Introspection.TypeNameMetaFieldDef.getName()) && objectTypeNames.size() == parentOutputTypeAsUnion.getTypes().size()) {
157+
return false; // Not conditional
149158
} else {
150-
return true;
159+
return true; // Conditional
151160
}
152161
}
153162

154163
if (objectTypeNames.size() > 1) {
155-
return true;
164+
return true; // Conditional
156165
}
157166

158167
return parentOutputType != getOneObjectType(schema);

‎src/main/java/graphql/normalized/ExecutableNormalizedOperationToAstCompiler.java‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import static graphql.Assert.assertNotNull;
3333
import static graphql.collect.ImmutableKit.map;
34+
import static graphql.execution.nextgen.Common.getOperationRootType;
3435
import static graphql.language.Argument.newArgument;
3536
import static graphql.language.Field.newField;
3637
import static graphql.language.InlineFragment.newInlineFragment;
@@ -66,8 +67,7 @@ public static CompilerResult compileToDocument(@NotNull GraphQLSchema schema,
6667
@Nullable String operationName,
6768
@NotNull List<ExecutableNormalizedField> topLevelFields,
6869
@Nullable VariablePredicate variablePredicate) {
69-
GraphQLObjectType operationType = assertNotNull(getOperationType(schema, operationKind),
70-
() -> "Schema does not support " + operationKind.name().toLowerCase() + " operations");
70+
GraphQLObjectType operationType = getOperationRootType(schema, operationKind);
7171

7272
VariableAccumulator variableAccumulator = new VariableAccumulator(variablePredicate);
7373
List<Selection<?>> selections = subselectionsForNormalizedField(schema, operationType.getName(), topLevelFields, variableAccumulator);
@@ -89,7 +89,7 @@ public static CompilerResult compileToDocument(@NotNull GraphQLSchema schema,
8989
}
9090

9191
private static List<Selection<?>> subselectionsForNormalizedField(GraphQLSchema schema,
92-
@Nullable String parentOutputType,
92+
@NotNull String parentOutputType,
9393
List<ExecutableNormalizedField> executableNormalizedFields,
9494
VariableAccumulator variableAccumulator) {
9595
ImmutableList.Builder<Selection<?>> selections = ImmutableList.builder();
@@ -240,19 +240,4 @@ private static GraphQLFieldDefinition getFieldDefinition(GraphQLSchema schema,
240240
ExecutableNormalizedField nf) {
241241
return schema.getFieldDefinition(coordinates(parentType, nf.getName()));
242242
}
243-
244-
@Nullable
245-
private static GraphQLObjectType getOperationType(@NotNull GraphQLSchema schema,
246-
@NotNull OperationDefinition.Operation operationKind) {
247-
switch (operationKind) {
248-
case QUERY:
249-
return schema.getQueryType();
250-
case MUTATION:
251-
return schema.getMutationType();
252-
case SUBSCRIPTION:
253-
return schema.getSubscriptionType();
254-
}
255-
256-
return Assert.assertShouldNeverHappen("Unknown operation kind " + operationKind);
257-
}
258243
}

0 commit comments

Comments
 (0)