Skip to content

Commit 7bfd8ce

Browse files
author
Thomas Draier
committed
Simplified methods to create connection type
1 parent 6416998 commit 7bfd8ce

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

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

Lines changed: 12 additions & 13 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,24 +575,21 @@ 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 = ensureTypeUniqueness(relay.edgeType(connectionName, (GraphQLOutputType) wrappedType, null, Collections.<GraphQLFieldDefinition>emptyList()));
592-
type = ensureTypeUniqueness(relay.connectionType(connectionName, edgeType, Collections.emptyList()));
593-
builder.argument(relay.getConnectionFieldArguments());
594-
return type;
591+
GraphQLObjectType edgeType = ensureTypeUniqueness(relay.edgeType(connectionName, wrappedType, null, Collections.<GraphQLFieldDefinition>emptyList()));
592+
return ensureTypeUniqueness(relay.connectionType(connectionName, edgeType, Collections.emptyList()));
595593
}
596594

597595
private GraphQLObjectType ensureTypeUniqueness(GraphQLObjectType type) {
@@ -639,7 +637,8 @@ protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotatio
639637

640638
boolean isConnection = isConnection(method, outputType);
641639
if (isConnection) {
642-
outputType = getGraphQLConnection(method, outputType, builder);
640+
outputType = getGraphQLConnection(method, outputType);
641+
builder.argument(relay.getConnectionFieldArguments());
643642
}
644643

645644
builder.type(outputType);

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

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

2828
import java.util.Arrays;
29-
import java.util.Collections;
3029
import java.util.List;
3130
import java.util.Map;
3231
import java.util.stream.Stream;
@@ -35,6 +34,7 @@
3534
import static graphql.schema.GraphQLSchema.newSchema;
3635
import static org.testng.Assert.assertEquals;
3736
import static org.testng.Assert.assertTrue;
37+
import static org.testng.Assert.fail;
3838

3939
@SuppressWarnings("unchecked")
4040
public class GraphQLConnectionTest {
@@ -291,9 +291,12 @@ public void customConnection() {
291291

292292
@Test
293293
public void duplicateConnection() {
294-
GraphQLObjectType object = GraphQLAnnotations.object(DuplicateTest.class);
295-
GraphQLSchema schema = newSchema().query(object).build();
296-
System.out.println("ok");
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+
}
297300
}
298301

299302
}

0 commit comments

Comments
 (0)