Skip to content

Commit 1618e53

Browse files
committed
graphql-java#492 - builder for DataEnvironmentImpl - now in its own class
1 parent cfd1fe0 commit 1618e53

9 files changed

Lines changed: 162 additions & 122 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import static graphql.introspection.Introspection.SchemaMetaFieldDef;
4141
import static graphql.introspection.Introspection.TypeMetaFieldDef;
4242
import static graphql.introspection.Introspection.TypeNameMetaFieldDef;
43-
import static graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment;
43+
import static graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment;
4444

4545
@PublicSpi
4646
public abstract class ExecutionStrategy {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import java.util.Queue;
3636

3737
import static graphql.execution.FieldCollectorParameters.newParameters;
38-
import static graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment;
38+
import static graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment;
3939
import static java.util.Collections.singletonList;
4040

4141
/**

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import java.util.ArrayList;
88
import java.util.List;
99

10-
import static graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment;
11-
1210
/**
1311
* Given a normal data fetcher as a delegate,
1412
* uses that fetcher in a batched context by iterating through each source value and calling
@@ -29,9 +27,8 @@ public Object get(DataFetchingEnvironment environment) {
2927
List<Object> results = new ArrayList<>();
3028
for (Object source : sources) {
3129

32-
DataFetchingEnvironment singleEnv = newDataFetchingEnvironment(environment)
33-
.source(source)
34-
.build();
30+
DataFetchingEnvironment singleEnv = environment
31+
.transform(builder -> builder.source(source));
3532
results.add(delegate.get(singleEnv));
3633
}
3734
return results;

src/main/java/graphql/schema/DataFetchingEnvironment.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import java.util.List;
99
import java.util.Map;
10+
import java.util.function.Consumer;
11+
12+
import static graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment;
1013

1114
/**
1215
* A DataFetchingEnvironment instance of passed to a {@link DataFetcher} as an execution context parameter
@@ -21,6 +24,7 @@ public interface DataFetchingEnvironment {
2124
* For the root query, it is equal to {{@link DataFetchingEnvironment#getRoot}
2225
*
2326
* @param <T> you decide what type it is
27+
*
2428
* @return can be null for the root query, otherwise it is never null
2529
*/
2630
<T> T getSource();
@@ -34,6 +38,7 @@ public interface DataFetchingEnvironment {
3438
* Returns true of the named argument is present
3539
*
3640
* @param name the name of the argument
41+
*
3742
* @return true of the named argument is present
3843
*/
3944
boolean containsArgument(String name);
@@ -43,6 +48,7 @@ public interface DataFetchingEnvironment {
4348
*
4449
* @param name the name of the argument
4550
* @param <T> you decide what type it is
51+
*
4652
* @return the named argument or null if its not [present
4753
*/
4854
<T> T getArgument(String name);
@@ -54,14 +60,16 @@ public interface DataFetchingEnvironment {
5460
* This is a info object which is provided to all DataFetcher, but never used by graphql-java itself.
5561
*
5662
* @param <T> you decide what type it is
63+
*
5764
* @return can be null
5865
*/
5966
<T> T getContext();
6067

6168
/**
6269
* This is the source object for the root query.
6370
*
64-
* @param <T> you decide what type it is
71+
* @param <T> you decide what type it is
72+
*
6573
* @return can be null
6674
*/
6775
<T> T getRoot();
@@ -100,4 +108,18 @@ public interface DataFetchingEnvironment {
100108
* @return the {@link DataFetchingFieldSelectionSet} for the current operation
101109
*/
102110
DataFetchingFieldSelectionSet getSelectionSet();
111+
112+
113+
/**
114+
* This allows you to transform this DataFetchingEnvironment object into another one.
115+
*
116+
* @param action the action to which a {@link DataFetchingEnvironmentBuilder} is passed
117+
*
118+
* @return a new transformed DataFetchingEnvironment
119+
*/
120+
default DataFetchingEnvironment transform(Consumer<DataFetchingEnvironmentBuilder> action) {
121+
DataFetchingEnvironmentBuilder builder = newDataFetchingEnvironment(this);
122+
action.accept(builder);
123+
return builder.build();
124+
}
103125
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package graphql.schema;
2+
3+
import graphql.PublicApi;
4+
import graphql.execution.ExecutionContext;
5+
import graphql.execution.ExecutionId;
6+
import graphql.language.Field;
7+
import graphql.language.FragmentDefinition;
8+
9+
import java.util.Collections;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
/**
14+
* A builder of {@link DataFetchingEnvironment}s
15+
*/
16+
@PublicApi
17+
public class DataFetchingEnvironmentBuilder {
18+
19+
/**
20+
* @return a new {@link DataFetchingEnvironmentBuilder}
21+
*/
22+
public static DataFetchingEnvironmentBuilder newDataFetchingEnvironment() {
23+
return new DataFetchingEnvironmentBuilder();
24+
}
25+
26+
public static DataFetchingEnvironmentBuilder newDataFetchingEnvironment(DataFetchingEnvironment environment) {
27+
return new DataFetchingEnvironmentBuilder()
28+
.source(environment.getSource())
29+
.arguments(environment.getArguments())
30+
.context(environment.getContext())
31+
.root(environment.getRoot())
32+
.fields(environment.getFields())
33+
.fieldType(environment.getFieldType())
34+
.parentType(environment.getParentType())
35+
.graphQLSchema(environment.getGraphQLSchema())
36+
.fragmentsByName(environment.getFragmentsByName())
37+
.executionId(environment.getExecutionId())
38+
.selectionSet(environment.getSelectionSet());
39+
}
40+
41+
public static DataFetchingEnvironmentBuilder newDataFetchingEnvironment(ExecutionContext executionContext) {
42+
return new DataFetchingEnvironmentBuilder()
43+
.context(executionContext.getContext())
44+
.root(executionContext.getRoot())
45+
.graphQLSchema(executionContext.getGraphQLSchema())
46+
.fragmentsByName(executionContext.getFragmentsByName())
47+
.executionId(executionContext.getExecutionId());
48+
}
49+
50+
51+
private Object source;
52+
private Map<String, Object> arguments = Collections.emptyMap();
53+
private Object context;
54+
private Object root;
55+
private List<Field> fields = Collections.emptyList();
56+
private GraphQLOutputType fieldType;
57+
private GraphQLType parentType;
58+
private GraphQLSchema graphQLSchema;
59+
private Map<String, FragmentDefinition> fragmentsByName = Collections.emptyMap();
60+
private ExecutionId executionId;
61+
private DataFetchingFieldSelectionSet selectionSet;
62+
63+
64+
public DataFetchingEnvironmentBuilder source(Object source) {
65+
this.source = source;
66+
return this;
67+
}
68+
69+
public DataFetchingEnvironmentBuilder arguments(Map<String, Object> arguments) {
70+
this.arguments = arguments;
71+
return this;
72+
}
73+
74+
public DataFetchingEnvironmentBuilder context(Object context) {
75+
this.context = context;
76+
return this;
77+
}
78+
79+
public DataFetchingEnvironmentBuilder root(Object root) {
80+
this.root = root;
81+
return this;
82+
}
83+
84+
public DataFetchingEnvironmentBuilder fields(List<Field> fields) {
85+
this.fields = fields;
86+
return this;
87+
}
88+
89+
public DataFetchingEnvironmentBuilder fieldType(GraphQLOutputType fieldType) {
90+
this.fieldType = fieldType;
91+
return this;
92+
}
93+
94+
public DataFetchingEnvironmentBuilder parentType(GraphQLType parentType) {
95+
this.parentType = parentType;
96+
return this;
97+
}
98+
99+
public DataFetchingEnvironmentBuilder graphQLSchema(GraphQLSchema graphQLSchema) {
100+
this.graphQLSchema = graphQLSchema;
101+
return this;
102+
}
103+
104+
public DataFetchingEnvironmentBuilder fragmentsByName(Map<String, FragmentDefinition> fragmentsByName) {
105+
this.fragmentsByName = fragmentsByName;
106+
return this;
107+
}
108+
109+
public DataFetchingEnvironmentBuilder executionId(ExecutionId executionId) {
110+
this.executionId = executionId;
111+
return this;
112+
}
113+
114+
public DataFetchingEnvironmentBuilder selectionSet(DataFetchingFieldSelectionSet selectionSet) {
115+
this.selectionSet = selectionSet;
116+
return this;
117+
}
118+
119+
public DataFetchingEnvironment build() {
120+
return new DataFetchingEnvironmentImpl(source, arguments, context, root,
121+
fields, fieldType, parentType, graphQLSchema, fragmentsByName, executionId, selectionSet
122+
);
123+
}
124+
}
Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package graphql.schema;
22

33

4-
import graphql.execution.ExecutionContext;
4+
import graphql.Internal;
55
import graphql.execution.ExecutionId;
66
import graphql.language.Field;
77
import graphql.language.FragmentDefinition;
88

9-
import java.util.Collections;
109
import java.util.List;
1110
import java.util.Map;
1211

1312
@SuppressWarnings("unchecked")
13+
@Internal
1414
public class DataFetchingEnvironmentImpl implements DataFetchingEnvironment {
1515
private final Object source;
1616
private final Map<String, Object> arguments;
@@ -102,110 +102,4 @@ public ExecutionId getExecutionId() {
102102
public DataFetchingFieldSelectionSet getSelectionSet() {
103103
return selectionSet;
104104
}
105-
106-
public static Builder newDataFetchingEnvironment() {
107-
return new Builder();
108-
}
109-
110-
public static Builder newDataFetchingEnvironment(ExecutionContext executionContext) {
111-
return new Builder()
112-
.context(executionContext.getContext())
113-
.root(executionContext.getRoot())
114-
.graphQLSchema(executionContext.getGraphQLSchema())
115-
.fragmentsByName(executionContext.getFragmentsByName())
116-
.executionId(executionContext.getExecutionId());
117-
}
118-
119-
120-
public static Builder newDataFetchingEnvironment(DataFetchingEnvironment environment) {
121-
return new Builder()
122-
.source(environment.getSource())
123-
.arguments(environment.getArguments())
124-
.context(environment.getContext())
125-
.root(environment.getRoot())
126-
.fields(environment.getFields())
127-
.fieldType(environment.getFieldType())
128-
.parentType(environment.getParentType())
129-
.graphQLSchema(environment.getGraphQLSchema())
130-
.fragmentsByName(environment.getFragmentsByName())
131-
.executionId(environment.getExecutionId())
132-
.selectionSet(environment.getSelectionSet());
133-
}
134-
135-
public static class Builder {
136-
137-
private Object source;
138-
private Map<String, Object> arguments = Collections.emptyMap();
139-
private Object context;
140-
private Object root;
141-
private List<Field> fields = Collections.emptyList();
142-
private GraphQLOutputType fieldType;
143-
private GraphQLType parentType;
144-
private GraphQLSchema graphQLSchema;
145-
private Map<String, FragmentDefinition> fragmentsByName = Collections.emptyMap();
146-
private ExecutionId executionId;
147-
private DataFetchingFieldSelectionSet selectionSet;
148-
149-
150-
public Builder source(Object source) {
151-
this.source = source;
152-
return this;
153-
}
154-
155-
public Builder arguments(Map<String, Object> arguments) {
156-
this.arguments = arguments;
157-
return this;
158-
}
159-
160-
public Builder context(Object context) {
161-
this.context = context;
162-
return this;
163-
}
164-
165-
public Builder root(Object root) {
166-
this.root = root;
167-
return this;
168-
}
169-
170-
public Builder fields(List<Field> fields) {
171-
this.fields = fields;
172-
return this;
173-
}
174-
175-
public Builder fieldType(GraphQLOutputType fieldType) {
176-
this.fieldType = fieldType;
177-
return this;
178-
}
179-
180-
public Builder parentType(GraphQLType parentType) {
181-
this.parentType = parentType;
182-
return this;
183-
}
184-
185-
public Builder graphQLSchema(GraphQLSchema graphQLSchema) {
186-
this.graphQLSchema = graphQLSchema;
187-
return this;
188-
}
189-
190-
public Builder fragmentsByName(Map<String, FragmentDefinition> fragmentsByName) {
191-
this.fragmentsByName = fragmentsByName;
192-
return this;
193-
}
194-
195-
public Builder executionId(ExecutionId executionId) {
196-
this.executionId = executionId;
197-
return this;
198-
}
199-
200-
public Builder selectionSet(DataFetchingFieldSelectionSet selectionSet) {
201-
this.selectionSet = selectionSet;
202-
return this;
203-
}
204-
205-
public DataFetchingEnvironment build() {
206-
return new DataFetchingEnvironmentImpl(source, arguments, context, root,
207-
fields, fieldType, parentType, graphQLSchema, fragmentsByName, executionId, selectionSet
208-
);
209-
}
210-
}
211105
}

src/test/groovy/graphql/DataFetcherTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package graphql
22

3-
import graphql.schema.DataFetchingEnvironmentImpl
43
import graphql.schema.FieldDataFetcher
54
import graphql.schema.GraphQLOutputType
65
import graphql.schema.PropertyDataFetcher
76
import spock.lang.Specification
87

98
import static graphql.Scalars.GraphQLBoolean
109
import static graphql.Scalars.GraphQLString
10+
import static graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment
1111

1212
class DataFetcherTest extends Specification {
1313

@@ -55,7 +55,7 @@ class DataFetcherTest extends Specification {
5555
}
5656

5757
def env(GraphQLOutputType type) {
58-
DataFetchingEnvironmentImpl.newDataFetchingEnvironment().source(dataHolder).fieldType(type).build()
58+
newDataFetchingEnvironment().source(dataHolder).fieldType(type).build()
5959
}
6060

6161
def "get field value"() {

0 commit comments

Comments
 (0)