Search in sources :

Example 6 with ExecutionResultImpl

use of graphql.ExecutionResultImpl 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 7 with ExecutionResultImpl

use of graphql.ExecutionResultImpl in project graphql-java by graphql-java.

the class ExecutionStrategy method completeValueForEnum.

/**
 * Called to turn an object into a enum value according to the {@link GraphQLEnumType} by asking that enum type to coerce the object into a valid value
 *
 * @param executionContext contains the top level execution parameters
 * @param parameters       contains the parameters holding the fields to be executed and source object
 * @param enumType         the type of the enum
 * @param result           the result to be coerced
 *
 * @return an {@link ExecutionResult}
 */
protected CompletableFuture<ExecutionResult> completeValueForEnum(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLEnumType enumType, Object result) {
    Object serialized;
    try {
        serialized = enumType.getCoercing().serialize(result);
    } catch (CoercingSerializeException e) {
        serialized = handleCoercionProblem(executionContext, parameters, e);
    }
    serialized = parameters.getNonNullFieldValidator().validate(parameters.getPath(), serialized);
    return completedFuture(new ExecutionResultImpl(serialized, null));
}
Also used : ExecutionResultImpl(graphql.ExecutionResultImpl) CoercingSerializeException(graphql.schema.CoercingSerializeException)

Example 8 with ExecutionResultImpl

use of graphql.ExecutionResultImpl in project graphql-java by graphql-java.

the class ExecutionStrategy method completeValueForList.

/**
 * Called to complete a list of value for a field based on a list type.  This iterates the values and calls
 * {@link #completeValue(ExecutionContext, ExecutionStrategyParameters)} for each value.
 *
 * @param executionContext contains the top level execution parameters
 * @param parameters       contains the parameters holding the fields to be executed and source object
 * @param iterableValues   the values to complete, can't be null
 *
 * @return an {@link ExecutionResult}
 */
protected CompletableFuture<ExecutionResult> completeValueForList(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Iterable<Object> iterableValues) {
    ExecutionTypeInfo typeInfo = parameters.getTypeInfo();
    GraphQLList fieldType = typeInfo.castType(GraphQLList.class);
    GraphQLFieldDefinition fieldDef = parameters.getTypeInfo().getFieldDefinition();
    InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, fieldDef, fieldTypeInfo(parameters, fieldDef), iterableValues);
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationContext<ExecutionResult> completeListCtx = instrumentation.beginFieldListComplete(instrumentationParams);
    CompletableFuture<List<ExecutionResult>> resultsFuture = Async.each(iterableValues, (item, index) -> {
        ExecutionPath indexedPath = parameters.getPath().segment(index);
        ExecutionTypeInfo wrappedTypeInfo = ExecutionTypeInfo.newTypeInfo().parentInfo(typeInfo).type(fieldType.getWrappedType()).path(indexedPath).fieldDefinition(fieldDef).build();
        NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, wrappedTypeInfo);
        ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.typeInfo(wrappedTypeInfo).nonNullFieldValidator(nonNullableFieldValidator).path(indexedPath).source(item));
        return completeValue(executionContext, newParameters);
    });
    CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
    completeListCtx.onDispatched(overallResult);
    resultsFuture.whenComplete((results, exception) -> {
        if (exception != null) {
            ExecutionResult executionResult = handleNonNullException(executionContext, overallResult, exception);
            completeListCtx.onCompleted(executionResult, exception);
            return;
        }
        List<Object> completedResults = new ArrayList<>();
        for (ExecutionResult completedValue : results) {
            completedResults.add(completedValue.getData());
        }
        ExecutionResultImpl executionResult = new ExecutionResultImpl(completedResults, null);
        overallResult.complete(executionResult);
    });
    overallResult.whenComplete(completeListCtx::onCompleted);
    return overallResult;
}
Also used : GraphQLList(graphql.schema.GraphQLList) InstrumentationFieldCompleteParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters) ArrayList(java.util.ArrayList) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionResultImpl(graphql.ExecutionResultImpl) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) GraphQLList(graphql.schema.GraphQLList)

Example 9 with ExecutionResultImpl

use of graphql.ExecutionResultImpl in project graphql-java by graphql-java.

the class ExecutionStrategy method completeValueForScalar.

/**
 * Called to turn an object into a scalar value according to the {@link GraphQLScalarType} by asking that scalar type to coerce the object
 * into a valid value
 *
 * @param executionContext contains the top level execution parameters
 * @param parameters       contains the parameters holding the fields to be executed and source object
 * @param scalarType       the type of the scalar
 * @param result           the result to be coerced
 *
 * @return an {@link ExecutionResult}
 */
protected CompletableFuture<ExecutionResult> completeValueForScalar(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLScalarType scalarType, Object result) {
    Object serialized;
    try {
        serialized = scalarType.getCoercing().serialize(result);
    } catch (CoercingSerializeException e) {
        serialized = handleCoercionProblem(executionContext, parameters, e);
    }
    // 6.6.1 http://facebook.github.io/graphql/#sec-Field-entries
    if (serialized instanceof Double && ((Double) serialized).isNaN()) {
        serialized = null;
    }
    serialized = parameters.getNonNullFieldValidator().validate(parameters.getPath(), serialized);
    return completedFuture(new ExecutionResultImpl(serialized, null));
}
Also used : ExecutionResultImpl(graphql.ExecutionResultImpl) CoercingSerializeException(graphql.schema.CoercingSerializeException) OptionalDouble(java.util.OptionalDouble)

Example 10 with ExecutionResultImpl

use of graphql.ExecutionResultImpl in project graphql-java by graphql-java.

the class ExecutionStrategy method handleNonNullException.

protected ExecutionResult handleNonNullException(ExecutionContext executionContext, CompletableFuture<ExecutionResult> result, Throwable e) {
    ExecutionResult executionResult = null;
    if (e instanceof NonNullableFieldWasNullException) {
        assertNonNullFieldPrecondition((NonNullableFieldWasNullException) e, result);
        if (!result.isDone()) {
            executionResult = new ExecutionResultImpl(null, executionContext.getErrors());
            result.complete(executionResult);
        }
    } else if (e instanceof CompletionException && e.getCause() instanceof NonNullableFieldWasNullException) {
        assertNonNullFieldPrecondition((NonNullableFieldWasNullException) e.getCause(), result);
        if (!result.isDone()) {
            executionResult = new ExecutionResultImpl(null, executionContext.getErrors());
            result.complete(executionResult);
        }
    } else {
        result.completeExceptionally(e);
    }
    return executionResult;
}
Also used : ExecutionResultImpl(graphql.ExecutionResultImpl) CompletionException(java.util.concurrent.CompletionException) ExecutionResult(graphql.ExecutionResult)

Aggregations

ExecutionResultImpl (graphql.ExecutionResultImpl)17 ExecutionResult (graphql.ExecutionResult)7 LinkedHashMap (java.util.LinkedHashMap)7 Field (graphql.language.Field)6 List (java.util.List)6 GraphQLError (graphql.GraphQLError)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 CoercingSerializeException (graphql.schema.CoercingSerializeException)3 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)3 ExecutionStrategyParameters (graphql.execution.ExecutionStrategyParameters)2 NonNullableFieldWasNullException (graphql.execution.NonNullableFieldWasNullException)2 Instrumentation (graphql.execution.instrumentation.Instrumentation)2 InstrumentationExecutionStrategyParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)2 OperationDefinition (graphql.language.OperationDefinition)2 GraphQLObjectType (graphql.schema.GraphQLObjectType)2 ArrayList (java.util.ArrayList)2 CompletionException (java.util.concurrent.CompletionException)2 ExceptionWhileDataFetching (graphql.ExceptionWhileDataFetching)1 GraphQLException (graphql.GraphQLException)1 InvalidSyntaxError (graphql.InvalidSyntaxError)1