-
Notifications
You must be signed in to change notification settings - Fork 1.1k
post processing on schema directive wiring afterr schema is build #2082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
77b91ca
b993ef6
d27b292
fa96def
933117e
cc818fe
be91649
41a391b
f95d792
5844e32
01033bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package graphql.schema.idl; | ||
|
|
||
| import graphql.Internal; | ||
| import graphql.language.NamedNode; | ||
| import graphql.schema.GraphQLCodeRegistry; | ||
| import graphql.schema.GraphQLEnumType; | ||
| import graphql.schema.GraphQLInputObjectType; | ||
| import graphql.schema.GraphQLInterfaceType; | ||
| import graphql.schema.GraphQLNamedType; | ||
| import graphql.schema.GraphQLObjectType; | ||
| import graphql.schema.GraphQLScalarType; | ||
| import graphql.schema.GraphQLSchema; | ||
| import graphql.schema.GraphQLSchemaElement; | ||
| import graphql.schema.GraphQLTypeVisitorStub; | ||
| import graphql.schema.GraphQLUnionType; | ||
| import graphql.schema.SchemaTransformer; | ||
| import graphql.util.TraversalControl; | ||
| import graphql.util.TraverserContext; | ||
| import graphql.util.TreeTransformerUtil; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
|
|
||
| import static graphql.util.TraversalControl.CONTINUE; | ||
|
|
||
| @Internal | ||
| class SchemaDirectiveWiringSchemaGeneratorPostProcessing implements SchemaGeneratorPostProcessing { | ||
|
|
||
| private final SchemaGeneratorDirectiveHelper generatorDirectiveHelper = new SchemaGeneratorDirectiveHelper(); | ||
| private final TypeDefinitionRegistry typeRegistry; | ||
| private final RuntimeWiring runtimeWiring; | ||
| private final GraphQLCodeRegistry.Builder codeRegistryBuilder; | ||
| private final Map<String, Object> directiveBehaviourContext = new HashMap<>(); | ||
|
|
||
|
|
||
| public SchemaDirectiveWiringSchemaGeneratorPostProcessing(TypeDefinitionRegistry typeRegistry, RuntimeWiring runtimeWiring, GraphQLCodeRegistry.Builder codeRegistryBuilder) { | ||
| this.typeRegistry = typeRegistry; | ||
| this.runtimeWiring = runtimeWiring; | ||
| this.codeRegistryBuilder = codeRegistryBuilder; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public GraphQLSchema process(GraphQLSchema originalSchema) { | ||
| GraphQLSchema newSchema = SchemaTransformer.transformSchema(originalSchema, new Visitor()); | ||
| return newSchema.transform(builder -> { | ||
| // they could have changed the code registry so rebuild it | ||
| GraphQLCodeRegistry codeRegistry = this.codeRegistryBuilder.build(); | ||
| builder.codeRegistry(codeRegistry); | ||
| }); | ||
| } | ||
|
|
||
| public class Visitor extends GraphQLTypeVisitorStub { | ||
|
|
||
| private SchemaGeneratorDirectiveHelper.Parameters mkBehaviourParams() { | ||
| return new SchemaGeneratorDirectiveHelper.Parameters(typeRegistry, runtimeWiring, directiveBehaviourContext, codeRegistryBuilder); | ||
| } | ||
|
|
||
| private TraversalControl changOrContinue(GraphQLSchemaElement node, GraphQLSchemaElement newNode, TraverserContext<GraphQLSchemaElement> context) { | ||
| if (node != newNode) { | ||
| TreeTransformerUtil.changeNode(context, newNode); | ||
| } | ||
| return CONTINUE; | ||
| } | ||
|
|
||
| private boolean isIntrospectionType(GraphQLNamedType type) { | ||
| return type.getName().startsWith("__"); | ||
| } | ||
|
|
||
| private <T extends GraphQLNamedType> boolean notSuitable(T node, Function<T, NamedNode<?>> suitableFunc) { | ||
| if (isIntrospectionType(node)) { | ||
| return true; | ||
| } | ||
| NamedNode<?> definition = suitableFunc.apply(node); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the schema directive wiring code makes an assumption we have a SDL AST definition behind it - so it's SDL only
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to restrict it? Is it bad that we restrict it?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want any one tweaking introspection types I think |
||
| return definition == null; | ||
| } | ||
|
|
||
| @Override | ||
| public TraversalControl visitGraphQLObjectType(GraphQLObjectType node, TraverserContext<GraphQLSchemaElement> context) { | ||
| if (notSuitable(node, GraphQLObjectType::getDefinition)) { | ||
| return CONTINUE; | ||
| } | ||
| GraphQLSchemaElement newNode = generatorDirectiveHelper.onObject(node, mkBehaviourParams()); | ||
| return changOrContinue(node, newNode, context); | ||
| } | ||
|
|
||
| @Override | ||
| public TraversalControl visitGraphQLInterfaceType(GraphQLInterfaceType node, TraverserContext<GraphQLSchemaElement> context) { | ||
| if (notSuitable(node, GraphQLInterfaceType::getDefinition)) { | ||
| return CONTINUE; | ||
| } | ||
| GraphQLSchemaElement newNode = generatorDirectiveHelper.onInterface(node, mkBehaviourParams()); | ||
| return changOrContinue(node, newNode, context); | ||
| } | ||
|
|
||
| @Override | ||
| public TraversalControl visitGraphQLEnumType(GraphQLEnumType node, TraverserContext<GraphQLSchemaElement> context) { | ||
| if (notSuitable(node, GraphQLEnumType::getDefinition)) { | ||
| return CONTINUE; | ||
| } | ||
| GraphQLSchemaElement newNode = generatorDirectiveHelper.onEnum(node, mkBehaviourParams()); | ||
| return changOrContinue(node, newNode, context); | ||
| } | ||
|
|
||
| @Override | ||
| public TraversalControl visitGraphQLInputObjectType(GraphQLInputObjectType node, TraverserContext<GraphQLSchemaElement> context) { | ||
| if (notSuitable(node, GraphQLInputObjectType::getDefinition)) { | ||
| return CONTINUE; | ||
| } | ||
| GraphQLSchemaElement newNode = generatorDirectiveHelper.onInputObjectType(node, mkBehaviourParams()); | ||
| return changOrContinue(node, newNode, context); | ||
| } | ||
|
|
||
| @Override | ||
| public TraversalControl visitGraphQLScalarType(GraphQLScalarType node, TraverserContext<GraphQLSchemaElement> context) { | ||
| if (notSuitable(node, GraphQLScalarType::getDefinition)) { | ||
| return CONTINUE; | ||
| } | ||
| GraphQLSchemaElement newNode = generatorDirectiveHelper.onScalar(node, mkBehaviourParams()); | ||
| return changOrContinue(node, newNode, context); | ||
| } | ||
|
|
||
| @Override | ||
| public TraversalControl visitGraphQLUnionType(GraphQLUnionType node, TraverserContext<GraphQLSchemaElement> context) { | ||
| if (notSuitable(node, GraphQLUnionType::getDefinition)) { | ||
| return CONTINUE; | ||
| } | ||
| GraphQLSchemaElement newNode = generatorDirectiveHelper.onUnion(node, mkBehaviourParams()); | ||
| return changOrContinue(node, newNode, context); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prevent IDEA warnings on generics