Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.function.UnaryOperator;

import static graphql.Assert.assertNotNull;
import static graphql.schema.GraphQLTypeUtil.unwrapAll;
import static graphql.schema.GraphQLTypeUtil.unwrapAllAs;
import static graphql.schema.GraphqlTypeComparatorEnvironment.newEnvironment;

/**
Expand All @@ -33,6 +33,7 @@ public class DefaultGraphqlTypeComparatorRegistry implements GraphqlTypeComparat

/**
* This orders the schema into a sensible grouped order
*
* @return a comparator that allows for sensible grouped order
*/
public static Comparator<GraphQLSchemaElement> sensibleGroupedOrder() {
Expand All @@ -51,15 +52,19 @@ public static Comparator<GraphQLSchemaElement> sensibleGroupedOrder() {

private static GraphQLSchemaElement unwrapElement(GraphQLSchemaElement element) {
if (element instanceof GraphQLType) {
element = unwrapAll((GraphQLType) element);
GraphQLType castElement = (GraphQLType) element;
// We need to unwrap as GraphQLType to support GraphQLTypeReferences which is not an GraphQLUnmodifiedType
// as returned by unwrapAll.
castElement = unwrapAllAs(castElement);
element = castElement;
}
return element;
}

private static int compareByName(GraphQLSchemaElement o1, GraphQLSchemaElement o2) {
return Comparator.comparing(element -> {
if (element instanceof GraphQLType) {
element = unwrapAll((GraphQLType) element);
element = unwrapElement((GraphQLType) element);
}
if (element instanceof GraphQLNamedSchemaElement) {
return ((GraphQLNamedSchemaElement) element).getName();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/graphql/schema/GraphQLTypeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public static <T extends GraphQLType> T unwrapOneAs(GraphQLType type) {

/**
* Unwraps all layers of the type or just returns the type again if it's not a wrapped type
* NOTE: This method does not support GraphQLTypeReference as input and will lead to a ClassCastException
*
* @param type the type to unwrapOne
*
Expand Down
26 changes: 26 additions & 0 deletions src/test/groovy/graphql/Issue3434.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package graphql

import static graphql.schema.GraphQLUnionType.newUnionType
import static graphql.schema.GraphQLTypeReference.typeRef
import graphql.schema.idl.SchemaPrinter

import spock.lang.Specification

class Issue3434 extends Specification {

def "allow printing of union types"() {
given:
def schema = newUnionType().name("Shape")
.possibleType(typeRef("Circle"))
.possibleType(typeRef("Square"))
.build()

when:
def printer = new SchemaPrinter()
def result = printer.print(schema)

then:
result.trim() == "union Shape = Circle | Square"
}
}