Skip to content

Commit 780cded

Browse files
committed
Added initial Relay tests.
1 parent 1ff078e commit 780cded

2 files changed

Lines changed: 180 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package graphql;
2+
3+
import graphql.relay.Relay;
4+
import graphql.schema.*;
5+
6+
import java.util.ArrayList;
7+
8+
import static graphql.Scalars.GraphQLString;
9+
import static graphql.schema.GraphQLArgument.newArgument;
10+
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
11+
import static graphql.schema.GraphQLObjectType.newObject;
12+
13+
public class RelaySchema {
14+
15+
public static Relay relay = new Relay();
16+
public static GraphQLObjectType StuffType = newObject()
17+
.name("Stuff")
18+
.field(newFieldDefinition()
19+
.name("id")
20+
.type(GraphQLString)
21+
.fetchField()
22+
.build())
23+
24+
.build();
25+
26+
public static GraphQLInterfaceType NodeInterface = relay.nodeInterface(new TypeResolver() {
27+
@Override
28+
public GraphQLObjectType getType(Object object) {
29+
Relay.ResolvedGlobalId resolvedGlobalId = relay.fromGlobalId((String) object);
30+
//TODO: implement
31+
return null;
32+
}
33+
});
34+
35+
public static GraphQLObjectType StuffEdgeType = relay.edgeType("Stuff", StuffType, NodeInterface, new ArrayList<GraphQLFieldDefinition>());
36+
37+
public static GraphQLObjectType StuffConnectionType = relay.connectionType("Stuff", StuffEdgeType, new ArrayList<GraphQLFieldDefinition>());
38+
39+
public static GraphQLObjectType ThingType = newObject()
40+
.name("Thing")
41+
.field(newFieldDefinition()
42+
.name("id")
43+
.type(GraphQLString)
44+
.fetchField()
45+
.build())
46+
.field(newFieldDefinition()
47+
.name("stuffs")
48+
.type(StuffConnectionType)
49+
.build())
50+
.build();
51+
52+
53+
public static GraphQLObjectType RelayQueryType = newObject()
54+
.name("RelayQuery")
55+
.field(relay.nodeField(NodeInterface, new DataFetcher() {
56+
@Override
57+
public Object get(DataFetchingEnvironment environment) {
58+
//TODO: implement
59+
return null;
60+
}
61+
}))
62+
.field(newFieldDefinition()
63+
.name("thing")
64+
.type(ThingType)
65+
.argument(newArgument()
66+
.name("id")
67+
.description("id of the thing")
68+
.type(new GraphQLNonNull(GraphQLString))
69+
.build())
70+
.dataFetcher(new DataFetcher() {
71+
@Override
72+
public Object get(DataFetchingEnvironment environment) {
73+
//TODO: implement
74+
return null;
75+
}
76+
})
77+
.build())
78+
.build();
79+
80+
81+
public static GraphQLSchema Schema = GraphQLSchema.newSchema()
82+
.query(RelayQueryType)
83+
.build();
84+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package graphql
2+
3+
import spock.lang.Specification
4+
5+
class RelaySchemaTest extends Specification {
6+
7+
def "Validate Relay Node schema"() {
8+
9+
given:
10+
def query = """{
11+
__schema {
12+
queryType {
13+
fields {
14+
name
15+
type {
16+
name
17+
kind
18+
}
19+
args {
20+
name
21+
type {
22+
kind
23+
ofType {
24+
name
25+
kind
26+
}
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
"""
34+
when:
35+
def result = new GraphQL(RelaySchema.Schema).execute(query);
36+
37+
then:
38+
def nodeField = result.data["__schema"]["queryType"]["fields"][0];
39+
nodeField == [name: "node", type: [name: "Node", kind: "INTERFACE"], args: [[name: "id", type: [kind: "NON_NULL", ofType: [name: "ID", kind: "SCALAR"]]]]]
40+
}
41+
42+
43+
def "Validate Relay StuffConnection schema"() {
44+
45+
given:
46+
def query = """{
47+
__type(name: "StuffConnection") {
48+
fields {
49+
name
50+
type {
51+
name
52+
kind
53+
ofType {
54+
name
55+
kind
56+
}
57+
}
58+
}
59+
}
60+
}"""
61+
when:
62+
def result = new GraphQL(RelaySchema.Schema).execute(query);
63+
64+
then:
65+
def fields = result.data["__type"]["fields"];
66+
fields == [[name: "edges", type: [name: null, kind: "LIST", ofType: [name: "StuffEdge", kind: "OBJECT"]]], [name: "pageInfo", type: [name: null, kind: "NON_NULL", ofType: [name: "PageInfo", kind: "OBJECT"]]]]
67+
}
68+
69+
def "Validate Relay StuffEdge schema"() {
70+
71+
given:
72+
def query = """{
73+
__type(name: "StuffEdge") {
74+
fields {
75+
name
76+
type {
77+
name
78+
kind
79+
ofType {
80+
name
81+
kind
82+
}
83+
}
84+
}
85+
}
86+
}
87+
"""
88+
when:
89+
def result = new GraphQL(RelaySchema.Schema).execute(query);
90+
91+
then:
92+
def fields = result.data["__type"]["fields"];
93+
fields == [[name: "node", type: [name: "Stuff", kind: "OBJECT", ofType: null]], [name: "cursor", type: [name: null, kind: "NON_NULL", ofType: [name: "String", kind: "SCALAR"]]]]
94+
}
95+
96+
}

0 commit comments

Comments
 (0)