Skip to content

Commit a4f3fd0

Browse files
committed
Separated TypeFunction of array and iterable
1 parent 6a0a8f3 commit a4f3fd0

File tree

4 files changed

+55
-35
lines changed

4 files changed

+55
-35
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package graphql.annotations.processor.typeFunctions;
2+
3+
import graphql.annotations.processor.ProcessingElementsContainer;
4+
import graphql.schema.GraphQLList;
5+
import graphql.schema.GraphQLType;
6+
7+
import java.lang.reflect.AnnotatedArrayType;
8+
import java.lang.reflect.AnnotatedType;
9+
import java.lang.reflect.ParameterizedType;
10+
11+
/**
12+
* Support for arrays
13+
*/
14+
public class ArrayFunction implements TypeFunction {
15+
16+
private DefaultTypeFunction defaultTypeFunction;
17+
18+
public ArrayFunction(DefaultTypeFunction defaultTypeFunction) {
19+
this.defaultTypeFunction = defaultTypeFunction;
20+
}
21+
22+
@Override
23+
public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
24+
return aClass.isArray();
25+
}
26+
27+
@Override
28+
public GraphQLType buildType(boolean input, Class<?> aClass, AnnotatedType annotatedType, ProcessingElementsContainer container) {
29+
if (!(annotatedType instanceof AnnotatedArrayType)) {
30+
throw new IllegalArgumentException("Array type parameter should be specified");
31+
}
32+
AnnotatedArrayType parameterizedType = (AnnotatedArrayType) annotatedType;
33+
AnnotatedType arg = parameterizedType.getAnnotatedGenericComponentType();
34+
Class<?> klass;
35+
if (arg.getType() instanceof ParameterizedType) {
36+
klass = (Class<?>) ((ParameterizedType) (arg.getType())).getRawType();
37+
} else {
38+
klass = (Class<?>) arg.getType();
39+
}
40+
return new GraphQLList(defaultTypeFunction.buildType(input, klass, arg, container));
41+
}
42+
}

src/main/java/graphql/annotations/processor/typeFunctions/DefaultTypeFunction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void activate() {
6262
typeFunctions.add(new BigDecimalFunction());
6363
typeFunctions.add(new CharFunction());
6464
typeFunctions.add(new IterableFunction(DefaultTypeFunction.this));
65+
typeFunctions.add(new ArrayFunction(DefaultTypeFunction.this));
6566
typeFunctions.add(new StreamFunction(DefaultTypeFunction.this));
6667
typeFunctions.add(new OptionalFunction(DefaultTypeFunction.this));
6768
typeFunctions.add(new ObjectFunction(graphQLInputProcessor, graphQLOutputProcessor));

src/main/java/graphql/annotations/processor/typeFunctions/IterableFunction.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -18,13 +18,12 @@
1818
import graphql.schema.GraphQLList;
1919
import graphql.schema.GraphQLType;
2020

21-
import java.lang.reflect.AnnotatedArrayType;
2221
import java.lang.reflect.AnnotatedParameterizedType;
2322
import java.lang.reflect.AnnotatedType;
2423
import java.lang.reflect.ParameterizedType;
2524

2625
/**
27-
* Support for the Iterable things like Lists / Sets / Collections / Arrays and so on..
26+
* Support for the Iterable things like Lists / Sets / Collections and so on..
2827
*/
2928
class IterableFunction implements TypeFunction {
3029

@@ -36,24 +35,16 @@ public IterableFunction(DefaultTypeFunction defaultTypeFunction) {
3635

3736
@Override
3837
public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
39-
return Iterable.class.isAssignableFrom(aClass) || aClass.isArray();
38+
return Iterable.class.isAssignableFrom(aClass);
4039
}
4140

4241
@Override
4342
public GraphQLType buildType(boolean input, Class<?> aClass, AnnotatedType annotatedType, ProcessingElementsContainer container) {
44-
if (!(annotatedType instanceof AnnotatedParameterizedType || annotatedType instanceof AnnotatedArrayType)) {
45-
throw new IllegalArgumentException("List or array type parameter should be specified");
43+
if (!(annotatedType instanceof AnnotatedParameterizedType)) {
44+
throw new IllegalArgumentException("List type parameter should be specified");
4645
}
47-
48-
AnnotatedType arg;
49-
if (annotatedType instanceof AnnotatedParameterizedType) {
50-
AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) annotatedType;
51-
arg = parameterizedType.getAnnotatedActualTypeArguments()[0];
52-
} else {
53-
AnnotatedArrayType parameterizedType = (AnnotatedArrayType) annotatedType;
54-
arg = parameterizedType.getAnnotatedGenericComponentType();
55-
}
56-
46+
AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) annotatedType;
47+
AnnotatedType arg = parameterizedType.getAnnotatedActualTypeArguments()[0];
5748
Class<?> klass;
5849
if (arg.getType() instanceof ParameterizedType) {
5950
klass = (Class<?>) ((ParameterizedType) (arg.getType())).getRawType();

src/test/java/graphql/annotations/GraphQLIterableTest.java renamed to src/test/java/graphql/annotations/GraphQLIterableAndArrayTest.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
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-
*/
151
package graphql.annotations;
162

173
import graphql.ExecutionResult;
@@ -35,7 +21,7 @@
3521
import static org.testng.Assert.assertEquals;
3622
import static org.testng.Assert.assertTrue;
3723

38-
public class GraphQLIterableTest {
24+
public class GraphQLIterableAndArrayTest {
3925
@BeforeMethod
4026
public void init() {
4127
GraphQLAnnotations.getInstance().getTypeRegistry().clear();
@@ -60,7 +46,7 @@ static class TestObjectDB {
6046
}
6147
}
6248

63-
public static class IterableTestQuery {
49+
public static class IterableAndArrayTestQuery {
6450
@GraphQLField
6551
@GraphQLDataFetcher(ArrayFetcher.class)
6652
public TestMappedObject[] array;
@@ -103,7 +89,7 @@ public TestObjectDB[][] get(DataFetchingEnvironment environment) {
10389

10490
@Test
10591
public void queryWithArray() {
106-
GraphQLObjectType object = GraphQLAnnotations.object(IterableTestQuery.class);
92+
GraphQLObjectType object = GraphQLAnnotations.object(IterableAndArrayTestQuery.class);
10793
GraphQLSchema schema = newSchema().query(object).build();
10894

10995
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{array {name foo}}");
@@ -114,7 +100,7 @@ public void queryWithArray() {
114100

115101
@Test
116102
public void queryWithList() {
117-
GraphQLObjectType object = GraphQLAnnotations.object(IterableTestQuery.class);
103+
GraphQLObjectType object = GraphQLAnnotations.object(IterableAndArrayTestQuery.class);
118104
GraphQLSchema schema = newSchema().query(object).build();
119105

120106
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{list {name foo}}");
@@ -125,7 +111,7 @@ public void queryWithList() {
125111

126112
@Test
127113
public void queryWithArrayWithinAnArray() {
128-
GraphQLObjectType object = GraphQLAnnotations.object(IterableTestQuery.class);
114+
GraphQLObjectType object = GraphQLAnnotations.object(IterableAndArrayTestQuery.class);
129115
GraphQLSchema schema = newSchema().query(object).build();
130116

131117
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{arrayWithinArray {name foo}}");

0 commit comments

Comments
 (0)