Skip to content

Commit e73f797

Browse files
authored
Add reflection for default Relay types in GraphQL Java (#307)
GraphQL Java ships interfaces for the Relay spec, as well as default implementations for those types: Connection, Edge, PageInfo, ConnectionCursor. This commits adds reflection metadata for default implementations since they are likely to be used when the GraphQL schema leverages the interfaces provided by GraphQL Java.
1 parent a4770a8 commit e73f797

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

metadata/com.graphql-java/graphql-java/19.2/reflect-config.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
11
[
2+
{
3+
"name": "graphql.relay.DefaultConnection",
4+
"allDeclaredMethods": true,
5+
"condition": {
6+
"typeReachable": "graphql.relay.Connection"
7+
}
8+
},
9+
{
10+
"name": "graphql.relay.DefaultEdge",
11+
"allDeclaredMethods": true,
12+
"condition": {
13+
"typeReachable": "graphql.relay.Connection"
14+
}
15+
},
16+
{
17+
"name": "graphql.relay.DefaultPageInfo",
18+
"allDeclaredMethods": true,
19+
"condition": {
20+
"typeReachable": "graphql.relay.Connection"
21+
}
22+
},
23+
{
24+
"name": "graphql.relay.DefaultConnectionCursor",
25+
"allDeclaredMethods": true,
26+
"condition": {
27+
"typeReachable": "graphql.relay.Connection"
28+
}
29+
},
230
{
331
"name": "graphql.schema.GraphQLArgument",
432
"allPublicMethods": true,

tests/src/com.graphql-java/graphql-java/19.2/src/test/java/graphql/GraphQlTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88

99
import java.io.IOException;
1010
import java.io.InputStream;
11+
import java.util.List;
1112
import java.util.function.Consumer;
1213

1314
import graphql.introspection.IntrospectionQuery;
15+
import graphql.relay.Connection;
16+
import graphql.relay.DefaultConnection;
17+
import graphql.relay.DefaultConnectionCursor;
18+
import graphql.relay.DefaultEdge;
19+
import graphql.relay.DefaultPageInfo;
20+
import graphql.relay.Edge;
21+
import graphql.relay.PageInfo;
1422
import graphql.schema.GraphQLSchema;
1523
import graphql.schema.StaticDataFetcher;
1624
import graphql.schema.TypeResolver;
@@ -52,6 +60,22 @@ void introspectionQuery() throws Exception {
5260
assertThat(result.getData().toString()).contains("{__schema={queryType={name=QueryType}");
5361
}
5462

63+
@Test
64+
void paginatedQuery() throws Exception {
65+
PageInfo pageInfo = new DefaultPageInfo(new DefaultConnectionCursor("start"),
66+
new DefaultConnectionCursor("end"), true, true);
67+
Edge<Human> edge = new DefaultEdge<>(new Human(), new DefaultConnectionCursor("current"));
68+
Connection<Human> connection = new DefaultConnection<>(List.of(edge), pageInfo);
69+
GraphQLSchema graphQLSchema = parseSchema("starwars", runtimeWiringBuilder ->
70+
runtimeWiringBuilder.type("QueryType", builder ->
71+
builder.dataFetcher("humans", new StaticDataFetcher(connection)))
72+
.type("Character", typeBuilder -> typeBuilder.typeResolver(characterTypeResolver())));
73+
GraphQL graphQl = GraphQL.newGraphQL(graphQLSchema).build();
74+
ExecutionResult result = graphQl.execute("{ humans { edges { node { id name } } pageInfo { startCursor } } }");
75+
assertThat(result.getErrors()).isEmpty();
76+
assertThat(result.getData().toString()).isEqualTo("{humans={edges=[{node={id=42, name=GraalVM}}], pageInfo={startCursor=start}}}");
77+
}
78+
5579
private GraphQLSchema parseSchema(String schemaFileName, Consumer<RuntimeWiring.Builder> consumer) throws IOException {
5680
try (InputStream inputStream = GraphQlTests.class.getResourceAsStream(schemaFileName + ".graphqls")) {
5781
TypeDefinitionRegistry registry = new SchemaParser().parse(inputStream);

tests/src/com.graphql-java/graphql-java/19.2/src/test/java/graphql/starwars/Human.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public class Human implements Character {
1515

1616
@Override
1717
public Long getId() {
18-
return null;
18+
return 42L;
1919
}
2020

2121
@Override
2222
public String getName() {
23-
return null;
23+
return "GraalVM";
2424
}
2525

2626
@Override
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
[]
1+
[
2+
{
3+
"name": "graphql.starwars.Human",
4+
"allPublicMethods": true
5+
}
6+
]

tests/src/com.graphql-java/graphql-java/19.2/src/test/resources/graphql/starwars.graphqls

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type QueryType {
1111
hero(episode: Episode): Character!
1212
human(id : String) : Human
1313
droid(id: ID!): Droid
14+
humans: HumanConnection
1415
}
1516

1617
enum Episode {
@@ -23,7 +24,7 @@ type Human implements Character {
2324
id: ID!
2425
name: String!
2526
friends: [Character]
26-
appearsIn: [Episode]!
27+
appearsIn: [Episode]
2728
homePlanet: String
2829
}
2930

@@ -43,3 +44,20 @@ interface Character {
4344
input NewCharacter {
4445
name: String
4546
}
47+
48+
type HumanConnection {
49+
edges: [HumanEdge]
50+
pageInfo: PageInfo!
51+
}
52+
53+
type HumanEdge {
54+
cursor: String!
55+
node: Human
56+
}
57+
58+
type PageInfo {
59+
hasNextPage: Boolean!
60+
hasPreviousPage: Boolean!
61+
startCursor: String
62+
endCursor: String
63+
}

0 commit comments

Comments
 (0)