Skip to content

Commit 6416998

Browse files
author
Thomas Draier
committed
Avoid duplicate definitions of the same types when using connections
1 parent e1ceb93 commit 6416998

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,12 +588,21 @@ private GraphQLOutputType internalGetGraphQLConnection(AccessibleObject field, G
588588
assert wrappedType instanceof GraphQLObjectType;
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());
591+
GraphQLObjectType edgeType = ensureTypeUniqueness(relay.edgeType(connectionName, (GraphQLOutputType) wrappedType, null, Collections.<GraphQLFieldDefinition>emptyList()));
592+
type = ensureTypeUniqueness(relay.connectionType(connectionName, edgeType, Collections.emptyList()));
593593
builder.argument(relay.getConnectionFieldArguments());
594594
return type;
595595
}
596596

597+
private GraphQLObjectType ensureTypeUniqueness(GraphQLObjectType type) {
598+
if (typeRegistry.containsKey(type.getName())) {
599+
type = (GraphQLObjectType) typeRegistry.get(type.getName());
600+
} else {
601+
typeRegistry.put(type.getName(), type);
602+
}
603+
return type;
604+
}
605+
597606
private boolean isConnection(AccessibleObject obj, GraphQLOutputType type) {
598607
if (type instanceof GraphQLNonNull) {
599608
type = (GraphQLOutputType) ((GraphQLNonNull) type).getWrappedType();

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.testng.annotations.Test;
2727

2828
import java.util.Arrays;
29+
import java.util.Collections;
2930
import java.util.List;
3031
import java.util.Map;
3132
import java.util.stream.Stream;
@@ -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,11 @@ public void customConnection() {
270289
assertEquals(edges.size(), 0);
271290
}
272291

292+
@Test
293+
public void duplicateConnection() {
294+
GraphQLObjectType object = GraphQLAnnotations.object(DuplicateTest.class);
295+
GraphQLSchema schema = newSchema().query(object).build();
296+
System.out.println("ok");
297+
}
298+
273299
}

0 commit comments

Comments
 (0)