Skip to content

Commit 81e3b95

Browse files
bmsantosyrashk
authored andcommitted
Issue #39: Should use DataFetcher specialized constructor (#44)
* Should use Datafeetcher specialized constructor DataFetchers can now be instantiated using specialized constructors that accept strings for arguments. This faciliates DataFetcher reusability for given fields (no need to create inumerous specific types). Issue #39 * Introduce missing license header Added mising license header. Issue #39
1 parent 754d434 commit 81e3b95

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

src/main/java/graphql/annotations/GraphQLAnnotations.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
import static graphql.schema.GraphQLInterfaceType.newInterface;
6363
import static graphql.schema.GraphQLObjectType.newObject;
6464
import static graphql.schema.GraphQLUnionType.newUnionType;
65+
import static java.util.Arrays.stream;
66+
import static java.util.Objects.isNull;
67+
import static java.util.Objects.nonNull;
6568

6669
/**
6770
* A utility class for extracting GraphQL data structures from annotated
@@ -321,7 +324,19 @@ protected GraphQLFieldDefinition getField(Field field) throws GraphQLAnnotations
321324
}
322325

323326
GraphQLDataFetcher dataFetcher = field.getAnnotation(GraphQLDataFetcher.class);
324-
DataFetcher actualDataFetcher = dataFetcher != null ? newInstance(dataFetcher.value()) : null;
327+
DataFetcher actualDataFetcher = null;
328+
if (nonNull(dataFetcher)) {
329+
final String[] args = dataFetcher.args();
330+
if (args.length == 0) {
331+
actualDataFetcher = newInstance(dataFetcher.value());
332+
} else {
333+
try {
334+
final Constructor<? extends DataFetcher> ctr = dataFetcher.value().getDeclaredConstructor(
335+
stream(args).map(v -> String.class).toArray(Class[]::new));
336+
actualDataFetcher = constructNewInstance(ctr, (Object[]) args);
337+
} catch (final NoSuchMethodException e) {}
338+
}
339+
}
325340

326341
if (actualDataFetcher == null) {
327342

src/main/java/graphql/annotations/GraphQLDataFetcher.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@
2525
@Retention(RetentionPolicy.RUNTIME)
2626
public @interface GraphQLDataFetcher {
2727
Class<? extends DataFetcher> value();
28+
String[] args() default {};
2829
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations;
16+
17+
import java.util.HashMap;
18+
19+
import graphql.ExecutionResult;
20+
import graphql.GraphQL;
21+
import graphql.schema.DataFetcher;
22+
import graphql.schema.DataFetchingEnvironment;
23+
import graphql.schema.GraphQLObjectType;
24+
import graphql.schema.GraphQLSchema;
25+
import graphql.schema.PropertyDataFetcher;
26+
import org.testng.annotations.Test;
27+
28+
import static graphql.schema.GraphQLSchema.newSchema;
29+
import static org.testng.Assert.assertNotNull;
30+
import static org.testng.Assert.assertTrue;
31+
32+
public class GraphQLDataFetcherTest {
33+
34+
@Test
35+
public void shouldUsePreferredConstructor() {
36+
// Given
37+
final GraphQLObjectType object = GraphQLAnnotations.object(GraphQLDataFetcherTest.TestGraphQLQuery.class);
38+
final GraphQLSchema schema = newSchema().query(object).build();
39+
final GraphQL graphql = new GraphQL(schema);
40+
41+
// When
42+
final ExecutionResult result = graphql.execute("{sample {isGreat}}");
43+
44+
// Then
45+
final HashMap<String, Object> data = (HashMap) result.getData();
46+
assertNotNull(data);
47+
assertTrue(((HashMap<String,Boolean>)data.get("sample")).get("isGreat"));
48+
}
49+
50+
@GraphQLName("Query")
51+
public static class TestGraphQLQuery {
52+
@GraphQLField
53+
@GraphQLDataFetcher(SampleDataFetcher.class)
54+
public TestSample sample() { // Note that GraphQL uses TestSample to build the graph
55+
return null;
56+
}
57+
}
58+
59+
public static class TestSample {
60+
@GraphQLField
61+
@GraphQLDataFetcher(value = PropertyDataFetcher.class, args = "isGreat")
62+
private Boolean isGreat = false; // Defaults to FieldDataFetcher
63+
}
64+
65+
public static class SampleDataFetcher implements DataFetcher {
66+
@Override
67+
public Object get(final DataFetchingEnvironment environment) {
68+
return new Sample(); // Notice that it return a Sample, not a TestSample
69+
}
70+
}
71+
72+
public static class Sample {
73+
private Boolean isGreat = true;
74+
75+
public Boolean getIsGreat() {
76+
return isGreat;
77+
}
78+
79+
public void setIsGreat(final Boolean isGreat) {
80+
this.isGreat = isGreat;
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)