Skip to content

Commit 2a3f280

Browse files
committed
Moved test
1 parent 91b6552 commit 2a3f280

File tree

2 files changed

+112
-115
lines changed

2 files changed

+112
-115
lines changed

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

Lines changed: 107 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -134,96 +134,6 @@ public void fieldList() {
134134
ExecutionResult result = graphQL.execute("{ objs(first: 1) { edges { cursor node { id, val } } } }",
135135
new TestListField(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world"))));
136136
}
137-
138-
public static class TestConnections {
139-
private List<Obj> objs;
140-
141-
public TestConnections(List<Obj> objs) {
142-
this.objs = objs;
143-
}
144-
145-
@GraphQLField
146-
@GraphQLConnection
147-
public PaginatedData<Obj> getObjs(DataFetchingEnvironment environment) {
148-
Integer first = environment.getArgument("first");
149-
List<Obj> actualobjs = new ArrayList<>(objs);
150-
151-
if (first != null && first < objs.size()) {
152-
actualobjs = actualobjs.subList(0, first);
153-
}
154-
return new AbstractPaginatedData<Obj>(false, true, actualobjs) {
155-
@Override
156-
public String getCursor(Obj entity) {
157-
return entity.id;
158-
}
159-
};
160-
}
161-
162-
@GraphQLField
163-
@GraphQLConnection(name = "objStream")
164-
public PaginatedData<Obj> getObjStream(DataFetchingEnvironment environment) {
165-
Integer first = environment.getArgument("first");
166-
List<Obj> actualobjs = new ArrayList<>(objs);
167-
168-
if (first != null && first < objs.size()) {
169-
actualobjs = actualobjs.subList(0, first);
170-
}
171-
172-
Obj[] a = new Obj[actualobjs.size()];
173-
Iterable<Obj> data = Stream.of(actualobjs.toArray(a))::iterator;
174-
return new AbstractPaginatedData<Obj>(false, true, data) {
175-
@Override
176-
public String getCursor(Obj entity) {
177-
return entity.id;
178-
}
179-
};
180-
}
181-
182-
@GraphQLField
183-
@GraphQLConnection(name = "objStreamWithParam")
184-
public PaginatedData<Obj> getObjStreamWithParam(DataFetchingEnvironment environment, @GraphQLName("filter") String filter) {
185-
Integer first = environment.getArgument("first");
186-
List<Obj> actualobjs = new ArrayList<>(objs);
187-
List<Obj> filteredObjs = actualobjs.stream().filter(obj -> obj.val.startsWith(filter)).collect(Collectors.toList());
188-
if (first != null && first < filteredObjs.size()) {
189-
filteredObjs = filteredObjs.subList(0, first);
190-
}
191-
Iterable<Obj> objIterable = filteredObjs::iterator;
192-
return new AbstractPaginatedData<Obj>(false, true, objIterable) {
193-
@Override
194-
public String getCursor(Obj entity) {
195-
return entity.id;
196-
}
197-
};
198-
}
199-
200-
@GraphQLField
201-
@GraphQLConnection(name = "nonNullObjs")
202-
@GraphQLNonNull
203-
public PaginatedData<Obj> getNonNullObjs(DataFetchingEnvironment environment) {
204-
Integer first = environment.getArgument("first");
205-
List<Obj> actualobjs = new ArrayList<>(objs);
206-
207-
if (first != null && first < objs.size()) {
208-
actualobjs = actualobjs.subList(0, first);
209-
}
210-
return new AbstractPaginatedData<Obj>(false, true, actualobjs) {
211-
@Override
212-
public String getCursor(Obj entity) {
213-
return entity.id;
214-
}
215-
};
216-
}
217-
218-
@GraphQLField
219-
@GraphQLConnection(name = "nullObj")
220-
public PaginatedData<Obj> getNullObj() {
221-
return null;
222-
}
223-
224-
225-
}
226-
227137
@Test
228138
public void methodList() {
229139
GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class);
@@ -239,6 +149,22 @@ public void methodList() {
239149

240150
}
241151

152+
@Test
153+
public void customConnection() {
154+
GraphQLObjectType object = GraphQLAnnotations.object(TestCustomConnection.class);
155+
GraphQLSchema schema = newSchema().query(object).build();
156+
157+
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
158+
ExecutionResult result = graphQL.execute("{ objs(first: 1) { edges { cursor node { id, val } } } }",
159+
new TestCustomConnection(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world"))));
160+
161+
assertTrue(result.getErrors().isEmpty());
162+
Map<String, Object> data = result.getData();
163+
Map<String, Object> objs = (Map<String, Object>) (data.get("objs"));
164+
List edges = (List) objs.get("edges");
165+
assertEquals(edges.size(), 0);
166+
}
167+
242168
@Test
243169
public void customRelayMethodList() {
244170
try {
@@ -379,30 +305,104 @@ public String getCursor(Obj entity) {
379305
}
380306
}
381307

382-
@Test
383-
public void customConnection() {
384-
GraphQLObjectType object = GraphQLAnnotations.object(TestCustomConnection.class);
385-
GraphQLSchema schema = newSchema().query(object).build();
386-
387-
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
388-
ExecutionResult result = graphQL.execute("{ objs(first: 1) { edges { cursor node { id, val } } } }",
389-
new TestCustomConnection(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world"))));
390-
391-
assertTrue(result.getErrors().isEmpty());
392-
Map<String, Object> data = (Map<String, Object>) result.getData();
393-
Map<String, Object> objs = (Map<String, Object>) (data.get("objs"));
394-
List edges = (List) objs.get("edges");
395-
assertEquals(edges.size(), 0);
396-
}
397-
398308
@Test
399309
public void duplicateConnection() {
400310
try {
401311
GraphQLObjectType object = GraphQLAnnotations.object(DuplicateTest.class);
402-
GraphQLSchema schema = newSchema().query(object).build();
312+
newSchema().query(object).build();
403313
} catch (GraphQLAnnotationsException e) {
404314
fail("Schema cannot be created", e);
405315
}
406316
}
407317

318+
public static class TestConnections {
319+
320+
private List<Obj> objs;
321+
322+
public TestConnections(List<Obj> objs) {
323+
this.objs = objs;
324+
}
325+
326+
@GraphQLField
327+
@GraphQLConnection
328+
public PaginatedData<Obj> getObjs(DataFetchingEnvironment environment) {
329+
Integer first = environment.getArgument("first");
330+
List<Obj> actualobjs = new ArrayList<>(objs);
331+
332+
if (first != null && first < objs.size()) {
333+
actualobjs = actualobjs.subList(0, first);
334+
}
335+
return new AbstractPaginatedData<Obj>(false, true, actualobjs) {
336+
@Override
337+
public String getCursor(Obj entity) {
338+
return entity.id;
339+
}
340+
};
341+
}
342+
343+
@GraphQLField
344+
@GraphQLConnection(name = "objStream")
345+
public PaginatedData<Obj> getObjStream(DataFetchingEnvironment environment) {
346+
Integer first = environment.getArgument("first");
347+
List<Obj> actualobjs = new ArrayList<>(objs);
348+
349+
if (first != null && first < objs.size()) {
350+
actualobjs = actualobjs.subList(0, first);
351+
}
352+
353+
Obj[] a = new Obj[actualobjs.size()];
354+
Iterable<Obj> data = Stream.of(actualobjs.toArray(a))::iterator;
355+
return new AbstractPaginatedData<Obj>(false, true, data) {
356+
@Override
357+
public String getCursor(Obj entity) {
358+
return entity.id;
359+
}
360+
};
361+
}
362+
363+
@GraphQLField
364+
@GraphQLConnection(name = "objStreamWithParam")
365+
public PaginatedData<Obj> getObjStreamWithParam(DataFetchingEnvironment environment, @GraphQLName("filter") String filter) {
366+
Integer first = environment.getArgument("first");
367+
List<Obj> actualobjs = new ArrayList<>(objs);
368+
List<Obj> filteredObjs = actualobjs.stream().filter(obj -> obj.val.startsWith(filter)).collect(Collectors.toList());
369+
if (first != null && first < filteredObjs.size()) {
370+
filteredObjs = filteredObjs.subList(0, first);
371+
}
372+
Iterable<Obj> objIterable = filteredObjs::iterator;
373+
return new AbstractPaginatedData<Obj>(false, true, objIterable) {
374+
@Override
375+
public String getCursor(Obj entity) {
376+
return entity.id;
377+
}
378+
};
379+
}
380+
381+
@GraphQLField
382+
@GraphQLConnection(name = "nonNullObjs")
383+
@GraphQLNonNull
384+
public PaginatedData<Obj> getNonNullObjs(DataFetchingEnvironment environment) {
385+
Integer first = environment.getArgument("first");
386+
List<Obj> actualobjs = new ArrayList<>(objs);
387+
388+
if (first != null && first < objs.size()) {
389+
actualobjs = actualobjs.subList(0, first);
390+
}
391+
return new AbstractPaginatedData<Obj>(false, true, actualobjs) {
392+
@Override
393+
public String getCursor(Obj entity) {
394+
return entity.id;
395+
}
396+
};
397+
}
398+
399+
@GraphQLField
400+
@GraphQLConnection(name = "nullObj")
401+
public PaginatedData<Obj> getNullObj() {
402+
return null;
403+
}
404+
405+
406+
}
407+
408408
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
import graphql.annotations.annotationTypes.GraphQLDataFetcher;
66
import graphql.annotations.annotationTypes.GraphQLField;
77
import graphql.annotations.connection.exceptions.GraphQLConnectionException;
8-
import graphql.annotations.connection.simple.SimplePaginatedData;
9-
import graphql.annotations.connection.simple.SimplePaginatedDataConnectionTypeValidator;
10-
import graphql.annotations.connection.simple.SimplePaginatedDataImpl;
11-
import graphql.annotations.connection.simple.SimpleRelay;
8+
import graphql.annotations.connection.simple.*;
129
import graphql.annotations.processor.GraphQLAnnotations;
1310
import graphql.schema.DataFetcher;
1411
import graphql.schema.DataFetchingEnvironment;
@@ -62,7 +59,7 @@ public void simpleConnection_queryForOverAll_getCorrectAnswer() {
6259

6360
ExecutionResult executionResult = graphQL.execute("{ objs(first: 1) {totalCount}}");
6461

65-
int overAllCount = (Integer) ((HashMap) ((HashMap) executionResult.getData()).get("objs")).get("overAllCount");
62+
int overAllCount = (Integer) ((HashMap) ((HashMap) executionResult.getData()).get("objs")).get("totalCount");
6663

6764
assertEquals(overAllCount, 5);
6865
}
@@ -95,7 +92,7 @@ public Obj(String id, String val) {
9592

9693
public static class TestConnectionOnField {
9794
@GraphQLField
98-
@GraphQLConnection(connectionType = SimpleRelay.class)
95+
@GraphQLConnection(connectionFetcher = SimplePaginatedDataConnectionFetcher.class, connectionType = SimpleRelay.class, validator = SimplePaginatedDataConnectionTypeValidator.class)
9996
public SimplePaginatedData<Obj> objs;
10097

10198
public TestConnectionOnField(List<Obj> objs) {
@@ -105,7 +102,7 @@ public TestConnectionOnField(List<Obj> objs) {
105102

106103
public static class TestConnectionNotGoodReturnType {
107104
@GraphQLField
108-
@GraphQLConnection(connectionType = SimpleRelay.class, validator = SimplePaginatedDataConnectionTypeValidator.class)
105+
@GraphQLConnection(connectionFetcher = SimplePaginatedDataConnectionFetcher.class, connectionType = SimpleRelay.class, validator = SimplePaginatedDataConnectionTypeValidator.class)
109106
@GraphQLDataFetcher(ObjsSimpleConnectionFetcher.class)
110107
public List<Obj> objs;
111108

@@ -116,7 +113,7 @@ public TestConnectionNotGoodReturnType(List<Obj> objs) {
116113

117114
public static class MainConnection {
118115
@GraphQLField
119-
@GraphQLConnection(connectionType = SimpleRelay.class, validator = SimplePaginatedDataConnectionTypeValidator.class)
116+
@GraphQLConnection(connectionFetcher = SimplePaginatedDataConnectionFetcher.class, connectionType = SimpleRelay.class, validator = SimplePaginatedDataConnectionTypeValidator.class)
120117
@GraphQLDataFetcher(ObjsSimpleConnectionFetcher.class)
121118
public SimplePaginatedData<Obj> objs;
122119
}

0 commit comments

Comments
 (0)