Skip to content

Commit 5e61bb3

Browse files
authored
Merge pull request #2135 from graphql-java/anonymizer-cyclic
fix handling of cyclic type references
2 parents 3fe482b + e5efab4 commit 5e61bb3

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/main/java/graphql/util/Anonymizer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import graphql.schema.GraphQLSchema;
4646
import graphql.schema.GraphQLSchemaElement;
4747
import graphql.schema.GraphQLType;
48+
import graphql.schema.GraphQLTypeReference;
4849
import graphql.schema.GraphQLTypeVisitor;
4950
import graphql.schema.GraphQLTypeVisitorStub;
5051
import graphql.schema.GraphQLUnionType;
@@ -104,6 +105,15 @@ public static AnonymizeResult anonymizeSchemaAndQueries(GraphQLSchema schema, Li
104105

105106
SchemaTransformer schemaTransformer = new SchemaTransformer();
106107
GraphQLSchema newSchema = schemaTransformer.transform(schema, new GraphQLTypeVisitorStub() {
108+
109+
@Override
110+
public TraversalControl visitGraphQLTypeReference(GraphQLTypeReference graphQLTypeReference, TraverserContext<GraphQLSchemaElement> context) {
111+
GraphQLNamedSchemaElement type = (GraphQLNamedSchemaElement) schema.getType(graphQLTypeReference.getName());
112+
String newName = newNameMap.get(type);
113+
GraphQLTypeReference newReference = GraphQLTypeReference.typeRef(newName);
114+
return changeNode(context, newReference);
115+
}
116+
107117
@Override
108118
public TraversalControl visitGraphQLArgument(GraphQLArgument graphQLArgument, TraverserContext<GraphQLSchemaElement> context) {
109119
String newName = assertNotNull(newNameMap.get(graphQLArgument));

src/test/groovy/graphql/util/AnonymizerTest.groovy

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,40 @@ type Object2 {
449449
then:
450450
newQuery == "query {field2 {__typename alias1:__typename field1}}"
451451
}
452+
453+
def "handles cyclic types"() {
454+
def schema = TestUtil.schema("""
455+
type Query {
456+
query: Foo
457+
}
458+
type Foo {
459+
foo: [Bar]
460+
}
461+
462+
type Bar {
463+
bar: [Foo]
464+
}
465+
""")
466+
when:
467+
def result = Anonymizer.anonymizeSchema(schema)
468+
def newSchema = new SchemaPrinter(SchemaPrinter.Options.defaultOptions().includeDirectiveDefinitions(false)).print(result)
469+
470+
then:
471+
newSchema == """schema {
472+
query: Object1
473+
}
474+
475+
type Object1 {
476+
field1: Object2
477+
}
478+
479+
type Object2 {
480+
field2: [Object3]
481+
}
482+
483+
type Object3 {
484+
field3: [Object2]
485+
}
486+
"""
487+
}
452488
}

0 commit comments

Comments
 (0)