|
| 1 | +package graphql.util; |
| 2 | + |
| 3 | +import graphql.ExperimentalApi; |
| 4 | +import graphql.introspection.Introspection; |
| 5 | +import graphql.schema.GraphQLNamedType; |
| 6 | +import graphql.schema.GraphQLSchema; |
| 7 | +import graphql.schema.GraphQLSchemaElement; |
| 8 | +import graphql.schema.GraphQLTypeUtil; |
| 9 | +import graphql.schema.GraphQLTypeVisitorStub; |
| 10 | +import graphql.schema.SchemaTraverser; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.StringJoiner; |
| 15 | + |
| 16 | +/** |
| 17 | + * Finds all cycles in a GraphQL Schema. |
| 18 | + * Cycles caused by built-in introspection types are filtered out. |
| 19 | + */ |
| 20 | +@ExperimentalApi |
| 21 | +public class CyclicSchemaAnalyzer { |
| 22 | + |
| 23 | + public static class SchemaElementWithChild { |
| 24 | + private final GraphQLSchemaElement element; |
| 25 | + private final int childIndex; |
| 26 | + |
| 27 | + public SchemaElementWithChild(GraphQLSchemaElement element, int childIndex) { |
| 28 | + this.element = element; |
| 29 | + this.childIndex = childIndex; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static class SchemaCycle { |
| 34 | + private final List<SchemaElementWithChild> cycleElements = new ArrayList<>(); |
| 35 | + |
| 36 | + public String toString() { |
| 37 | + StringJoiner result = new StringJoiner(" -> "); |
| 38 | + for (SchemaElementWithChild schemaElementWithChild : cycleElements) { |
| 39 | + result.add(GraphQLTypeUtil.simplePrint(schemaElementWithChild.element) + "/" + schemaElementWithChild.childIndex); |
| 40 | + } |
| 41 | + return result.toString(); |
| 42 | + } |
| 43 | + |
| 44 | + public int size() { |
| 45 | + return cycleElements.size(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public static List<SchemaCycle> findCycles(GraphQLSchema schema) { |
| 50 | + SchemaTraverser traverser = new SchemaTraverser(); |
| 51 | + Visitor visitor = new Visitor(); |
| 52 | + traverser.depthFirstFullSchema(visitor, schema); |
| 53 | + List<SchemaCycle> result = new ArrayList<>(); |
| 54 | + for (List<Breadcrumb<GraphQLSchemaElement>> possibleCycle : visitor.cycles) { |
| 55 | + SchemaCycle schemaCycle = new SchemaCycle(); |
| 56 | + // the breadcrumbs are in the reverse order of the dependency |
| 57 | + for (int i = possibleCycle.size() - 1; i >= 0; i--) { |
| 58 | + Breadcrumb<GraphQLSchemaElement> breadcrumb = possibleCycle.get(i); |
| 59 | + SchemaElementWithChild schemaElementWithChild = new SchemaElementWithChild(breadcrumb.getNode(), breadcrumb.getLocation().getIndex()); |
| 60 | + schemaCycle.cycleElements.add(schemaElementWithChild); |
| 61 | + } |
| 62 | + result.add(schemaCycle); |
| 63 | + } |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + private static class Visitor extends GraphQLTypeVisitorStub { |
| 68 | + public final List<List<Breadcrumb<GraphQLSchemaElement>>> cycles = new ArrayList<>(); |
| 69 | + |
| 70 | + @Override |
| 71 | + public TraversalControl visitBackRef(TraverserContext<GraphQLSchemaElement> context) { |
| 72 | + List<Breadcrumb<GraphQLSchemaElement>> breadcrumbs = context.getBreadcrumbs(); |
| 73 | + |
| 74 | + for (int i = breadcrumbs.size() - 1; i >= 0; i--) { |
| 75 | + Breadcrumb<GraphQLSchemaElement> breadcrumb = breadcrumbs.get(i); |
| 76 | + if (breadcrumb.getNode() instanceof GraphQLNamedType && Introspection.isIntrospectionTypes((GraphQLNamedType) breadcrumb.getNode())) { |
| 77 | + return TraversalControl.CONTINUE; |
| 78 | + } |
| 79 | + if (breadcrumb.getNode() == context.thisNode()) { |
| 80 | + List<Breadcrumb<GraphQLSchemaElement>> cycle = breadcrumbs.subList(0, i + 1); |
| 81 | + cycles.add(cycle); |
| 82 | + } |
| 83 | + } |
| 84 | + return TraversalControl.CONTINUE; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +} |
0 commit comments