Skip to content

Commit 9229b15

Browse files
authored
Merge pull request #114 from Jahia/duplicate-relay-types
Avoid duplicate definitions of the same types when using connections
2 parents 678cb6b + 8fb8472 commit 9229b15

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ protected GraphQLFieldDefinition getField(Field field) throws GraphQLAnnotations
473473

474474
boolean isConnection = isConnection(field, outputType);
475475
if (isConnection) {
476-
outputType = getGraphQLConnection(field, outputType, builder);
476+
outputType = getGraphQLConnection(field, outputType);
477+
builder.argument(relay.getConnectionFieldArguments());
477478
}
478479

479480
builder.type(outputType);
@@ -574,23 +575,29 @@ private boolean checkIfPrefixGetterExists(Class c, String prefix, String propert
574575
return true;
575576
}
576577

577-
private GraphQLOutputType getGraphQLConnection(AccessibleObject field, GraphQLOutputType type, GraphQLFieldDefinition.Builder builder) {
578+
private GraphQLOutputType getGraphQLConnection(AccessibleObject field, GraphQLOutputType type) {
578579
if (type instanceof GraphQLNonNull) {
579-
type = (GraphQLOutputType) ((GraphQLNonNull) type).getWrappedType();
580-
return new GraphQLNonNull(internalGetGraphQLConnection(field, type, builder));
580+
GraphQLList listType = (GraphQLList) ((GraphQLNonNull) type).getWrappedType();
581+
return new GraphQLNonNull(internalGetGraphQLConnection(field, listType));
581582
} else {
582-
return internalGetGraphQLConnection(field, type, builder);
583+
return internalGetGraphQLConnection(field, (GraphQLList) type);
583584
}
584585
}
585586

586-
private GraphQLOutputType internalGetGraphQLConnection(AccessibleObject field, GraphQLOutputType type, GraphQLFieldDefinition.Builder builder) {
587-
graphql.schema.GraphQLType wrappedType = ((GraphQLList) type).getWrappedType();
588-
assert wrappedType instanceof GraphQLObjectType;
587+
private GraphQLOutputType internalGetGraphQLConnection(AccessibleObject field, GraphQLList listType) {
588+
GraphQLOutputType wrappedType = (GraphQLOutputType) listType.getWrappedType();
589589
String connectionName = field.getAnnotation(GraphQLConnection.class).name();
590590
connectionName = connectionName.isEmpty() ? wrappedType.getName() : connectionName;
591-
GraphQLObjectType edgeType = relay.edgeType(connectionName, (GraphQLOutputType) wrappedType, null, Collections.<GraphQLFieldDefinition>emptyList());
592-
type = relay.connectionType(connectionName, edgeType, Collections.emptyList());
593-
builder.argument(relay.getConnectionFieldArguments());
591+
GraphQLObjectType edgeType = getActualType(relay.edgeType(connectionName, wrappedType, null, Collections.<GraphQLFieldDefinition>emptyList()));
592+
return getActualType(relay.connectionType(connectionName, edgeType, Collections.emptyList()));
593+
}
594+
595+
private GraphQLObjectType getActualType(GraphQLObjectType type) {
596+
if (typeRegistry.containsKey(type.getName())) {
597+
type = (GraphQLObjectType) typeRegistry.get(type.getName());
598+
} else {
599+
typeRegistry.put(type.getName(), type);
600+
}
594601
return type;
595602
}
596603

@@ -630,7 +637,8 @@ protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotatio
630637

631638
boolean isConnection = isConnection(method, outputType);
632639
if (isConnection) {
633-
outputType = getGraphQLConnection(method, outputType, builder);
640+
outputType = getGraphQLConnection(method, outputType);
641+
builder.argument(relay.getConnectionFieldArguments());
634642
}
635643

636644
builder.type(outputType);

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static graphql.schema.GraphQLSchema.newSchema;
3535
import static org.testng.Assert.assertEquals;
3636
import static org.testng.Assert.assertTrue;
37+
import static org.testng.Assert.fail;
3738

3839
@SuppressWarnings("unchecked")
3940
public class GraphQLConnectionTest {
@@ -55,6 +56,14 @@ public Obj(String id, String val) {
5556
}
5657
}
5758

59+
public static class DuplicateTest {
60+
@GraphQLField
61+
public TestListField field1;
62+
63+
@GraphQLField
64+
public TestListField2 field2;
65+
}
66+
5867
public static class TestListField {
5968
@GraphQLField
6069
@GraphQLConnection
@@ -65,6 +74,16 @@ public TestListField(List<Obj> objs) {
6574
}
6675
}
6776

77+
public static class TestListField2 {
78+
@GraphQLField
79+
@GraphQLConnection
80+
public List<Obj> objs;
81+
82+
public TestListField2(List<Obj> objs) {
83+
this.objs = objs;
84+
}
85+
}
86+
6887
@Test
6988
public void fieldList() {
7089
GraphQLObjectType object = GraphQLAnnotations.object(TestListField.class);
@@ -270,4 +289,14 @@ public void customConnection() {
270289
assertEquals(edges.size(), 0);
271290
}
272291

292+
@Test
293+
public void duplicateConnection() {
294+
try {
295+
GraphQLObjectType object = GraphQLAnnotations.object(DuplicateTest.class);
296+
GraphQLSchema schema = newSchema().query(object).build();
297+
} catch (GraphQLAnnotationsException e) {
298+
fail("Schema cannot be created",e);
299+
}
300+
}
301+
273302
}

0 commit comments

Comments
 (0)