1313
1414import static graphql .Assert .assertNotNull ;
1515import static graphql .Assert .assertValidName ;
16+ import static graphql .schema .DataFetcherFactoryEnvironment .newDataFetchingFactoryEnvironment ;
1617
1718/**
1819 * Fields are the ways you get data values in graphql and a field definition represents a field, its type, the arguments it takes
@@ -30,27 +31,28 @@ public class GraphQLFieldDefinition {
3031 private final String name ;
3132 private final String description ;
3233 private GraphQLOutputType type ;
33- private final DataFetcher dataFetcher ;
34+ private final DataFetcherFactory dataFetcherFactory ;
3435 private final String deprecationReason ;
3536 private final List <GraphQLArgument > arguments ;
3637 private final FieldDefinition definition ;
3738
3839
40+ @ Deprecated
3941 @ Internal
40- public GraphQLFieldDefinition (String name , String description , GraphQLOutputType type , DataFetcher dataFetcher , List <GraphQLArgument > arguments , String deprecationReason ) {
41- this (name , description , type , dataFetcher , arguments , deprecationReason , null );
42+ public GraphQLFieldDefinition (String name , String description , GraphQLOutputType type , DataFetcher <?> dataFetcher , List <GraphQLArgument > arguments , String deprecationReason ) {
43+ this (name , description , type , DataFetcherFactories . useDataFetcher ( dataFetcher ) , arguments , deprecationReason , null );
4244 }
4345
4446 @ Internal
45- public GraphQLFieldDefinition (String name , String description , GraphQLOutputType type , DataFetcher dataFetcher , List <GraphQLArgument > arguments , String deprecationReason , FieldDefinition definition ) {
47+ public GraphQLFieldDefinition (String name , String description , GraphQLOutputType type , DataFetcherFactory dataFetcherFactory , List <GraphQLArgument > arguments , String deprecationReason , FieldDefinition definition ) {
4648 assertValidName (name );
47- assertNotNull (dataFetcher , "dataFetcher can't be null " );
49+ assertNotNull (dataFetcherFactory , "you have to provide a DataFetcher (or DataFetcherFactory) " );
4850 assertNotNull (type , "type can't be null" );
4951 assertNotNull (arguments , "arguments can't be null" );
5052 this .name = name ;
5153 this .description = description ;
5254 this .type = type ;
53- this .dataFetcher = dataFetcher ;
55+ this .dataFetcherFactory = dataFetcherFactory ;
5456 this .arguments = Collections .unmodifiableList (new ArrayList <>(arguments ));
5557 this .deprecationReason = deprecationReason ;
5658 this .definition = definition ;
@@ -71,7 +73,9 @@ public GraphQLOutputType getType() {
7173 }
7274
7375 public DataFetcher getDataFetcher () {
74- return dataFetcher ;
76+ return dataFetcherFactory .get (newDataFetchingFactoryEnvironment ()
77+ .fieldDefinition (this )
78+ .build ());
7579 }
7680
7781 public GraphQLArgument getArgument (String name ) {
@@ -106,8 +110,8 @@ public String toString() {
106110 return "GraphQLFieldDefinition{" +
107111 "name='" + name + '\'' +
108112 ", type=" + type +
109- ", dataFetcher=" + dataFetcher +
110113 ", arguments=" + arguments +
114+ ", dataFetcherFactory=" + dataFetcherFactory +
111115 ", description='" + description + '\'' +
112116 ", deprecationReason='" + deprecationReason + '\'' +
113117 ", definition=" + definition +
@@ -124,7 +128,7 @@ public static class Builder {
124128 private String name ;
125129 private String description ;
126130 private GraphQLOutputType type ;
127- private DataFetcher dataFetcher ;
131+ private DataFetcherFactory <?> dataFetcherFactory ;
128132 private List <GraphQLArgument > arguments = new ArrayList <>();
129133 private String deprecationReason ;
130134 private boolean isField ;
@@ -163,14 +167,41 @@ public Builder type(GraphQLOutputType type) {
163167 return this ;
164168 }
165169
166- public Builder dataFetcher (DataFetcher dataFetcher ) {
170+ /**
171+ * Sets the {@link graphql.schema.DataFetcher} to use with this field.
172+ *
173+ * @param dataFetcher the data fetcher to use
174+ *
175+ * @return this builder
176+ */
177+ public Builder dataFetcher (DataFetcher <?> dataFetcher ) {
167178 assertNotNull (dataFetcher , "dataFetcher must be not null" );
168- this .dataFetcher = dataFetcher ;
179+ this .dataFetcherFactory = DataFetcherFactories .useDataFetcher (dataFetcher );
180+ return this ;
181+ }
182+
183+ /**
184+ * Sets the {@link graphql.schema.DataFetcherFactory} to use with this field.
185+ *
186+ * @param dataFetcherFactory the factory to use
187+ *
188+ * @return this builder
189+ */
190+ public Builder dataFetcherFactory (DataFetcherFactory dataFetcherFactory ) {
191+ assertNotNull (dataFetcherFactory , "dataFetcherFactory must be not null" );
192+ this .dataFetcherFactory = dataFetcherFactory ;
169193 return this ;
170194 }
171195
196+ /**
197+ * This will cause the data fetcher of this field to always return the supplied value
198+ *
199+ * @param value the value to always return
200+ *
201+ * @return this builder
202+ */
172203 public Builder staticValue (final Object value ) {
173- this .dataFetcher = environment -> value ;
204+ this .dataFetcherFactory = DataFetcherFactories . useDataFetcher ( environment -> value ) ;
174205 return this ;
175206 }
176207
@@ -232,16 +263,14 @@ public Builder deprecate(String deprecationReason) {
232263 }
233264
234265 public GraphQLFieldDefinition build () {
235- if (dataFetcher == null ) {
266+ if (dataFetcherFactory == null ) {
236267 if (isField ) {
237- dataFetcher = new FieldDataFetcher (name );
268+ dataFetcherFactory = DataFetcherFactories . useDataFetcher ( new FieldDataFetcher <> (name ) );
238269 } else {
239- dataFetcher = new PropertyDataFetcher (name );
270+ dataFetcherFactory = DataFetcherFactories . useDataFetcher ( new PropertyDataFetcher <> (name ) );
240271 }
241272 }
242- return new GraphQLFieldDefinition (name , description , type , dataFetcher , arguments , deprecationReason , definition );
273+ return new GraphQLFieldDefinition (name , description , type , dataFetcherFactory , arguments , deprecationReason , definition );
243274 }
244-
245-
246275 }
247276}
0 commit comments