Skip to content

Commit 35abbde

Browse files
committed
Added support for arrays
1 parent 8df94f5 commit 35abbde

File tree

2 files changed

+127
-14
lines changed

2 files changed

+127
-14
lines changed
Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2016 Yurii Rashkovskii
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,39 +18,48 @@
1818
import graphql.schema.GraphQLList;
1919
import graphql.schema.GraphQLType;
2020

21+
import java.lang.reflect.AnnotatedArrayType;
2122
import java.lang.reflect.AnnotatedParameterizedType;
2223
import java.lang.reflect.AnnotatedType;
2324
import java.lang.reflect.ParameterizedType;
2425

2526
/**
26-
* Support for the Iterable things like Lists / Sets / Collections and so on..
27+
* Support for the Iterable things like Lists / Sets / Collections / Arrays and so on..
2728
*/
28-
class IterableFunction implements TypeFunction {
29+
class IterableFunction implements TypeFunction {
2930

3031
private DefaultTypeFunction defaultTypeFunction;
3132

32-
public IterableFunction(DefaultTypeFunction defaultTypeFunction){
33-
this.defaultTypeFunction=defaultTypeFunction;
33+
public IterableFunction(DefaultTypeFunction defaultTypeFunction) {
34+
this.defaultTypeFunction = defaultTypeFunction;
3435
}
3536

3637
@Override
3738
public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
38-
return Iterable.class.isAssignableFrom(aClass);
39+
return Iterable.class.isAssignableFrom(aClass) || aClass.isArray();
3940
}
4041

4142
@Override
4243
public GraphQLType buildType(boolean input, Class<?> aClass, AnnotatedType annotatedType, ProcessingElementsContainer container) {
43-
if (!(annotatedType instanceof AnnotatedParameterizedType)) {
44-
throw new IllegalArgumentException("List type parameter should be specified");
44+
if (!(annotatedType instanceof AnnotatedParameterizedType || annotatedType instanceof AnnotatedArrayType)) {
45+
throw new IllegalArgumentException("List or array type parameter should be specified");
4546
}
46-
AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) annotatedType;
47-
AnnotatedType arg = parameterizedType.getAnnotatedActualTypeArguments()[0];
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+
4857
Class<?> klass;
4958
if (arg.getType() instanceof ParameterizedType) {
5059
klass = (Class<?>) ((ParameterizedType) (arg.getType())).getRawType();
5160
} else {
5261
klass = (Class<?>) arg.getType();
5362
}
54-
return new GraphQLList(defaultTypeFunction.buildType(input, klass, arg,container));
63+
return new GraphQLList(defaultTypeFunction.buildType(input, klass, arg, container));
5564
}
5665
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package graphql.annotations;
2+
3+
import graphql.ExecutionResult;
4+
import graphql.GraphQL;
5+
import graphql.annotations.annotationTypes.GraphQLDataFetcher;
6+
import graphql.annotations.annotationTypes.GraphQLField;
7+
import graphql.annotations.processor.GraphQLAnnotations;
8+
import graphql.schema.DataFetcher;
9+
import graphql.schema.DataFetchingEnvironment;
10+
import graphql.schema.GraphQLObjectType;
11+
import graphql.schema.GraphQLSchema;
12+
import org.testng.annotations.BeforeMethod;
13+
import org.testng.annotations.Test;
14+
15+
import java.util.ArrayList;
16+
import java.util.Collections;
17+
import java.util.LinkedHashMap;
18+
import java.util.List;
19+
20+
import static graphql.schema.GraphQLSchema.newSchema;
21+
import static org.testng.Assert.assertEquals;
22+
import static org.testng.Assert.assertTrue;
23+
24+
public class GraphQLIterableTest {
25+
@BeforeMethod
26+
public void init() {
27+
GraphQLAnnotations.getInstance().getTypeRegistry().clear();
28+
}
29+
30+
public static class TestMappedObject {
31+
@GraphQLField
32+
public String name;
33+
34+
@GraphQLField
35+
public String foo;
36+
}
37+
38+
public static class TestObjectDB {
39+
private String foo;
40+
41+
private String name;
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public String getFoo() {
48+
return foo;
49+
}
50+
51+
TestObjectDB(String name, String foo) {
52+
this.name = name;
53+
this.foo = foo;
54+
}
55+
}
56+
57+
public static class IterableTestQuery {
58+
@GraphQLField
59+
@GraphQLDataFetcher(ArrayFetcher.class)
60+
public TestMappedObject[] array;
61+
62+
@GraphQLField
63+
@GraphQLDataFetcher(ListFetcher.class)
64+
public List<TestMappedObject> list;
65+
}
66+
67+
public static class ArrayFetcher implements DataFetcher<TestObjectDB[]> {
68+
69+
@Override
70+
public TestObjectDB[] get(DataFetchingEnvironment environment) {
71+
return new TestObjectDB[]{new TestObjectDB("hello", "world")};
72+
}
73+
}
74+
75+
public static class ListFetcher implements DataFetcher<List<TestObjectDB>> {
76+
77+
@Override
78+
public List<TestObjectDB> get(DataFetchingEnvironment environment) {
79+
return Collections.singletonList(new TestObjectDB("test", "test"));
80+
}
81+
}
82+
83+
@Test
84+
public void queryWithArray() {
85+
GraphQLObjectType object = GraphQLAnnotations.object(IterableTestQuery.class);
86+
GraphQLSchema schema = newSchema().query(object).build();
87+
88+
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{array {name foo}}");
89+
assertTrue(result.getErrors().isEmpty());
90+
assertEquals(((LinkedHashMap) ((ArrayList) (((LinkedHashMap) result.getData()).get("array"))).get(0)).get("name"), "hello");
91+
assertEquals(((LinkedHashMap) ((ArrayList) (((LinkedHashMap) result.getData()).get("array"))).get(0)).get("foo"), "world");
92+
}
93+
94+
@Test
95+
public void queryWithList() {
96+
GraphQLObjectType object = GraphQLAnnotations.object(IterableTestQuery.class);
97+
GraphQLSchema schema = newSchema().query(object).build();
98+
99+
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{list {name foo}}");
100+
assertTrue(result.getErrors().isEmpty());
101+
assertEquals(((LinkedHashMap) ((ArrayList) (((LinkedHashMap) result.getData()).get("list"))).get(0)).get("name"), "test");
102+
assertEquals(((LinkedHashMap) ((ArrayList) (((LinkedHashMap) result.getData()).get("list"))).get(0)).get("foo"), "test");
103+
}
104+
}

0 commit comments

Comments
 (0)