Skip to content

Commit 812fd60

Browse files
Sean Brandtyrashk
authored andcommitted
Problem: GraphQLDataFetcher use with respect to names is overly verbose/repetitive
Solution: Enhance the GraphQLDataFetcher annotation to optionally pass it's target's name as the first 'arg' to the DataFetcher's constructor.
1 parent 81e3b95 commit 812fd60

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import java.util.TreeMap;
5353
import java.util.function.Function;
5454
import java.util.stream.Collectors;
55+
import java.util.stream.Stream;
5556

5657
import static graphql.Scalars.GraphQLBoolean;
5758
import static graphql.annotations.ReflectionKit.constructNewInstance;
@@ -326,7 +327,12 @@ protected GraphQLFieldDefinition getField(Field field) throws GraphQLAnnotations
326327
GraphQLDataFetcher dataFetcher = field.getAnnotation(GraphQLDataFetcher.class);
327328
DataFetcher actualDataFetcher = null;
328329
if (nonNull(dataFetcher)) {
329-
final String[] args = dataFetcher.args();
330+
final String[] args;
331+
if ( dataFetcher.firstArgIsTargetName() ) {
332+
args = Stream.concat(Stream.of(field.getName()), stream(dataFetcher.args())).toArray(String[]::new);
333+
} else {
334+
args = dataFetcher.args();
335+
}
330336
if (args.length == 0) {
331337
actualDataFetcher = newInstance(dataFetcher.value());
332338
} else {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@
2626
public @interface GraphQLDataFetcher {
2727
Class<? extends DataFetcher> value();
2828
String[] args() default {};
29+
boolean firstArgIsTargetName() default false;
2930
}

src/test/java/graphql/annotations/GraphQLDataFetcherTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.testng.annotations.Test;
2727

2828
import static graphql.schema.GraphQLSchema.newSchema;
29+
import static org.testng.Assert.assertFalse;
2930
import static org.testng.Assert.assertNotNull;
3031
import static org.testng.Assert.assertTrue;
3132

@@ -39,12 +40,13 @@ public void shouldUsePreferredConstructor() {
3940
final GraphQL graphql = new GraphQL(schema);
4041

4142
// When
42-
final ExecutionResult result = graphql.execute("{sample {isGreat}}");
43+
final ExecutionResult result = graphql.execute("{sample {isGreat isBad}}");
4344

4445
// Then
4546
final HashMap<String, Object> data = (HashMap) result.getData();
4647
assertNotNull(data);
4748
assertTrue(((HashMap<String,Boolean>)data.get("sample")).get("isGreat"));
49+
assertTrue(((HashMap<String,Boolean>)data.get("sample")).get("isBad"));
4850
}
4951

5052
@GraphQLName("Query")
@@ -60,6 +62,11 @@ public static class TestSample {
6062
@GraphQLField
6163
@GraphQLDataFetcher(value = PropertyDataFetcher.class, args = "isGreat")
6264
private Boolean isGreat = false; // Defaults to FieldDataFetcher
65+
66+
@GraphQLField
67+
@GraphQLDataFetcher(value = SampleMultiArgDataFetcher.class, firstArgIsTargetName = true, args = {"true"})
68+
private Boolean isBad = false; // Defaults to FieldDataFetcher
69+
6370
}
6471

6572
public static class SampleDataFetcher implements DataFetcher {
@@ -69,8 +76,28 @@ public Object get(final DataFetchingEnvironment environment) {
6976
}
7077
}
7178

79+
public static class SampleMultiArgDataFetcher extends PropertyDataFetcher {
80+
private boolean flip = false;
81+
82+
public SampleMultiArgDataFetcher(String target, String flip) {
83+
super(target);
84+
this.flip = Boolean.valueOf(flip);
85+
}
86+
87+
@Override
88+
public Object get(DataFetchingEnvironment environment) {
89+
final Object result = super.get(environment);
90+
if ( flip ) {
91+
return !(Boolean)result;
92+
} else {
93+
return result;
94+
}
95+
}
96+
}
97+
7298
public static class Sample {
7399
private Boolean isGreat = true;
100+
private Boolean isBad = false;
74101

75102
public Boolean getIsGreat() {
76103
return isGreat;
@@ -79,5 +106,13 @@ public Boolean getIsGreat() {
79106
public void setIsGreat(final Boolean isGreat) {
80107
this.isGreat = isGreat;
81108
}
109+
110+
public Boolean getIsBad() {
111+
return isBad;
112+
}
113+
114+
public void setIsBad(Boolean bad) {
115+
isBad = bad;
116+
}
82117
}
83-
}
118+
}

0 commit comments

Comments
 (0)