Skip to content

Commit fe422ea

Browse files
authored
Tklovett builder consistency (#524)
* Use chaining for all Builder parameters * Fixed return statements Merge branch 'builder-consistency' of https://github.com/tklovett/graphql-java into tklovett-builder-consistency # Conflicts: # src/main/java/graphql/execution/Execution.java
1 parent 3a7cf5b commit fe422ea

File tree

8 files changed

+133
-51
lines changed

8 files changed

+133
-51
lines changed

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

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import graphql.ExecutionInput;
55
import graphql.ExecutionResult;
66
import graphql.ExecutionResultImpl;
7-
import graphql.GraphQLException;
87
import graphql.Internal;
98
import graphql.MutationNotSupportedError;
109
import graphql.execution.instrumentation.Instrumentation;
@@ -20,6 +19,7 @@
2019
import java.util.List;
2120
import java.util.Map;
2221

22+
import static graphql.Assert.assertShouldNeverHappen;
2323
import static graphql.execution.ExecutionStrategyParameters.newParameters;
2424
import static graphql.execution.TypeInfo.newTypeInfo;
2525
import static graphql.language.OperationDefinition.Operation.MUTATION;
@@ -43,37 +43,25 @@ public Execution(ExecutionStrategy queryStrategy, ExecutionStrategy mutationStra
4343
}
4444

4545
public ExecutionResult execute(Document document, GraphQLSchema graphQLSchema, ExecutionId executionId, ExecutionInput executionInput) {
46-
final Object context = executionInput.getContext();
47-
final Object root = executionInput.getRoot();
48-
final String operationName = executionInput.getOperationName();
49-
final Map<String, Object> variables = executionInput.getVariables();
5046

51-
ExecutionContextBuilder executionContextBuilder = new ExecutionContextBuilder(new ValuesResolver(), instrumentation);
52-
ExecutionContext executionContext = executionContextBuilder
47+
ExecutionContext executionContext = new ExecutionContextBuilder()
48+
.valuesResolver(new ValuesResolver())
49+
.instrumentation(instrumentation)
5350
.executionId(executionId)
54-
.build(graphQLSchema, queryStrategy, mutationStrategy, subscriptionStrategy, context, root, document, operationName, variables);
55-
return executeOperation(executionContext, root, executionContext.getOperationDefinition());
56-
}
57-
58-
private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition.Operation operation) {
59-
if (operation == MUTATION) {
60-
return graphQLSchema.getMutationType();
61-
62-
} else if (operation == QUERY) {
63-
return graphQLSchema.getQueryType();
64-
65-
} else if (operation == SUBSCRIPTION) {
66-
return graphQLSchema.getSubscriptionType();
67-
68-
} else {
69-
throw new GraphQLException("Unhandled case. An extra operation enum has been added without code support");
70-
}
51+
.graphQLSchema(graphQLSchema)
52+
.queryStrategy(queryStrategy)
53+
.mutationStrategy(mutationStrategy)
54+
.subscriptionStrategy(subscriptionStrategy)
55+
.context(executionInput.getContext())
56+
.root(executionInput.getRoot())
57+
.document(document)
58+
.operationName(executionInput.getOperationName())
59+
.variables(executionInput.getVariables())
60+
.build();
61+
return executeOperation(executionContext, executionInput.getRoot(), executionContext.getOperationDefinition());
7162
}
7263

73-
private ExecutionResult executeOperation(
74-
ExecutionContext executionContext,
75-
Object root,
76-
OperationDefinition operationDefinition) {
64+
private ExecutionResult executeOperation(ExecutionContext executionContext, Object root, OperationDefinition operationDefinition) {
7765

7866
InstrumentationContext<ExecutionResult> dataFetchCtx = instrumentation.beginDataFetch(new InstrumentationDataFetchParameters(executionContext));
7967

@@ -89,7 +77,9 @@ private ExecutionResult executeOperation(
8977
return new ExecutionResultImpl(Collections.singletonList(new MutationNotSupportedError()));
9078
}
9179

92-
FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters(executionContext.getGraphQLSchema(), operationRootType)
80+
FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters()
81+
.schema(executionContext.getGraphQLSchema())
82+
.objectType(operationRootType)
9383
.fragments(executionContext.getFragmentsByName())
9484
.variables(executionContext.getVariables())
9585
.build();
@@ -130,4 +120,16 @@ private ExecutionResult executeOperation(
130120
dataFetchCtx.onEnd(result);
131121
return result;
132122
}
123+
124+
private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition.Operation operation) {
125+
if (operation == MUTATION) {
126+
return graphQLSchema.getMutationType();
127+
} else if (operation == QUERY) {
128+
return graphQLSchema.getQueryType();
129+
} else if (operation == SUBSCRIPTION) {
130+
return graphQLSchema.getSubscriptionType();
131+
} else {
132+
return assertShouldNeverHappen("Unhandled case. An extra operation enum has been added without code support");
133+
}
134+
}
133135
}

src/main/java/graphql/execution/ExecutionContextBuilder.java

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,77 @@ public class ExecutionContextBuilder {
1818
private ValuesResolver valuesResolver;
1919
private Instrumentation instrumentation;
2020
private ExecutionId executionId;
21+
private GraphQLSchema graphQLSchema;
22+
private ExecutionStrategy queryStrategy;
23+
private ExecutionStrategy mutationStrategy;
24+
private ExecutionStrategy subscriptionStrategy;
25+
private Object context;
26+
private Object root;
27+
private Document document;
28+
private String operationName;
29+
private Map<String, Object> variables;
2130

22-
public ExecutionContextBuilder(ValuesResolver valuesResolver, Instrumentation instrumentation) {
31+
public ExecutionContextBuilder valuesResolver(ValuesResolver valuesResolver) {
2332
this.valuesResolver = valuesResolver;
24-
this.instrumentation = instrumentation;
33+
return this;
2534
}
2635

36+
public ExecutionContextBuilder instrumentation(Instrumentation instrumentation) {
37+
this.instrumentation = instrumentation;
38+
return this;
39+
}
2740

2841
public ExecutionContextBuilder executionId(ExecutionId executionId) {
2942
this.executionId = executionId;
3043
return this;
3144
}
3245

33-
public ExecutionContext build(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy, ExecutionStrategy subscriptionStrategy, Object context, Object root, Document document, String operationName, Map<String, Object> variables) {
46+
public ExecutionContextBuilder graphQLSchema(GraphQLSchema graphQLSchema) {
47+
this.graphQLSchema = graphQLSchema;
48+
return this;
49+
}
50+
51+
public ExecutionContextBuilder queryStrategy(ExecutionStrategy queryStrategy) {
52+
this.queryStrategy = queryStrategy;
53+
return this;
54+
}
55+
56+
public ExecutionContextBuilder mutationStrategy(ExecutionStrategy mutationStrategy) {
57+
this.mutationStrategy = mutationStrategy;
58+
return this;
59+
}
60+
61+
public ExecutionContextBuilder subscriptionStrategy(ExecutionStrategy subscriptionStrategy) {
62+
this.subscriptionStrategy = subscriptionStrategy;
63+
return this;
64+
}
65+
66+
public ExecutionContextBuilder context(Object context) {
67+
this.context = context;
68+
return this;
69+
}
70+
71+
public ExecutionContextBuilder root(Object root) {
72+
this.root = root;
73+
return this;
74+
}
75+
76+
public ExecutionContextBuilder document(Document document) {
77+
this.document = document;
78+
return this;
79+
}
80+
81+
public ExecutionContextBuilder operationName(String operationName) {
82+
this.operationName = operationName;
83+
return this;
84+
}
85+
86+
public ExecutionContextBuilder variables(Map<String, Object> variables) {
87+
this.variables = variables;
88+
return this;
89+
}
90+
91+
public ExecutionContext build() {
3492
// preconditions
3593
assertNotNull(executionId, "You must provide a query identifier");
3694

src/main/java/graphql/execution/ExecutionStrategy.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ protected ExecutionResult completeValue(ExecutionContext executionContext, Execu
176176
resolvedType = (GraphQLObjectType) fieldType;
177177
}
178178

179-
FieldCollectorParameters collectorParameters = newParameters(executionContext.getGraphQLSchema(), resolvedType)
179+
FieldCollectorParameters collectorParameters = newParameters()
180+
.schema(executionContext.getGraphQLSchema())
181+
.objectType(resolvedType)
180182
.fragments(executionContext.getFragmentsByName())
181183
.variables(executionContext.getVariables())
182184
.build();

src/main/java/graphql/execution/ExecutionStrategyParameters.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public ExecutionStrategyParameters transform(Consumer<Builder> builderConsumer)
6161
return builder.build();
6262
}
6363

64+
@Override
65+
public String toString() {
66+
return String.format("ExecutionStrategyParameters { path=%s, typeInfo=%s, source=%s, fields=%s }",
67+
path, typeInfo, source, fields);
68+
}
6469

6570
public static Builder newParameters() {
6671
return new Builder();
@@ -70,12 +75,6 @@ public static Builder newParameters(ExecutionStrategyParameters oldParameters) {
7075
return new Builder(oldParameters);
7176
}
7277

73-
@Override
74-
public String toString() {
75-
return String.format("ExecutionStrategyParameters { path=%s, typeInfo=%s, source=%s, fields=%s }",
76-
path, typeInfo, source, fields);
77-
}
78-
7978
public static class Builder {
8079
TypeInfo typeInfo;
8180
Object source;
@@ -84,9 +83,15 @@ public static class Builder {
8483
NonNullableFieldValidator nonNullableFieldValidator;
8584
ExecutionPath path = ExecutionPath.rootPath();
8685

86+
/**
87+
* @see ExecutionStrategyParameters#newParameters()
88+
*/
8789
private Builder() {
8890
}
8991

92+
/**
93+
* @see ExecutionStrategyParameters#newParameters(ExecutionStrategyParameters)
94+
*/
9095
private Builder(ExecutionStrategyParameters oldParameters) {
9196
this.typeInfo = oldParameters.typeInfo;
9297
this.source = oldParameters.source;

src/main/java/graphql/execution/FieldCollectorParameters.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ private FieldCollectorParameters(GraphQLSchema graphQLSchema, Map<String, Object
3737
this.objectType = objectType;
3838
}
3939

40-
public static Builder newParameters(GraphQLSchema graphQLSchema, GraphQLObjectType objectType) {
41-
Assert.assertNotNull(graphQLSchema, "You must provide a schema");
42-
Assert.assertNotNull(objectType, "You must provide an object type");
43-
return new Builder().schema(graphQLSchema).objectType(objectType);
40+
public static Builder newParameters() {
41+
return new Builder();
4442
}
4543

4644
public static class Builder {
@@ -49,6 +47,13 @@ public static class Builder {
4947
private Map<String, Object> variables = new LinkedHashMap<>();
5048
private GraphQLObjectType objectType;
5149

50+
/**
51+
* @see FieldCollectorParameters#newParameters()
52+
*/
53+
private Builder() {
54+
55+
}
56+
5257
public Builder schema(GraphQLSchema graphQLSchema) {
5358
this.graphQLSchema = graphQLSchema;
5459
return this;
@@ -70,6 +75,8 @@ public Builder variables(Map<String, Object> variables) {
7075
}
7176

7277
public FieldCollectorParameters build() {
78+
Assert.assertNotNull(graphQLSchema, "You must provide a schema");
79+
Assert.assertNotNull(objectType, "You must provide an object type");
7380
return new FieldCollectorParameters(graphQLSchema, variables, fragmentsByName, objectType);
7481
}
7582

src/main/java/graphql/execution/TypeInfo.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ public TypeInfo asType(GraphQLType type) {
5555
return new TypeInfo(unwrap(type), this.parentType, this.typeIsNonNull);
5656
}
5757

58-
public static Builder newTypeInfo() {
59-
return new Builder();
60-
}
61-
6258
private static GraphQLType unwrap(GraphQLType type) {
6359
// its possible to have non nulls wrapping non nulls of things but it must end at some point
6460
while (type instanceof GraphQLNonNull) {
@@ -67,17 +63,26 @@ private static GraphQLType unwrap(GraphQLType type) {
6763
return type;
6864
}
6965

70-
7166
@Override
7267
public String toString() {
7368
return String.format("TypeInfo { nonnull=%s, type=%s, parentType=%s }",
7469
typeIsNonNull, type, parentType);
7570
}
7671

72+
public static TypeInfo.Builder newTypeInfo() {
73+
return new Builder();
74+
}
75+
7776
public static class Builder {
7877
GraphQLType type;
7978
TypeInfo parentType;
8079

80+
/**
81+
* @see TypeInfo#newTypeInfo()
82+
*/
83+
private Builder() {
84+
}
85+
8186
public Builder type(GraphQLType type) {
8287
this.type = type;
8388
return this;

src/main/java/graphql/execution/batched/BatchedExecutionStrategy.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ private boolean isNonNull(GraphQLType fieldType) {
217217
private Map<String, List<Field>> getChildFields(ExecutionContext executionContext, GraphQLObjectType resolvedType,
218218
List<Field> fields) {
219219

220-
FieldCollectorParameters collectorParameters = newParameters(executionContext.getGraphQLSchema(), resolvedType)
220+
FieldCollectorParameters collectorParameters = newParameters()
221+
.schema(executionContext.getGraphQLSchema())
222+
.objectType(resolvedType)
221223
.fragments(executionContext.getFragmentsByName())
222224
.variables(executionContext.getVariables())
223225
.build();

src/main/java/graphql/schema/DataFetchingFieldSelectionSetImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public static DataFetchingFieldSelectionSet newCollector(ExecutionContext execut
3030
private DataFetchingFieldSelectionSetImpl(ExecutionContext executionContext, GraphQLObjectType fieldType, List<Field> fields) {
3131
this.fields = fields;
3232
this.fieldCollector = new FieldCollector();
33-
this.parameters = FieldCollectorParameters.
34-
newParameters(executionContext.getGraphQLSchema(), fieldType)
33+
this.parameters = FieldCollectorParameters.newParameters()
34+
.schema(executionContext.getGraphQLSchema())
35+
.objectType(fieldType)
3536
.fragments(executionContext.getFragmentsByName())
3637
.variables(executionContext.getVariables())
3738
.build();

0 commit comments

Comments
 (0)