Skip to content

Commit 46923f2

Browse files
committed
Added and modified tests
1 parent 52a5135 commit 46923f2

File tree

2 files changed

+140
-69
lines changed

2 files changed

+140
-69
lines changed

src/test/java/graphql/annotations/connection/GraphQLConnectionTest.java

Lines changed: 109 additions & 29 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,
@@ -17,8 +17,6 @@
1717
import graphql.ExecutionResult;
1818
import graphql.GraphQL;
1919
import graphql.annotations.*;
20-
import graphql.annotations.connection.ConnectionFetcher;
21-
import graphql.annotations.connection.GraphQLConnection;
2220
import graphql.annotations.util.CustomRelay;
2321
import graphql.relay.Relay;
2422
import graphql.schema.DataFetchingEnvironment;
@@ -28,9 +26,8 @@
2826
import org.testng.annotations.BeforeMethod;
2927
import org.testng.annotations.Test;
3028

31-
import java.util.Arrays;
32-
import java.util.List;
33-
import java.util.Map;
29+
import java.util.*;
30+
import java.util.stream.Collectors;
3431
import java.util.stream.Stream;
3532

3633
import static graphql.annotations.util.RelayKit.EMPTY_CONNECTION;
@@ -68,36 +65,67 @@ public static class DuplicateTest {
6865
}
6966

7067
public static class TestListField {
71-
@GraphQLField
72-
@GraphQLConnection
7368
public List<Obj> objs;
7469

7570
public TestListField(List<Obj> objs) {
7671
this.objs = objs;
7772
}
78-
}
7973

80-
public static class TestListField2 {
8174
@GraphQLField
8275
@GraphQLConnection
76+
public PaginatedData<Obj> objs() {
77+
return new AbstarctPaginatedData<Obj>(false, true, objs) {
78+
@Override
79+
public String getCursor(Obj entity) {
80+
return entity.id;
81+
}
82+
};
83+
}
84+
}
85+
86+
public static class TestListField2 {
87+
8388
public List<Obj> objs;
8489

8590
public TestListField2(List<Obj> objs) {
8691
this.objs = objs;
8792
}
93+
94+
@GraphQLField
95+
@GraphQLConnection
96+
public PaginatedData<Obj> objs() {
97+
return new AbstarctPaginatedData<Obj>(false, true, objs) {
98+
@Override
99+
public String getCursor(Obj entity) {
100+
return entity.id;
101+
}
102+
};
103+
}
88104
}
89105

90-
@Test
106+
public static class TestConnectionOnField {
107+
@GraphQLField
108+
@GraphQLConnection
109+
public PaginatedData<Obj> objs;
110+
111+
public TestConnectionOnField(List<Obj> objs) {
112+
this.objs = new AbstarctPaginatedData<Obj>(false, true, objs) {
113+
@Override
114+
public String getCursor(Obj entity) {
115+
return entity.id;
116+
}
117+
};
118+
}
119+
}
120+
121+
@Test(expectedExceptions = GraphQLConnectionException.class)
91122
public void fieldList() {
92-
GraphQLObjectType object = GraphQLAnnotations.object(TestListField.class);
123+
GraphQLObjectType object = GraphQLAnnotations.object(TestConnectionOnField.class);
93124
GraphQLSchema schema = newSchema().query(object).build();
94125

95126
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
96127
ExecutionResult result = graphQL.execute("{ objs(first: 1) { edges { cursor node { id, val } } } }",
97128
new TestListField(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world"))));
98-
assertTrue(result.getErrors().isEmpty());
99-
100-
testResult("objs", result);
101129
}
102130

103131
public static class TestConnections {
@@ -109,33 +137,80 @@ public TestConnections(List<Obj> objs) {
109137

110138
@GraphQLField
111139
@GraphQLConnection
112-
public List<Obj> getObjs() {
113-
return this.objs;
140+
public PaginatedData<Obj> getObjs(DataFetchingEnvironment environment) {
141+
Integer first = environment.getArgument("first");
142+
List<Obj> actualobjs = new ArrayList<>(objs);
143+
144+
if (first != null && first < objs.size()) {
145+
actualobjs = actualobjs.subList(0, first);
146+
}
147+
return new AbstarctPaginatedData<Obj>(false, true, actualobjs) {
148+
@Override
149+
public String getCursor(Obj entity) {
150+
return entity.id;
151+
}
152+
};
114153
}
115154

116155
@GraphQLField
117156
@GraphQLConnection(name = "objStream")
118-
public Stream<Obj> getObjStream() {
119-
Obj[] a = new Obj[objs.size()];
120-
return Stream.of(objs.toArray(a));
157+
public PaginatedData<Obj> getObjStream(DataFetchingEnvironment environment) {
158+
Integer first = environment.getArgument("first");
159+
List<Obj> actualobjs = new ArrayList<>(objs);
160+
161+
if (first != null && first < objs.size()) {
162+
actualobjs = actualobjs.subList(0, first);
163+
}
164+
165+
Obj[] a = new Obj[actualobjs.size()];
166+
Iterable<Obj> data = Stream.of(actualobjs.toArray(a))::iterator;
167+
return new AbstarctPaginatedData<Obj>(false, true, data) {
168+
@Override
169+
public String getCursor(Obj entity) {
170+
return entity.id;
171+
}
172+
};
121173
}
122174

123175
@GraphQLField
124176
@GraphQLConnection(name = "objStreamWithParam")
125-
public Stream<Obj> getObjStreamWithParam(@GraphQLName("filter") String filter) {
126-
return this.objs.stream().filter( obj -> obj.val.startsWith(filter));
177+
public PaginatedData<Obj> getObjStreamWithParam(DataFetchingEnvironment environment, @GraphQLName("filter") String filter) {
178+
Integer first = environment.getArgument("first");
179+
List<Obj> actualobjs = new ArrayList<>(objs);
180+
List<Obj> filteredObjs = actualobjs.stream().filter(obj -> obj.val.startsWith(filter)).collect(Collectors.toList());
181+
if (first != null && first < filteredObjs.size()) {
182+
filteredObjs = filteredObjs.subList(0, first);
183+
}
184+
Iterable<Obj> objIterable = filteredObjs::iterator;
185+
return new AbstarctPaginatedData<Obj>(false, true, objIterable) {
186+
@Override
187+
public String getCursor(Obj entity) {
188+
return entity.id;
189+
}
190+
};
127191
}
128192

129193
@GraphQLField
130194
@GraphQLConnection(name = "nonNullObjs")
131195
@GraphQLNonNull
132-
public List<Obj> getNonNullObjs() {
133-
return this.objs;
196+
public PaginatedData<Obj> getNonNullObjs(DataFetchingEnvironment environment) {
197+
Integer first = environment.getArgument("first");
198+
List<Obj> actualobjs = new ArrayList<>(objs);
199+
200+
if (first != null && first < objs.size()) {
201+
actualobjs = actualobjs.subList(0, first);
202+
}
203+
return new AbstarctPaginatedData<Obj>(false, true, actualobjs) {
204+
@Override
205+
public String getCursor(Obj entity) {
206+
return entity.id;
207+
}
208+
};
134209
}
135210

136211
@GraphQLField
137212
@GraphQLConnection(name = "nullObj")
138-
public List<Obj> getNullObj() {
213+
public PaginatedData<Obj> getNullObj() {
139214
return null;
140215
}
141216

@@ -167,7 +242,7 @@ public void customRelayMethodList() {
167242
graphql.schema.GraphQLObjectType f = (GraphQLObjectType) schema.getType("ObjConnection");
168243
assertTrue(f.getFieldDefinitions().size() == 4);
169244
assertTrue(f.getFieldDefinition("nodes").getType() instanceof GraphQLList);
170-
assertEquals(((GraphQLList)f.getFieldDefinition("nodes").getType()).getWrappedType().getName(), "Obj");
245+
assertEquals(((GraphQLList) f.getFieldDefinition("nodes").getType()).getWrappedType().getName(), "Obj");
171246

172247
GraphQLObjectType pageInfo = (GraphQLObjectType) schema.getType("PageInfo");
173248
assertTrue(pageInfo.getFieldDefinition("additionalInfo") != null);
@@ -271,8 +346,13 @@ public TestCustomConnection(List<Obj> objs) {
271346

272347
@GraphQLField
273348
@GraphQLConnection(connection = CustomConnection.class)
274-
public List<Obj> getObjs() {
275-
return this.objs;
349+
public PaginatedData<Obj> getObjs() {
350+
return new AbstarctPaginatedData<Obj>(true, false, objs) {
351+
@Override
352+
public String getCursor(Obj entity) {
353+
return entity.id;
354+
}
355+
};
276356
}
277357
}
278358

@@ -298,7 +378,7 @@ public void duplicateConnection() {
298378
GraphQLObjectType object = GraphQLAnnotations.object(DuplicateTest.class);
299379
GraphQLSchema schema = newSchema().query(object).build();
300380
} catch (GraphQLAnnotationsException e) {
301-
fail("Schema cannot be created",e);
381+
fail("Schema cannot be created", e);
302382
}
303383
}
304384

src/test/java/graphql/annotations/connection/GraphQLEnhancedConnectionTest.java

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@
2828
import org.testng.annotations.BeforeMethod;
2929
import org.testng.annotations.Test;
3030

31-
import java.util.Arrays;
32-
import java.util.Collections;
33-
import java.util.List;
34-
import java.util.Map;
31+
import java.util.*;
3532

3633
import static graphql.schema.GraphQLSchema.newSchema;
3734
import static org.testng.Assert.assertEquals;
@@ -44,6 +41,7 @@ public class GraphQLEnhancedConnectionTest {
4441

4542
@BeforeClass
4643
public static void setUp() throws Exception {
44+
GraphQLAnnotations.getInstance().getTypeRegistry().clear();
4745
GraphQLObjectType object = GraphQLAnnotations.object(TestListField.class);
4846
GraphQLSchema schema = newSchema().query(object).build();
4947

@@ -62,6 +60,7 @@ public static class Obj {
6260

6361
@GraphQLField
6462
public String val;
63+
6564
public Obj(String id, String val) {
6665
this.id = id;
6766
this.val = val;
@@ -80,64 +79,56 @@ public static class TestListField {
8079
@GraphQLField
8180
@GraphQLConnection(connection = EnhancedConnectionFetcher.class)
8281
@GraphQLDataFetcher(GoodConnectionDataFetcher.class)
83-
public List<Obj> objs;
82+
public PaginatedData<Obj> objs;
8483

85-
public TestListField(List<Obj> objs) {
84+
public TestListField(PaginatedData<Obj> objs) {
8685
this.objs = objs;
8786
}
8887
}
8988

90-
public static class GoodConnectionDataFetcher implements PaginationDataFetcher<Obj> {
91-
89+
public static class GoodConnectionDataFetcher implements DataFetcher<PaginatedData<Obj>> {
9290

9391
@Override
94-
public boolean hasNextPage(String lastCursor) {
95-
return true;
96-
}
92+
public PaginatedData<Obj> get(DataFetchingEnvironment environment) {
9793

98-
@Override
99-
public boolean hasPreviousPage(String firstCursor) {
100-
return false;
101-
}
102-
103-
@Override
104-
public String getCursor(Obj entity) {
105-
return entity.id;
106-
}
107-
108-
@Override
109-
public List<Obj> get(DataFetchingEnvironment environment) {
11094
Integer first = environment.getArgument("first");
11195
List<Obj> objs = Arrays.asList(new Obj("1", "1"), new Obj("2", "2"), new Obj("3", "3"));
112-
if(first != null && first <= 3) {
113-
return objs.subList(0,first);
96+
if (first != null && first <= 3) {
97+
objs = objs.subList(0, first);
11498
}
115-
return objs;
99+
return new AbstarctPaginatedData<Obj>(false, true, objs) {
100+
@Override
101+
public String getCursor(Obj entity) {
102+
return entity.getId();
103+
}
104+
};
116105
}
106+
117107
}
118108

119-
public static class NotValidConnectionField {
120-
@GraphQLField
121-
@GraphQLConnection(connection = EnhancedConnectionFetcher.class)
122-
@GraphQLDataFetcher(NotGoodDataFetcher.class)
123-
public List<GraphQLConnectionTest.Obj> objs;
109+
public static class NotValidConnectionField {
110+
@GraphQLField
111+
@GraphQLConnection
112+
@GraphQLDataFetcher(NotGoodDataFetcher.class)
113+
public List<GraphQLConnectionTest.Obj> objs;
124114

125-
public NotValidConnectionField(List<GraphQLConnectionTest.Obj> objs) {
126-
this.objs = objs;
127-
}
115+
public NotValidConnectionField(List<GraphQLConnectionTest.Obj> objs) {
116+
this.objs = objs;
128117
}
118+
}
129119

130-
public static class NotGoodDataFetcher implements DataFetcher<List<Obj>> {
120+
public static class NotGoodDataFetcher implements DataFetcher<List<Obj>> {
131121

132-
@Override
133-
public List<Obj> get(DataFetchingEnvironment environment) {
134-
return Collections.emptyList();
135-
}
122+
@Override
123+
public List<Obj> get(DataFetchingEnvironment environment) {
124+
return Collections.emptyList();
136125
}
137126

127+
}
128+
138129

139130
@Test(expectedExceptions = GraphQLConnectionException.class)
140-
public void datafetcherDoesntExtendPaginationDataFetcher_tryToBuildSchema_getException() throws Exception {
131+
public void ConnectionFieldDoesntReturnPaginatedData_tryToBuildSchema_getException() throws Exception {
141132
//Act + Assert
142133
GraphQLAnnotations.object(NotValidConnectionField.class);
143134
}

0 commit comments

Comments
 (0)