Skip to content

Commit 6445434

Browse files
author
Thomas Draier
committed
Allows to set a custom Relay implementation, in order to add fields in the auto generated types
1 parent a76155f commit 6445434

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

src/main/java/graphql/annotations/GraphQLAnnotations.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,11 @@
8080
@Component
8181
public class GraphQLAnnotations implements GraphQLAnnotationsProcessor {
8282

83-
private static final Relay RELAY_TYPES = new Relay();
84-
8583
private static final List<Class> TYPES_FOR_CONNECTION = Arrays.asList(GraphQLObjectType.class, GraphQLInterfaceType.class, GraphQLUnionType.class, GraphQLTypeReference.class);
8684

8785
private Map<String, graphql.schema.GraphQLType> typeRegistry = new HashMap<>();
8886
private final Stack<String> processing = new Stack<>();
87+
private Relay relay = new Relay();
8988

9089
public GraphQLAnnotations() {
9190
this(new DefaultTypeFunction());
@@ -102,6 +101,10 @@ public static GraphQLAnnotations getInstance() {
102101
return instance;
103102
}
104103

104+
public void setRelay(Relay relay) {
105+
this.relay = relay;
106+
}
107+
105108
@Override
106109
public graphql.schema.GraphQLType getInterface(Class<?> iface) throws GraphQLAnnotationsException {
107110
String typeName = getTypeName(iface);
@@ -502,9 +505,9 @@ private GraphQLOutputType getGraphQLConnection(boolean isConnection, AccessibleO
502505
assert wrappedType instanceof GraphQLObjectType;
503506
String annValue = field.getAnnotation(GraphQLConnection.class).name();
504507
String connectionName = annValue.isEmpty() ? wrappedType.getName() : annValue;
505-
GraphQLObjectType edgeType = RELAY_TYPES.edgeType(connectionName, (GraphQLOutputType) wrappedType, null, Collections.<GraphQLFieldDefinition>emptyList());
506-
outputType = RELAY_TYPES.connectionType(connectionName, edgeType, Collections.emptyList());
507-
builder.argument(RELAY_TYPES.getConnectionFieldArguments());
508+
GraphQLObjectType edgeType = relay.edgeType(connectionName, (GraphQLOutputType) wrappedType, null, Collections.<GraphQLFieldDefinition>emptyList());
509+
outputType = relay.connectionType(connectionName, edgeType, Collections.emptyList());
510+
builder.argument(relay.getConnectionFieldArguments());
508511
}
509512
}
510513
return outputType;
@@ -571,7 +574,7 @@ protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotatio
571574
List<GraphQLFieldDefinition> fieldDefinitions = outputType instanceof GraphQLObjectType ?
572575
((GraphQLObjectType) outputType).getFieldDefinitions() :
573576
((GraphQLInterfaceType) outputType).getFieldDefinitions();
574-
relay = RELAY_TYPES.mutationWithClientMutationId(title, method.getName(),
577+
relay = GraphQLAnnotations.this.relay.mutationWithClientMutationId(title, method.getName(),
575578
args.stream().
576579
map(t -> newInputObjectField().name(t.getName()).type(t.getType()).description(t.getDescription()).build()).
577580
collect(Collectors.toList()), fieldDefinitions, null);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
import graphql.ExecutionResult;
1818
import graphql.GraphQL;
19+
import graphql.annotations.util.CustomRelay;
20+
import graphql.relay.Relay;
1921
import graphql.schema.DataFetchingEnvironment;
22+
import graphql.schema.GraphQLList;
2023
import graphql.schema.GraphQLObjectType;
2124
import graphql.schema.GraphQLSchema;
2225
import org.testng.annotations.Test;
@@ -113,6 +116,25 @@ public void methodList() {
113116

114117
}
115118

119+
@Test
120+
public void customRelayMethodList() {
121+
try {
122+
GraphQLAnnotations.getInstance().setRelay(new CustomRelay());
123+
GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class);
124+
GraphQLSchema schema = newSchema().query(object).build();
125+
126+
graphql.schema.GraphQLObjectType f = (GraphQLObjectType) schema.getType("ObjConnection");
127+
assertTrue(f.getFieldDefinitions().size() == 4);
128+
assertTrue(f.getFieldDefinition("nodes").getType() instanceof GraphQLList);
129+
assertEquals(((GraphQLList)f.getFieldDefinition("nodes").getType()).getWrappedType().getName(), "Obj");
130+
131+
GraphQLObjectType pageInfo = (GraphQLObjectType) schema.getType("PageInfo");
132+
assertTrue(pageInfo.getFieldDefinition("additionalInfo") != null);
133+
} finally {
134+
GraphQLAnnotations.getInstance().setRelay(new Relay());
135+
}
136+
}
137+
116138
public void testResult(String name, ExecutionResult result) {
117139
Map<String, Map<String, List<Map<String, Map<String, Object>>>>> data = result.getData();
118140
List<Map<String, Map<String, Object>>> edges = data.get(name).get("edges");
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations.util;
16+
17+
import graphql.relay.Relay;
18+
import graphql.schema.*;
19+
20+
import java.util.List;
21+
22+
import static graphql.Scalars.GraphQLBoolean;
23+
import static graphql.Scalars.GraphQLInt;
24+
import static graphql.Scalars.GraphQLString;
25+
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
26+
import static graphql.schema.GraphQLObjectType.newObject;
27+
28+
public class CustomRelay extends Relay {
29+
30+
private GraphQLObjectType pageInfoType = newObject()
31+
.name("PageInfo")
32+
.description("Information about pagination in a connection.")
33+
.field(newFieldDefinition()
34+
.name("hasNextPage")
35+
.type(new GraphQLNonNull(GraphQLBoolean))
36+
.description("When paginating forwards, are there more items?"))
37+
.field(newFieldDefinition()
38+
.name("hasPreviousPage")
39+
.type(new GraphQLNonNull(GraphQLBoolean))
40+
.description("When paginating backwards, are there more items?"))
41+
.field(newFieldDefinition()
42+
.name("startCursor")
43+
.type(GraphQLString)
44+
.description("When paginating backwards, the cursor to continue."))
45+
.field(newFieldDefinition()
46+
.name("endCursor")
47+
.type(GraphQLString)
48+
.description("When paginating forwards, the cursor to continue."))
49+
.field(newFieldDefinition()
50+
.name("additionalInfo")
51+
.type(GraphQLString))
52+
.build();
53+
54+
@Override
55+
public GraphQLObjectType connectionType(String name, GraphQLObjectType edgeType, List<GraphQLFieldDefinition> connectionFields) {
56+
return newObject()
57+
.name(name + "Connection")
58+
.description("A connection to a list of items.")
59+
.field(newFieldDefinition()
60+
.name("edges")
61+
.description("a list of edges")
62+
.type(new GraphQLList(edgeType)))
63+
.field(newFieldDefinition()
64+
.name("nodes")
65+
.description("a list of nodes")
66+
.type(new GraphQLList(edgeType.getFieldDefinition("node").getType())))
67+
.field(newFieldDefinition()
68+
.name("pageInfo")
69+
.description("details about this specific page")
70+
.type(new GraphQLNonNull(pageInfoType)))
71+
.field(newFieldDefinition()
72+
.name("totalCount")
73+
.description("number of nodes in connection")
74+
.type(GraphQLInt))
75+
.fields(connectionFields)
76+
.build();
77+
}}

0 commit comments

Comments
 (0)