|
1 | 1 | package com.coxautodev.graphql.tools; |
2 | 2 |
|
| 3 | +import graphql.GraphQL; |
| 4 | +import graphql.execution.AsyncExecutionStrategy; |
3 | 5 | import graphql.relay.Connection; |
4 | 6 | import graphql.relay.SimpleListConnection; |
5 | 7 | import graphql.schema.*; |
6 | 8 | import graphql.schema.idl.SchemaDirectiveWiring; |
7 | 9 | import graphql.schema.idl.SchemaDirectiveWiringEnvironment; |
8 | | -import graphql.schema.idl.SchemaDirectiveWiringEnvironmentImpl; |
| 10 | +import groovy.lang.Closure; |
9 | 11 | import org.junit.Test; |
10 | 12 | import org.slf4j.Logger; |
11 | 13 | import org.slf4j.LoggerFactory; |
12 | 14 |
|
13 | | -import java.util.ArrayList; |
| 15 | +import java.util.*; |
14 | 16 |
|
15 | 17 | public class RelayConnectionTest { |
16 | 18 |
|
17 | 19 | private static final Logger log = LoggerFactory.getLogger(RelayConnectionTest.class); |
18 | 20 |
|
19 | 21 | @Test |
20 | 22 | public void compiles() { |
21 | | - SchemaParser.newParser().file("RelayConnection.graphqls") |
| 23 | + GraphQLSchema schema = SchemaParser.newParser().file("RelayConnection.graphqls") |
22 | 24 | .resolvers(new QueryResolver()) |
23 | 25 | .dictionary(User.class) |
24 | | - .directive("connection", new RelayConnection()) |
| 26 | + .directive("connection", new ConnectionDirective()) |
25 | 27 | .build() |
26 | 28 | .makeExecutableSchema(); |
| 29 | + |
| 30 | + GraphQL gql = GraphQL.newGraphQL(schema) |
| 31 | + .queryExecutionStrategy(new AsyncExecutionStrategy()) |
| 32 | + .build(); |
| 33 | + |
| 34 | + Map<String,Object> variables = new HashMap<>(); |
| 35 | + variables.put("limit", 10); |
| 36 | + Utils.assertNoGraphQlErrors(gql, variables, new Closure<String>(null) { |
| 37 | + @Override |
| 38 | + public String call() { |
| 39 | + return "query {\n" + |
| 40 | + " users {\n" + |
| 41 | + " edges {\n" + |
| 42 | + " node {\n" + |
| 43 | + " id\n" + |
| 44 | + " name\n" + |
| 45 | + " }\n" + |
| 46 | + " }\n" + |
| 47 | + " }\n" + |
| 48 | + "}"; |
| 49 | + } |
| 50 | + }); |
27 | 51 | } |
28 | 52 |
|
29 | 53 | static class QueryResolver implements GraphQLQueryResolver { |
30 | 54 | // fixme #114: desired return type to use: Connection<User> |
31 | 55 | public Connection<User> users(int first, String after, DataFetchingEnvironment env) { |
32 | | - return new SimpleListConnection<User>(new ArrayList<>()).get(env); |
| 56 | + return new SimpleListConnection<>(Collections.singletonList(new User(1L, "Luke"))).get(env); |
33 | 57 | } |
34 | 58 | } |
35 | 59 |
|
36 | 60 | static class User { |
37 | 61 | Long id; |
38 | 62 | String name; |
| 63 | + |
| 64 | + public User(Long id, String name) { |
| 65 | + this.id = id; |
| 66 | + this.name = name; |
| 67 | + } |
39 | 68 | } |
40 | 69 |
|
41 | | - static class RelayConnection implements SchemaDirectiveWiring { |
| 70 | + static class ConnectionDirective implements SchemaDirectiveWiring { |
42 | 71 |
|
43 | 72 | @Override |
44 | 73 | public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> environment) { |
|
0 commit comments