|
| 1 | +package graphql.schema.idl; |
| 2 | + |
| 3 | +import graphql.Internal; |
| 4 | +import graphql.language.NamedNode; |
| 5 | +import graphql.schema.GraphQLCodeRegistry; |
| 6 | +import graphql.schema.GraphQLEnumType; |
| 7 | +import graphql.schema.GraphQLInputObjectType; |
| 8 | +import graphql.schema.GraphQLInterfaceType; |
| 9 | +import graphql.schema.GraphQLNamedType; |
| 10 | +import graphql.schema.GraphQLObjectType; |
| 11 | +import graphql.schema.GraphQLScalarType; |
| 12 | +import graphql.schema.GraphQLSchema; |
| 13 | +import graphql.schema.GraphQLSchemaElement; |
| 14 | +import graphql.schema.GraphQLTypeVisitorStub; |
| 15 | +import graphql.schema.GraphQLUnionType; |
| 16 | +import graphql.schema.SchemaTransformer; |
| 17 | +import graphql.util.TraversalControl; |
| 18 | +import graphql.util.TraverserContext; |
| 19 | +import graphql.util.TreeTransformerUtil; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.Function; |
| 24 | + |
| 25 | +import static graphql.util.TraversalControl.CONTINUE; |
| 26 | + |
| 27 | +@Internal |
| 28 | +class SchemaDirectiveWiringSchemaGeneratorPostProcessing implements SchemaGeneratorPostProcessing { |
| 29 | + |
| 30 | + private final SchemaGeneratorDirectiveHelper generatorDirectiveHelper = new SchemaGeneratorDirectiveHelper(); |
| 31 | + private final TypeDefinitionRegistry typeRegistry; |
| 32 | + private final RuntimeWiring runtimeWiring; |
| 33 | + private final GraphQLCodeRegistry.Builder codeRegistryBuilder; |
| 34 | + private final Map<String, Object> directiveBehaviourContext = new HashMap<>(); |
| 35 | + |
| 36 | + |
| 37 | + public SchemaDirectiveWiringSchemaGeneratorPostProcessing(TypeDefinitionRegistry typeRegistry, RuntimeWiring runtimeWiring, GraphQLCodeRegistry.Builder codeRegistryBuilder) { |
| 38 | + this.typeRegistry = typeRegistry; |
| 39 | + this.runtimeWiring = runtimeWiring; |
| 40 | + this.codeRegistryBuilder = codeRegistryBuilder; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + @Override |
| 45 | + public GraphQLSchema process(GraphQLSchema originalSchema) { |
| 46 | + GraphQLSchema newSchema = SchemaTransformer.transformSchema(originalSchema, new Visitor()); |
| 47 | + return newSchema.transform(builder -> { |
| 48 | + // they could have changed the code registry so rebuild it |
| 49 | + GraphQLCodeRegistry codeRegistry = this.codeRegistryBuilder.build(); |
| 50 | + builder.codeRegistry(codeRegistry); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + public class Visitor extends GraphQLTypeVisitorStub { |
| 55 | + |
| 56 | + private SchemaGeneratorDirectiveHelper.Parameters mkBehaviourParams() { |
| 57 | + return new SchemaGeneratorDirectiveHelper.Parameters(typeRegistry, runtimeWiring, directiveBehaviourContext, codeRegistryBuilder); |
| 58 | + } |
| 59 | + |
| 60 | + private TraversalControl changOrContinue(GraphQLSchemaElement node, GraphQLSchemaElement newNode, TraverserContext<GraphQLSchemaElement> context) { |
| 61 | + if (node != newNode) { |
| 62 | + TreeTransformerUtil.changeNode(context, newNode); |
| 63 | + } |
| 64 | + return CONTINUE; |
| 65 | + } |
| 66 | + |
| 67 | + private boolean isIntrospectionType(GraphQLNamedType type) { |
| 68 | + return type.getName().startsWith("__"); |
| 69 | + } |
| 70 | + |
| 71 | + private <T extends GraphQLNamedType> boolean notSuitable(T node, Function<T, NamedNode<?>> suitableFunc) { |
| 72 | + if (isIntrospectionType(node)) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + NamedNode<?> definition = suitableFunc.apply(node); |
| 76 | + return definition == null; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public TraversalControl visitGraphQLObjectType(GraphQLObjectType node, TraverserContext<GraphQLSchemaElement> context) { |
| 81 | + if (notSuitable(node, GraphQLObjectType::getDefinition)) { |
| 82 | + return CONTINUE; |
| 83 | + } |
| 84 | + GraphQLSchemaElement newNode = generatorDirectiveHelper.onObject(node, mkBehaviourParams()); |
| 85 | + return changOrContinue(node, newNode, context); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public TraversalControl visitGraphQLInterfaceType(GraphQLInterfaceType node, TraverserContext<GraphQLSchemaElement> context) { |
| 90 | + if (notSuitable(node, GraphQLInterfaceType::getDefinition)) { |
| 91 | + return CONTINUE; |
| 92 | + } |
| 93 | + GraphQLSchemaElement newNode = generatorDirectiveHelper.onInterface(node, mkBehaviourParams()); |
| 94 | + return changOrContinue(node, newNode, context); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public TraversalControl visitGraphQLEnumType(GraphQLEnumType node, TraverserContext<GraphQLSchemaElement> context) { |
| 99 | + if (notSuitable(node, GraphQLEnumType::getDefinition)) { |
| 100 | + return CONTINUE; |
| 101 | + } |
| 102 | + GraphQLSchemaElement newNode = generatorDirectiveHelper.onEnum(node, mkBehaviourParams()); |
| 103 | + return changOrContinue(node, newNode, context); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public TraversalControl visitGraphQLInputObjectType(GraphQLInputObjectType node, TraverserContext<GraphQLSchemaElement> context) { |
| 108 | + if (notSuitable(node, GraphQLInputObjectType::getDefinition)) { |
| 109 | + return CONTINUE; |
| 110 | + } |
| 111 | + GraphQLSchemaElement newNode = generatorDirectiveHelper.onInputObjectType(node, mkBehaviourParams()); |
| 112 | + return changOrContinue(node, newNode, context); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public TraversalControl visitGraphQLScalarType(GraphQLScalarType node, TraverserContext<GraphQLSchemaElement> context) { |
| 117 | + if (notSuitable(node, GraphQLScalarType::getDefinition)) { |
| 118 | + return CONTINUE; |
| 119 | + } |
| 120 | + GraphQLSchemaElement newNode = generatorDirectiveHelper.onScalar(node, mkBehaviourParams()); |
| 121 | + return changOrContinue(node, newNode, context); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public TraversalControl visitGraphQLUnionType(GraphQLUnionType node, TraverserContext<GraphQLSchemaElement> context) { |
| 126 | + if (notSuitable(node, GraphQLUnionType::getDefinition)) { |
| 127 | + return CONTINUE; |
| 128 | + } |
| 129 | + GraphQLSchemaElement newNode = generatorDirectiveHelper.onUnion(node, mkBehaviourParams()); |
| 130 | + return changOrContinue(node, newNode, context); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments