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

graphql.execution.Execution Maven / Gradle / Ivy

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


import graphql.ExecutionResult;
import graphql.GraphQLException;
import graphql.language.Document;
import graphql.language.Field;
import graphql.language.OperationDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Execution {

    private FieldCollector fieldCollector = new FieldCollector();
    private ExecutionStrategy strategy;

    public Execution(ExecutionStrategy strategy) {
        this.strategy = strategy;

        if (this.strategy == null) {
            this.strategy = new SimpleExecutionStrategy();
        }
    }

    public ExecutionResult execute(GraphQLSchema graphQLSchema, Object root, Document document, String operationName, Map args) {
        ExecutionContextBuilder executionContextBuilder = new ExecutionContextBuilder(new ValuesResolver());
        ExecutionContext executionContext = executionContextBuilder.build(graphQLSchema, strategy, root, document, operationName, args);
        return executeOperation(executionContext, root, executionContext.getOperationDefinition());
    }

    private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition operationDefinition) {
        if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) {
            return graphQLSchema.getMutationType();

        } else if (operationDefinition.getOperation() == OperationDefinition.Operation.QUERY) {
            return graphQLSchema.getQueryType();

        } else {
            throw new GraphQLException();
        }
    }

    private ExecutionResult executeOperation(
            ExecutionContext executionContext,
            Object root,
            OperationDefinition operationDefinition) {
        GraphQLObjectType operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operationDefinition);

        Map> fields = new LinkedHashMap>();
        fieldCollector.collectFields(executionContext, operationRootType, operationDefinition.getSelectionSet(), new ArrayList(), fields);

        if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) {
            return new SimpleExecutionStrategy().execute(executionContext, operationRootType, root, fields);
        } else {
            return strategy.execute(executionContext, operationRootType, root, fields);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy