Skip to content

Commit 51e822f

Browse files
committed
Added a test for query that returns array within an array
1 parent cf9d193 commit 51e822f

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

src/test/java/graphql/annotations/GraphQLIterableTest.java

Lines changed: 48 additions & 18 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-
*
7+
* <p>
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
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.
@@ -41,27 +41,19 @@ public void init() {
4141
GraphQLAnnotations.getInstance().getTypeRegistry().clear();
4242
}
4343

44-
public static class TestMappedObject {
44+
static class TestMappedObject {
4545
@GraphQLField
4646
public String name;
4747

4848
@GraphQLField
4949
public String foo;
5050
}
5151

52-
public static class TestObjectDB {
52+
static class TestObjectDB {
5353
private String foo;
5454

5555
private String name;
5656

57-
public String getName() {
58-
return name;
59-
}
60-
61-
public String getFoo() {
62-
return foo;
63-
}
64-
6557
TestObjectDB(String name, String foo) {
6658
this.name = name;
6759
this.foo = foo;
@@ -76,6 +68,10 @@ public static class IterableTestQuery {
7668
@GraphQLField
7769
@GraphQLDataFetcher(ListFetcher.class)
7870
public List<TestMappedObject> list;
71+
72+
@GraphQLField
73+
@GraphQLDataFetcher(ArrayWithinArrayFetcher.class)
74+
public TestMappedObject[][] arrayWithinArray;
7975
}
8076

8177
public static class ArrayFetcher implements DataFetcher<TestObjectDB[]> {
@@ -90,7 +86,18 @@ public static class ListFetcher implements DataFetcher<List<TestObjectDB>> {
9086

9187
@Override
9288
public List<TestObjectDB> get(DataFetchingEnvironment environment) {
93-
return Collections.singletonList(new TestObjectDB("test", "test"));
89+
return Collections.singletonList(new TestObjectDB("test name", "test foo"));
90+
}
91+
}
92+
93+
public static class ArrayWithinArrayFetcher implements DataFetcher<TestObjectDB[][]> {
94+
95+
@Override
96+
public TestObjectDB[][] get(DataFetchingEnvironment environment) {
97+
return new TestObjectDB[][]{
98+
{new TestObjectDB("hello", "world")},
99+
{new TestObjectDB("a", "b"), new TestObjectDB("c", "d")}
100+
};
94101
}
95102
}
96103

@@ -101,8 +108,8 @@ public void queryWithArray() {
101108

102109
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{array {name foo}}");
103110
assertTrue(result.getErrors().isEmpty());
104-
assertEquals(((LinkedHashMap) ((ArrayList) (((LinkedHashMap) result.getData()).get("array"))).get(0)).get("name"), "hello");
105-
assertEquals(((LinkedHashMap) ((ArrayList) (((LinkedHashMap) result.getData()).get("array"))).get(0)).get("foo"), "world");
111+
assertEquals(((LinkedHashMap) getQueryResultAtIndex(result, "array", 0)).get("name"), "hello");
112+
assertEquals(((LinkedHashMap) getQueryResultAtIndex(result, "array", 0)).get("foo"), "world");
106113
}
107114

108115
@Test
@@ -112,7 +119,30 @@ public void queryWithList() {
112119

113120
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{list {name foo}}");
114121
assertTrue(result.getErrors().isEmpty());
115-
assertEquals(((LinkedHashMap) ((ArrayList) (((LinkedHashMap) result.getData()).get("list"))).get(0)).get("name"), "test");
116-
assertEquals(((LinkedHashMap) ((ArrayList) (((LinkedHashMap) result.getData()).get("list"))).get(0)).get("foo"), "test");
122+
assertEquals(((LinkedHashMap) getQueryResultAtIndex(result, "list", 0)).get("name"), "test name");
123+
assertEquals(((LinkedHashMap) getQueryResultAtIndex(result, "list", 0)).get("foo"), "test foo");
124+
}
125+
126+
@Test
127+
public void queryWithArrayWithinAnArray() {
128+
GraphQLObjectType object = GraphQLAnnotations.object(IterableTestQuery.class);
129+
GraphQLSchema schema = newSchema().query(object).build();
130+
131+
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{arrayWithinArray {name foo}}");
132+
assertTrue(result.getErrors().isEmpty());
133+
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 0, 0)).get("name"), "hello");
134+
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 0, 0)).get("foo"), "world");
135+
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 1, 0)).get("name"), "a");
136+
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 1, 0)).get("foo"), "b");
137+
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 1, 1)).get("name"), "c");
138+
assertEquals(((LinkedHashMap) getQueryResultAtCell(result, "arrayWithinArray", 1, 1)).get("foo"), "d");
139+
}
140+
141+
private Object getQueryResultAtIndex(ExecutionResult result, String queryName, int index) {
142+
return ((ArrayList) (((LinkedHashMap) result.getData()).get(queryName))).get(index);
143+
}
144+
145+
private Object getQueryResultAtCell(ExecutionResult result, String queryName, int rowIndex, int columnIndex) {
146+
return (((ArrayList) (getQueryResultAtIndex(result, queryName, rowIndex))).get(columnIndex));
117147
}
118148
}

0 commit comments

Comments
 (0)