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
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
branches:
- master
jobs:
buildAndPublish:
buildAndTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/graphql/schema/SchemaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ Map<String, List<GraphQLObjectType>> groupImplementations(GraphQLSchema schema)
return ImmutableMap.copyOf(new TreeMap<>(result));
}

public Map<String, List<GraphQLImplementingType>> groupImplementationsForInterfacesAndObjects(GraphQLSchema schema) {
Map<String, List<GraphQLImplementingType>> result = new LinkedHashMap<>();
for (GraphQLType type : schema.getAllTypesAsList()) {
if (type instanceof GraphQLImplementingType) {
List<GraphQLNamedOutputType> interfaces = ((GraphQLImplementingType) type).getInterfaces();
for (GraphQLNamedOutputType interfaceType : interfaces) {
List<GraphQLImplementingType> myGroup = result.computeIfAbsent(interfaceType.getName(), k -> new ArrayList<>());
myGroup.add((GraphQLImplementingType) type);
}
}
}
return ImmutableMap.copyOf(new TreeMap<>(result));
}

/**
* This method is deprecated due to a performance concern.
*
Expand Down
54 changes: 49 additions & 5 deletions src/main/java/graphql/util/Anonymizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import graphql.schema.GraphQLTypeVisitorStub;
import graphql.schema.GraphQLUnionType;
import graphql.schema.SchemaTransformer;
import graphql.schema.SchemaUtil;
import graphql.schema.TypeResolver;
import graphql.schema.idl.DirectiveInfo;
import graphql.schema.idl.ScalarInfo;
Expand Down Expand Up @@ -297,6 +298,9 @@ public static Map<GraphQLNamedSchemaElement, String> recordNewNames(GraphQLSchem

Map<GraphQLNamedSchemaElement, String> newNameMap = new LinkedHashMap<>();

Map<String, List<GraphQLImplementingType>> interfaceToImplementations =
new SchemaUtil().groupImplementationsForInterfacesAndObjects(schema);

GraphQLTypeVisitor visitor = new GraphQLTypeVisitorStub() {
@Override
public TraversalControl visitGraphQLArgument(GraphQLArgument graphQLArgument, TraverserContext<GraphQLSchemaElement> context) {
Expand Down Expand Up @@ -366,16 +370,17 @@ public TraversalControl visitGraphQLFieldDefinition(GraphQLFieldDefinition graph
String curName = graphQLFieldDefinition.getName();
GraphQLImplementingType parentNode = (GraphQLImplementingType) context.getParentNode();
List<GraphQLNamedOutputType> interfaces = parentNode.getInterfaces();
List<GraphQLFieldDefinition> matchingInterfaceFieldDefinitions = getMatchingInterfaceFieldDefinitions(curName, interfaces);
List<GraphQLFieldDefinition> sameFields = getMatchingInterfaceFieldDefinitions(curName, interfaces);
sameFields.addAll(getMatchingImplementationsFieldDefinitions(parentNode.getName(), curName, interfaceToImplementations));
String newName;
if (matchingInterfaceFieldDefinitions.size() == 0) {
if (sameFields.size() == 0) {
newName = "field" + fieldCounter.getAndIncrement();
} else {
if (newNameMap.containsKey(matchingInterfaceFieldDefinitions.get(0))) {
newName = newNameMap.get(matchingInterfaceFieldDefinitions.get(0));
if (newNameMap.containsKey(sameFields.get(0))) {
newName = newNameMap.get(sameFields.get(0));
} else {
newName = "field" + fieldCounter.getAndIncrement();
for (GraphQLFieldDefinition fieldDefinition : matchingInterfaceFieldDefinitions) {
for (GraphQLFieldDefinition fieldDefinition : sameFields) {
newNameMap.put(fieldDefinition, newName);
}
}
Expand Down Expand Up @@ -461,6 +466,23 @@ private static List<GraphQLFieldDefinition> getMatchingInterfaceFieldDefinitions
return matchingInterfaceFieldDefinitions;
}

private static List<GraphQLFieldDefinition> getMatchingImplementationsFieldDefinitions(
String interfaceName,
String fieldName,
Map<String, List<GraphQLImplementingType>> interfaceToImplementations) {
List<GraphQLFieldDefinition> result = new ArrayList<>();
List<GraphQLImplementingType> implementations = interfaceToImplementations.get(interfaceName);
if (implementations == null) {
return Collections.emptyList();
}
for (GraphQLImplementingType implementingType : implementations) {
if (implementingType.getFieldDefinition(fieldName) != null) {
result.add(implementingType.getFieldDefinition(fieldName));
}
}
return result;
}

private static List<GraphQLArgument> getMatchingArgumentDefinitions(
String name,
List<GraphQLFieldDefinition> fieldDefinitions) {
Expand Down Expand Up @@ -613,6 +635,27 @@ public TraversalControl visitArgument(Argument node, TraverserContext<Node> cont
return AstPrinter.printAstCompact(newDocument);
}

// private void findAllTheSameFields(GraphQLSchema schema) {
// Map<GraphQLFieldDefinition, Collection<GraphQLFieldDefinition>> sameFields = new LinkedHashMap<>();
//
// GraphQLTypeVisitor visitor = new GraphQLTypeVisitorStub() {
// @Override
// public TraversalControl visitGraphQLFieldDefinition(GraphQLFieldDefinition graphQLFieldDefinition, TraverserContext<GraphQLSchemaElement> context) {
// String curName = graphQLFieldDefinition.getName();
// GraphQLImplementingType parentNode = (GraphQLImplementingType) context.getParentNode();
// List<GraphQLNamedOutputType> interfaces = parentNode.getInterfaces();
// List<GraphQLFieldDefinition> matchingInterfaceFieldDefinitions = getMatchingInterfaceFieldDefinitions(curName, interfaces);
// if (matchingInterfaceFieldDefinitions.size() > 0) {
// sameFields.put(graphQLFieldDefinition, matchingInterfaceFieldDefinitions);
// }
// }
//
// };
//
// SchemaTransformer.transformSchema(schema, visitor);
//
// }

private static void assertUniqueOperation(Document document) {
String operationName = null;
for (Definition definition : document.getDefinitions()) {
Expand All @@ -624,6 +667,7 @@ private static void assertUniqueOperation(Document document) {
operationName = operationDefinition.getName();
}
}

}

}
52 changes: 52 additions & 0 deletions src/test/groovy/graphql/util/AnonymizerTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,58 @@ type Object2 {
type Object1 {
field1: String @deprecated
}
"""
}

def "same field across hierarchy"() {
def schema = TestUtil.schema("""
type Query {
foo: Object
}
interface Interface1 implements Interface2 & Interface3 {
id: ID!
}

interface Interface2 {
id: ID!
}

interface Interface3 {
id: ID!
}
type Object implements Interface1 & Interface2 & Interface3 {
id: ID!
}

""")
when:
def result = Anonymizer.anonymizeSchema(schema)
def newSchema = new SchemaPrinter(SchemaPrinter.Options.defaultOptions().includeDirectiveDefinitions(false)).print(result)

then:
newSchema == """schema {
query: Object1
}

interface Interface1 implements Interface2 & Interface3 {
field2: ID!
}

interface Interface2 {
field2: ID!
}

interface Interface3 {
field2: ID!
}

type Object1 {
field1: Object2
}

type Object2 implements Interface1 & Interface2 & Interface3 {
field2: ID!
}
"""
}
}