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
12 changes: 12 additions & 0 deletions src/main/java/graphql/schema/usage/SchemaUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class SchemaUsage {
private final Map<String, Set<String>> interfaceImplementors;
private final Map<String, Set<String>> elementBackReferences;

private final Map<String, Set<String>> unionReferences;

private SchemaUsage(Builder builder) {
this.fieldReferenceCounts = ImmutableMap.copyOf(builder.fieldReferenceCounts);
this.inputFieldReferenceCounts = ImmutableMap.copyOf(builder.inputFieldReferenceCounts);
Expand All @@ -57,6 +59,7 @@ private SchemaUsage(Builder builder) {
this.directiveReferenceCount = ImmutableMap.copyOf(builder.directiveReferenceCount);
this.interfaceImplementors = ImmutableMap.copyOf(builder.interfaceImplementors);
this.elementBackReferences = ImmutableMap.copyOf(builder.elementBackReferences);
this.unionReferences = ImmutableMap.copyOf(builder.unionReferences);
}

/**
Expand Down Expand Up @@ -225,6 +228,13 @@ private boolean isReferencedImpl(GraphQLSchema schema, String elementName, Set<S
}
}
}

Set<String> unionContainers = unionReferences.getOrDefault(type.getName(), emptySet());
for (String unionContainer : unionContainers) {
if (isReferencedImpl(schema, unionContainer, pathSoFar)) {
return true;
}
}
}
return false;
}
Expand All @@ -249,6 +259,8 @@ static class Builder {
Map<String, Integer> unionReferenceCount = new LinkedHashMap<>();
Map<String, Integer> directiveReferenceCount = new LinkedHashMap<>();
Map<String, Set<String>> interfaceImplementors = new LinkedHashMap<>();

Map<String, Set<String>> unionReferences = new LinkedHashMap<>();
Map<String, Set<String>> elementBackReferences = new LinkedHashMap<>();

SchemaUsage build() {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/graphql/schema/usage/SchemaUsageSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public TraversalControl visitGraphQLUnionType(GraphQLUnionType unionType, Traver
List<GraphQLNamedOutputType> members = unionType.getTypes();
for (GraphQLNamedOutputType member : members) {
builder.unionReferenceCount.compute(member.getName(), incCount());
builder.unionReferences.computeIfAbsent(member.getName(), k -> new HashSet<>()).add(unionType.getName());

recordBackReference(unionType, member);
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/groovy/graphql/schema/usage/SchemaUsageSupportTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SchemaUsageSupportTest extends Specification {
f3 : RefUnion1
f4 : RefEnum1
f5 : String
f6 : RefUnion2

f_arg1( arg : RefInput1) : String
f_arg2( arg : [RefInput2]) : String
Expand Down Expand Up @@ -53,6 +54,12 @@ class SchemaUsageSupportTest extends Specification {

union RefUnion1 = Ref1 | Ref2


type RefByUnionOnly1 { f : ID}
type RefByUnionOnly2 { f : ID}

union RefUnion2 = RefByUnionOnly1 | RefByUnionOnly2

enum RefEnum1 { A, B }


Expand Down Expand Up @@ -154,6 +161,10 @@ class SchemaUsageSupportTest extends Specification {

schemaUsage.isStronglyReferenced(schema, "RefUnion1")

schemaUsage.isStronglyReferenced(schema, "RefUnion2")
schemaUsage.isStronglyReferenced(schema, "RefByUnionOnly1")
schemaUsage.isStronglyReferenced(schema, "RefByUnionOnly2")

schemaUsage.isStronglyReferenced(schema, "RefInput1")
schemaUsage.isStronglyReferenced(schema, "RefInput2")
schemaUsage.isStronglyReferenced(schema, "RefInput3")
Expand Down