Skip to content

Commit 40819a1

Browse files
committed
logic changes
1 parent 1186a0c commit 40819a1

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

src/main/java/graphql/annotations/processor/util/DataFetcherConstructor.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,31 @@ public DataFetcher constructDataFetcher(String fieldName, GraphQLDataFetcher ann
3838
if (args.length == 0) {
3939
return newInstance(annotatedDataFetcher.value());
4040
} else {
41-
try {
42-
// Get the String[] constructor
43-
final Constructor<? extends DataFetcher> ctr = annotatedDataFetcher.value().getDeclaredConstructor(String[].class);
44-
return constructNewInstance(ctr, new Object[] {args});
45-
} catch (final NoSuchMethodException e) {
46-
throw new GraphQLAnnotationsException("Unable to instantiate DataFetcher via constructor for: " + fieldName, e);
47-
}
41+
return Stream.of(annotatedDataFetcher.value().getConstructors())
42+
// filter only constructor that have the same args as the annotation args
43+
// or that get String[], String... args
44+
.filter(x -> (x.getParameterCount() == args.length) ||
45+
(x.getParameterTypes().length == 1 && x.getParameterTypes()[0] == String[].class))
46+
.map(x -> {
47+
try {
48+
Constructor<? extends DataFetcher> constructor;
49+
if (x.getParameterTypes().length == 1 && x.getParameterTypes()[0] == String[].class) {
50+
constructor = annotatedDataFetcher.value().getDeclaredConstructor(String[].class);
51+
return constructNewInstance(constructor, new Object[]{args});
52+
}
53+
54+
constructor = annotatedDataFetcher.value().getDeclaredConstructor(
55+
stream(args).map(v -> String.class).toArray(Class[]::new));
56+
return constructNewInstance(constructor, (Object[]) args);
57+
} catch (NoSuchMethodException e) {
58+
throw new GraphQLAnnotationsException("Unable to instantiate DataFetcher via constructor for: " + fieldName, e);
59+
}
60+
})
61+
.findFirst()
62+
.orElseThrow(NoArgsConstructorException::new);
4863
}
4964
}
5065

66+
public static class NoArgsConstructorException extends RuntimeException {
67+
}
5168
}

src/test/java/graphql/annotations/processor/util/DataFetcherConstructorTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
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+
*/
115
package graphql.annotations.processor.util;
216

317
import graphql.annotations.annotationTypes.GraphQLDataFetcher;
@@ -16,7 +30,7 @@ public class DataFetcherConstructorTest {
1630
private DataFetcherConstructor constructor = new DataFetcherConstructor();
1731

1832
@Test
19-
public void graphQLDataFetcherWithArgsTest() {
33+
public void graphQLDataFetcherWithArrayCtorTest() {
2034
GraphQLDataFetcher graphQLDataFetcher = getGraphQLDataFetcher(DataFetcherMock.class, false, "Arg1", "Arg2");
2135
DataFetcherMock dataFetcher = (DataFetcherMock) constructor.constructDataFetcher(null, graphQLDataFetcher);
2236

@@ -25,6 +39,17 @@ public void graphQLDataFetcherWithArgsTest() {
2539
assertEquals(dataFetcher.getArgs()[1], "Arg2");
2640
}
2741

42+
@Test
43+
public void graphQLDataFetcherWithParamsCtorTest() {
44+
GraphQLDataFetcher graphQLDataFetcher = getGraphQLDataFetcher(DataFetcherMock.class, false, "Arg1", "Arg2", "Arg3");
45+
DataFetcherMock dataFetcher = (DataFetcherMock) constructor.constructDataFetcher(null, graphQLDataFetcher);
46+
47+
assertEquals(dataFetcher.getArgs().length, 3);
48+
assertEquals(dataFetcher.getArgs()[0], "Arg1");
49+
assertEquals(dataFetcher.getArgs()[1], "Arg2");
50+
assertEquals(dataFetcher.getArgs()[2], "Arg3");
51+
}
52+
2853
@Test
2954
public void graphQLDataFetcherDefaultCtorTest() {
3055
GraphQLDataFetcher graphQLDataFetcher = getGraphQLDataFetcher(DataFetcherMock.class, false);

src/test/java/graphql/annotations/processor/util/DataFetcherMock.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
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+
*/
115
package graphql.annotations.processor.util;
216

317
import graphql.schema.DataFetcher;
@@ -9,11 +23,18 @@
923
public class DataFetcherMock implements DataFetcher {
1024
private String[] args;
1125

12-
public DataFetcherMock(String... args){
26+
public DataFetcherMock(String... args) {
1327
this.args = args;
1428
}
1529

16-
public DataFetcherMock(){ }
30+
public DataFetcherMock(String arg1, String arg2, String arg3) {
31+
this.args = new String[]{
32+
arg1, arg2, arg3
33+
};
34+
}
35+
36+
public DataFetcherMock() {
37+
}
1738

1839
@Override
1940
public Object get(DataFetchingEnvironment environment) {

0 commit comments

Comments
 (0)