Skip to content

Commit 768e507

Browse files
authored
Merge pull request #3506 from bhabegger/fix-issue-3434
Fix printing of union types
2 parents 4d817c2 + 07c909e commit 768e507

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

src/main/java/graphql/schema/DefaultGraphqlTypeComparatorRegistry.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.function.UnaryOperator;
1111

1212
import static graphql.Assert.assertNotNull;
13-
import static graphql.schema.GraphQLTypeUtil.unwrapAll;
13+
import static graphql.schema.GraphQLTypeUtil.unwrapAllAs;
1414
import static graphql.schema.GraphqlTypeComparatorEnvironment.newEnvironment;
1515

1616
/**
@@ -33,6 +33,7 @@ public class DefaultGraphqlTypeComparatorRegistry implements GraphqlTypeComparat
3333

3434
/**
3535
* This orders the schema into a sensible grouped order
36+
*
3637
* @return a comparator that allows for sensible grouped order
3738
*/
3839
public static Comparator<GraphQLSchemaElement> sensibleGroupedOrder() {
@@ -51,15 +52,19 @@ public static Comparator<GraphQLSchemaElement> sensibleGroupedOrder() {
5152

5253
private static GraphQLSchemaElement unwrapElement(GraphQLSchemaElement element) {
5354
if (element instanceof GraphQLType) {
54-
element = unwrapAll((GraphQLType) element);
55+
GraphQLType castElement = (GraphQLType) element;
56+
// We need to unwrap as GraphQLType to support GraphQLTypeReferences which is not an GraphQLUnmodifiedType
57+
// as returned by unwrapAll.
58+
castElement = unwrapAllAs(castElement);
59+
element = castElement;
5560
}
5661
return element;
5762
}
5863

5964
private static int compareByName(GraphQLSchemaElement o1, GraphQLSchemaElement o2) {
6065
return Comparator.comparing(element -> {
6166
if (element instanceof GraphQLType) {
62-
element = unwrapAll((GraphQLType) element);
67+
element = unwrapElement((GraphQLType) element);
6368
}
6469
if (element instanceof GraphQLNamedSchemaElement) {
6570
return ((GraphQLNamedSchemaElement) element).getName();

src/main/java/graphql/schema/GraphQLTypeUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public static <T extends GraphQLType> T unwrapOneAs(GraphQLType type) {
184184

185185
/**
186186
* Unwraps all layers of the type or just returns the type again if it's not a wrapped type
187+
* NOTE: This method does not support GraphQLTypeReference as input and will lead to a ClassCastException
187188
*
188189
* @param type the type to unwrapOne
189190
*
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package graphql
2+
3+
import static graphql.schema.GraphQLUnionType.newUnionType
4+
import static graphql.schema.GraphQLTypeReference.typeRef
5+
import graphql.schema.idl.SchemaPrinter
6+
7+
import spock.lang.Specification
8+
9+
class Issue3434 extends Specification {
10+
11+
def "allow printing of union types"() {
12+
given:
13+
def schema = newUnionType().name("Shape")
14+
.possibleType(typeRef("Circle"))
15+
.possibleType(typeRef("Square"))
16+
.build()
17+
18+
when:
19+
def printer = new SchemaPrinter()
20+
def result = printer.print(schema)
21+
22+
then:
23+
result.trim() == "union Shape = Circle | Square"
24+
}
25+
}
26+

0 commit comments

Comments
 (0)