Search in sources :

Example 1 with OperationDefinition

use of graphql.language.OperationDefinition in project graphql-java by graphql-java.

the class GraphqlAntlrToLanguage method visitOperationDefinition.

@Override
public Void visitOperationDefinition(GraphqlParser.OperationDefinitionContext ctx) {
    OperationDefinition operationDefinition;
    operationDefinition = new OperationDefinition();
    newNode(operationDefinition, ctx);
    if (ctx.operationType() == null) {
        operationDefinition.setOperation(OperationDefinition.Operation.QUERY);
    } else {
        operationDefinition.setOperation(parseOperation(ctx.operationType()));
    }
    if (ctx.name() != null) {
        operationDefinition.setName(ctx.name().getText());
    }
    result.getDefinitions().add(operationDefinition);
    addContextProperty(ContextProperty.OperationDefinition, operationDefinition);
    super.visitOperationDefinition(ctx);
    popContext();
    return null;
}
Also used : OperationDefinition(graphql.language.OperationDefinition)

Example 2 with OperationDefinition

use of graphql.language.OperationDefinition in project graphql-java by graphql-java.

the class GraphqlAntlrToLanguage method visitVariableDefinition.

@Override
public Void visitVariableDefinition(GraphqlParser.VariableDefinitionContext ctx) {
    VariableDefinition variableDefinition = new VariableDefinition();
    newNode(variableDefinition, ctx);
    variableDefinition.setName(ctx.variable().name().getText());
    if (ctx.defaultValue() != null) {
        Value value = getValue(ctx.defaultValue().value());
        variableDefinition.setDefaultValue(value);
    }
    OperationDefinition operationDefinition = (OperationDefinition) getFromContextStack(ContextProperty.OperationDefinition);
    operationDefinition.getVariableDefinitions().add(variableDefinition);
    addContextProperty(ContextProperty.VariableDefinition, variableDefinition);
    super.visitVariableDefinition(ctx);
    popContext();
    return null;
}
Also used : VariableDefinition(graphql.language.VariableDefinition) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) ArrayValue(graphql.language.ArrayValue) ObjectValue(graphql.language.ObjectValue) EnumValue(graphql.language.EnumValue) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) BooleanValue(graphql.language.BooleanValue) OperationDefinition(graphql.language.OperationDefinition)

Example 3 with OperationDefinition

use of graphql.language.OperationDefinition in project graphql-java by graphql-java.

the class Execution method execute.

public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSchema graphQLSchema, ExecutionId executionId, ExecutionInput executionInput, InstrumentationState instrumentationState) {
    NodeUtil.GetOperationResult getOperationResult = NodeUtil.getOperation(document, executionInput.getOperationName());
    Map<String, FragmentDefinition> fragmentsByName = getOperationResult.fragmentsByName;
    OperationDefinition operationDefinition = getOperationResult.operationDefinition;
    ValuesResolver valuesResolver = new ValuesResolver();
    Map<String, Object> inputVariables = executionInput.getVariables();
    List<VariableDefinition> variableDefinitions = operationDefinition.getVariableDefinitions();
    Map<String, Object> coercedVariables;
    try {
        coercedVariables = valuesResolver.coerceArgumentValues(graphQLSchema, variableDefinitions, inputVariables);
    } catch (RuntimeException rte) {
        if (rte instanceof GraphQLError) {
            return completedFuture(new ExecutionResultImpl((GraphQLError) rte));
        }
        throw rte;
    }
    ExecutionContext executionContext = newExecutionContextBuilder().instrumentation(instrumentation).instrumentationState(instrumentationState).executionId(executionId).graphQLSchema(graphQLSchema).queryStrategy(queryStrategy).mutationStrategy(mutationStrategy).subscriptionStrategy(subscriptionStrategy).context(executionInput.getContext()).root(executionInput.getRoot()).fragmentsByName(fragmentsByName).variables(coercedVariables).document(document).operationDefinition(operationDefinition).build();
    InstrumentationExecutionParameters parameters = new InstrumentationExecutionParameters(executionInput, graphQLSchema, instrumentationState);
    executionContext = instrumentation.instrumentExecutionContext(executionContext, parameters);
    return executeOperation(executionContext, parameters, executionInput.getRoot(), executionContext.getOperationDefinition());
}
Also used : FragmentDefinition(graphql.language.FragmentDefinition) VariableDefinition(graphql.language.VariableDefinition) NodeUtil(graphql.language.NodeUtil) ExecutionResultImpl(graphql.ExecutionResultImpl) GraphQLError(graphql.GraphQLError) InstrumentationExecutionParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters) OperationDefinition(graphql.language.OperationDefinition)

Example 4 with OperationDefinition

use of graphql.language.OperationDefinition in project graphql-java by graphql-java.

the class Execution method executeOperation.

private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext executionContext, InstrumentationExecutionParameters instrumentationExecutionParameters, Object root, OperationDefinition operationDefinition) {
    InstrumentationExecuteOperationParameters instrumentationParams = new InstrumentationExecuteOperationParameters(executionContext);
    InstrumentationContext<ExecutionResult> executeOperationCtx = instrumentation.beginExecuteOperation(instrumentationParams);
    OperationDefinition.Operation operation = operationDefinition.getOperation();
    GraphQLObjectType operationRootType;
    try {
        operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operationDefinition);
    } catch (RuntimeException rte) {
        if (rte instanceof GraphQLError) {
            ExecutionResult executionResult = new ExecutionResultImpl(Collections.singletonList((GraphQLError) rte));
            CompletableFuture<ExecutionResult> resultCompletableFuture = completedFuture(executionResult);
            executeOperationCtx.onDispatched(resultCompletableFuture);
            executeOperationCtx.onCompleted(executionResult, rte);
            return resultCompletableFuture;
        }
        throw rte;
    }
    FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters().schema(executionContext.getGraphQLSchema()).objectType(operationRootType).fragments(executionContext.getFragmentsByName()).variables(executionContext.getVariables()).build();
    Map<String, List<Field>> fields = fieldCollector.collectFields(collectorParameters, operationDefinition.getSelectionSet());
    ExecutionPath path = ExecutionPath.rootPath();
    ExecutionTypeInfo typeInfo = newTypeInfo().type(operationRootType).path(path).build();
    NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, typeInfo);
    ExecutionStrategyParameters parameters = newParameters().typeInfo(typeInfo).source(root).fields(fields).nonNullFieldValidator(nonNullableFieldValidator).path(path).build();
    CompletableFuture<ExecutionResult> result;
    try {
        ExecutionStrategy executionStrategy;
        if (operation == OperationDefinition.Operation.MUTATION) {
            executionStrategy = mutationStrategy;
        } else if (operation == SUBSCRIPTION) {
            executionStrategy = subscriptionStrategy;
        } else {
            executionStrategy = queryStrategy;
        }
        log.debug("Executing '{}' query operation: '{}' using '{}' execution strategy", executionContext.getExecutionId(), operation, executionStrategy.getClass().getName());
        result = executionStrategy.execute(executionContext, parameters);
    } catch (NonNullableFieldWasNullException e) {
        // this means it was non null types all the way from an offending non null type
        // up to the root object type and there was a a null value some where.
        // 
        // The spec says we should return null for the data in this case
        // 
        // http://facebook.github.io/graphql/#sec-Errors-and-Non-Nullability
        // 
        result = completedFuture(new ExecutionResultImpl(null, executionContext.getErrors()));
    }
    // note this happens NOW - not when the result completes
    executeOperationCtx.onDispatched(result);
    result = result.whenComplete(executeOperationCtx::onCompleted);
    return deferSupport(executionContext, result);
}
Also used : ExecutionResult(graphql.ExecutionResult) InstrumentationExecuteOperationParameters(graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionResultImpl(graphql.ExecutionResultImpl) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLError(graphql.GraphQLError) List(java.util.List) OperationDefinition(graphql.language.OperationDefinition)

Example 5 with OperationDefinition

use of graphql.language.OperationDefinition in project structr by structr.

the class GraphQLRequest method initialize.

// ----- private methods -----
private void initialize(final SecurityContext securityContext, Document document) throws FrameworkException {
    for (final Node child : document.getChildren()) {
        if (child instanceof OperationDefinition) {
            final OperationDefinition operationDefinition = (OperationDefinition) child;
            final SelectionSet selectionSet = operationDefinition.getSelectionSet();
            for (final Selection selection : selectionSet.getSelections()) {
                if (selection instanceof Field) {
                    queries.add(new GraphQLQuery(securityContext, (Field) selection));
                } else {
                    logger.warn("Unknown selection set element {} in GraphQL query, ignoring.", selection.getClass());
                }
            }
        } else {
            logger.warn("Unknown document element {} in GraphQL query, ignoring.", child.getClass());
        }
    }
}
Also used : Field(graphql.language.Field) SelectionSet(graphql.language.SelectionSet) Selection(graphql.language.Selection) Node(graphql.language.Node) OperationDefinition(graphql.language.OperationDefinition)

Aggregations

OperationDefinition (graphql.language.OperationDefinition)5 ExecutionResultImpl (graphql.ExecutionResultImpl)2 GraphQLError (graphql.GraphQLError)2 VariableDefinition (graphql.language.VariableDefinition)2 ExecutionResult (graphql.ExecutionResult)1 InstrumentationExecuteOperationParameters (graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters)1 InstrumentationExecutionParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters)1 ArrayValue (graphql.language.ArrayValue)1 BooleanValue (graphql.language.BooleanValue)1 EnumValue (graphql.language.EnumValue)1 Field (graphql.language.Field)1 FloatValue (graphql.language.FloatValue)1 FragmentDefinition (graphql.language.FragmentDefinition)1 IntValue (graphql.language.IntValue)1 Node (graphql.language.Node)1 NodeUtil (graphql.language.NodeUtil)1 ObjectValue (graphql.language.ObjectValue)1 Selection (graphql.language.Selection)1 SelectionSet (graphql.language.SelectionSet)1 StringValue (graphql.language.StringValue)1