All Downloads are FREE. Search and download functionalities are using the official Maven repository.

graphql.schema.visitor.GraphQLSchemaVisitorAdapter Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql.schema.visitor;

import graphql.Internal;
import graphql.schema.GraphQLAppliedDirective;
import graphql.schema.GraphQLAppliedDirectiveArgument;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLDirectiveContainer;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLEnumValueDefinition;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLFieldsContainer;
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLNamedInputType;
import graphql.schema.GraphQLNamedOutputType;
import graphql.schema.GraphQLNamedSchemaElement;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchemaElement;
import graphql.schema.GraphQLTypeUtil;
import graphql.schema.GraphQLTypeVisitorStub;
import graphql.schema.GraphQLUnionType;
import graphql.util.TraversalControl;
import graphql.util.TraverserContext;

import java.util.function.Supplier;

import static graphql.schema.visitor.GraphQLSchemaVisitor.FieldDefinitionVisitorEnvironment;
import static graphql.schema.visitor.GraphQLSchemaVisitor.ObjectVisitorEnvironment;

@Internal
class GraphQLSchemaVisitorAdapter extends GraphQLTypeVisitorStub {

    private final GraphQLSchemaVisitor schemaVisitor;

    GraphQLSchemaVisitorAdapter(GraphQLSchemaVisitor schemaVisitor) {
        this.schemaVisitor = schemaVisitor;
    }

    static class SchemaElementEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.SchemaElementVisitorEnvironment {
        public SchemaElementEnv(TraverserContext context) {
            super(context);
        }
    }

    private TraversalControl visitE(TraverserContext context, Supplier visitCall) {

        GraphQLSchemaTraversalControl generalCall = schemaVisitor.visitSchemaElement(context.thisNode(), new SchemaElementEnv(context));
        // if they have changed anything in the general schema element visitation then we don't call the specific visit method
        if (generalCall.isAbortive() || generalCall.isMutative()) {
            return generalCall.toTraversalControl(context);
        }
        GraphQLSchemaTraversalControl specificCall = visitCall.get();
        return specificCall.toTraversalControl(context);
    }

    static class AppliedDirectiveArgumentEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.AppliedDirectiveArgumentVisitorEnvironment {
        public AppliedDirectiveArgumentEnv(TraverserContext context) {
            super(context);
        }

        @Override
        public GraphQLAppliedDirective getContainer() {
            return (GraphQLAppliedDirective) context.getParentNode();
        }

        @Override
        public GraphQLNamedInputType getUnwrappedType() {
            return GraphQLTypeUtil.unwrapAllAs(getElement().getType());
        }
    }

    @Override
    public TraversalControl visitGraphQLAppliedDirectiveArgument(GraphQLAppliedDirectiveArgument node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitAppliedDirectiveArgument(node, new AppliedDirectiveArgumentEnv(context)));
    }

    static class AppliedDirectiveEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.AppliedDirectiveVisitorEnvironment {
        public AppliedDirectiveEnv(TraverserContext context) {
            super(context);
        }

        @Override
        public GraphQLDirectiveContainer getContainer() {
            return (GraphQLDirectiveContainer) context.getParentNode();
        }
    }

    @Override
    public TraversalControl visitGraphQLAppliedDirective(GraphQLAppliedDirective node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitAppliedDirective(node, new AppliedDirectiveEnv(context)));
    }

    static class ArgumentEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.ArgumentVisitorEnvironment {
        public ArgumentEnv(TraverserContext context) {
            super(context);
        }

        @Override
        public GraphQLNamedSchemaElement getContainer() {
            return (GraphQLNamedSchemaElement) context.getParentNode();
        }

        @Override
        public GraphQLNamedInputType getUnwrappedType() {
            return GraphQLTypeUtil.unwrapAllAs(getElement().getType());
        }
    }

    @Override
    public TraversalControl visitGraphQLArgument(GraphQLArgument node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitArgument(node, new ArgumentEnv(context)));
    }


    static class DirectiveEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.DirectiveVisitorEnvironment {
        public DirectiveEnv(TraverserContext context) {
            super(context);
        }
    }

    @Override
    public TraversalControl visitGraphQLDirective(GraphQLDirective node, TraverserContext context) {
        //
        // we only want to visit directive definitions at the schema level
        // this is our chance to fix up the applied directive problem
        // of one class used in two contexts.
        //
        if (context.getParentNode() == null) {
            return visitE(context, () -> schemaVisitor.visitDirective(node, new DirectiveEnv(context)));

        }
        return TraversalControl.CONTINUE;
    }

    static class EnumTypeEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.EnumTypeVisitorEnvironment {
        public EnumTypeEnv(TraverserContext context) {
            super(context);
        }
    }

    @Override
    public TraversalControl visitGraphQLEnumType(GraphQLEnumType node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitEnumType(node, new EnumTypeEnv(context)));
    }

    static class EnumValueDefinitionEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.EnumValueDefinitionVisitorEnvironment {
        public EnumValueDefinitionEnv(TraverserContext context) {
            super(context);
        }

        @Override
        public GraphQLEnumType getContainer() {
            return (GraphQLEnumType) context.getParentNode();
        }
    }

    @Override
    public TraversalControl visitGraphQLEnumValueDefinition(GraphQLEnumValueDefinition node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitEnumValueDefinition(node, new EnumValueDefinitionEnv(context)));
    }

    static class FieldDefinitionEnv extends GraphQLSchemaVisitorEnvironmentImpl implements FieldDefinitionVisitorEnvironment {

        public FieldDefinitionEnv(TraverserContext context) {
            super(context);
        }

        @Override
        public GraphQLFieldsContainer getContainer() {
            return (GraphQLFieldsContainer) context.getParentNode();
        }

        @Override
        public GraphQLNamedOutputType getUnwrappedType() {
            return GraphQLTypeUtil.unwrapAllAs(getElement().getType());
        }
    }

    @Override
    public TraversalControl visitGraphQLFieldDefinition(GraphQLFieldDefinition node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitFieldDefinition(node, new FieldDefinitionEnv(context)));
    }

    static class InputObjectFieldEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.InputObjectFieldVisitorEnvironment {
        public InputObjectFieldEnv(TraverserContext context) {
            super(context);
        }

        @Override
        public GraphQLInputObjectType getContainer() {
            return (GraphQLInputObjectType) context.getParentNode();
        }

        @Override
        public GraphQLNamedInputType getUnwrappedType() {
            return GraphQLTypeUtil.unwrapAllAs(getElement().getType());
        }
    }

    @Override
    public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitInputObjectField(node, new InputObjectFieldEnv(context)));
    }

    static class InputObjectTypeEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.InputObjectTypeVisitorEnvironment {
        public InputObjectTypeEnv(TraverserContext context) {
            super(context);
        }
    }

    @Override
    public TraversalControl visitGraphQLInputObjectType(GraphQLInputObjectType node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitInputObjectType(node, new InputObjectTypeEnv(context)));
    }


    static class InterfaceTypeEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.InterfaceTypeVisitorEnvironment {
        public InterfaceTypeEnv(TraverserContext context) {
            super(context);
        }
    }

    @Override
    public TraversalControl visitGraphQLInterfaceType(GraphQLInterfaceType node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitInterfaceType(node, new InterfaceTypeEnv(context)));
    }

    static class ObjectEnv extends GraphQLSchemaVisitorEnvironmentImpl implements ObjectVisitorEnvironment {
        public ObjectEnv(TraverserContext context) {
            super(context);
        }

    }

    @Override
    public TraversalControl visitGraphQLObjectType(GraphQLObjectType node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitObjectType(node, new ObjectEnv(context)));
    }


    static class ScalarTypeEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.ScalarTypeVisitorEnvironment {
        public ScalarTypeEnv(TraverserContext context) {
            super(context);
        }
    }

    @Override
    public TraversalControl visitGraphQLScalarType(GraphQLScalarType node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitScalarType(node, new ScalarTypeEnv(context)));
    }

    static class UnionTypeEnv extends GraphQLSchemaVisitorEnvironmentImpl implements GraphQLSchemaVisitor.UnionTypeVisitorEnvironment {
        public UnionTypeEnv(TraverserContext context) {
            super(context);
        }
    }

    @Override
    public TraversalControl visitGraphQLUnionType(GraphQLUnionType node, TraverserContext context) {
        return visitE(context, () -> schemaVisitor.visitUnionType(node, new UnionTypeEnv(context)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy