Skip to content

Commit f740eb7

Browse files
committed
Fix #4200: Allow empty union base definitions when extensions add members
The GraphQL spec grammar makes UnionMemberTypes optional in a UnionTypeDefinition, allowing patterns like: union Pet extend union Pet = Cat | Dog Previously, the UnionTypesChecker rejected empty base definitions before considering extensions. Now it checks for extensions with member types before reporting an error. https://claude.ai/code/session_01JBZ9kgZK35T4G7aPiaymV7
1 parent 121e207 commit f740eb7

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎src/main/java/graphql/schema/idl/UnionTypesChecker.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ private void checkUnionType(TypeDefinitionRegistry typeRegistry, UnionTypeDefini
4545
//noinspection rawtypes
4646
List<Type> memberTypes = unionTypeDefinition.getMemberTypes();
4747
if (memberTypes == null || memberTypes.isEmpty()) {
48+
// For base definitions (not extensions), an empty member list is allowed
49+
// if there are extensions that will add members (per the GraphQL spec, UnionMemberTypes is optional)
50+
if (!(unionTypeDefinition instanceof UnionTypeExtensionDefinition) && hasExtensionsWithMembers(typeRegistry, unionTypeDefinition.getName())) {
51+
return;
52+
}
4853
errors.add(new UnionTypeError(unionTypeDefinition, format("Union type '%s' must include one or more member types.", unionTypeDefinition.getName())));
4954
return;
5055
}
@@ -66,6 +71,14 @@ private void checkUnionType(TypeDefinitionRegistry typeRegistry, UnionTypeDefini
6671
}
6772
}
6873

74+
private boolean hasExtensionsWithMembers(TypeDefinitionRegistry typeRegistry, String unionName) {
75+
List<UnionTypeExtensionDefinition> extensions = typeRegistry.unionTypeExtensions().get(unionName);
76+
if (extensions == null) {
77+
return false;
78+
}
79+
return extensions.stream().anyMatch(ext -> ext.getMemberTypes() != null && !ext.getMemberTypes().isEmpty());
80+
}
81+
6982
private void assertTypeName(UnionTypeDefinition unionTypeDefinition, List<GraphQLError> errors) {
7083
if (unionTypeDefinition.getName().length() >= 2 && unionTypeDefinition.getName().startsWith("__")) {
7184
errors.add((new UnionTypeError(unionTypeDefinition, String.format("'%s' must not begin with '__', which is reserved by GraphQL introspection.", unionTypeDefinition.getName()))));

‎src/test/groovy/graphql/schema/idl/SchemaTypeCheckerTest.groovy‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,15 +1787,34 @@ class SchemaTypeCheckerTest extends Specification {
17871787
errorContaining(result, "Union type 'UnionType' must include one or more member types.")
17881788
}
17891789

1790+
def "empty union base definition is valid when extensions add members"() {
1791+
given:
1792+
def sdl = """
1793+
type Query { hello: String }
1794+
1795+
type Cat { name: String }
1796+
type Dog { name: String }
1797+
1798+
union Pet
1799+
extend union Pet = Cat | Dog
1800+
"""
1801+
1802+
when:
1803+
def result = check(sdl, ["Pet"])
1804+
1805+
then:
1806+
result.isEmpty()
1807+
}
1808+
17901809
def "The member types of a Union type must all be object base types"() {
17911810
given:
17921811
def sdl = """
17931812
type Query { hello: String }
1794-
1813+
17951814
type A { hello: String }
1796-
1815+
17971816
interface B { hello: String }
1798-
1817+
17991818
union UnionType = A | B
18001819
"""
18011820

0 commit comments

Comments
 (0)