Search in sources :

Example 11 with ExecutionResult

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

the class BatchedExecutionStrategy method resolveField.

private CompletableFuture<List<ExecutionNode>> resolveField(ExecutionContext executionContext, ExecutionStrategyParameters parameters, String fieldName, ExecutionNode node) {
    GraphQLObjectType parentType = node.getType();
    List<Field> fields = node.getFields().get(fieldName);
    GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, fields.get(0));
    Instrumentation instrumentation = executionContext.getInstrumentation();
    ExecutionTypeInfo typeInfo = parameters.getTypeInfo();
    InstrumentationContext<ExecutionResult> fieldCtx = instrumentation.beginField(new InstrumentationFieldParameters(executionContext, fieldDef, typeInfo));
    CompletableFuture<FetchedValues> fetchedData = fetchData(executionContext, parameters, fieldName, node, fieldDef);
    CompletableFuture<List<ExecutionNode>> result = fetchedData.thenApply((fetchedValues) -> {
        GraphqlFieldVisibility fieldVisibility = executionContext.getGraphQLSchema().getFieldVisibility();
        Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldVisibility, fieldDef.getArguments(), fields.get(0).getArguments(), executionContext.getVariables());
        return completeValues(executionContext, fetchedValues, typeInfo, fieldName, fields, argumentValues);
    });
    fieldCtx.onDispatched(null);
    result.whenComplete((nodes, throwable) -> fieldCtx.onCompleted(null, throwable));
    return result;
}
Also used : GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) InstrumentationFieldParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldParameters) Field(graphql.language.Field) ExecutionTypeInfo(graphql.execution.ExecutionTypeInfo) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) GraphQLObjectType(graphql.schema.GraphQLObjectType) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) GraphQLList(graphql.schema.GraphQLList)

Example 12 with ExecutionResult

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

the class BatchedExecutionStrategy method execute.

@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
    InstrumentationContext<ExecutionResult> executionStrategyCtx = executionContext.getInstrumentation().beginExecutionStrategy(new InstrumentationExecutionStrategyParameters(executionContext, parameters));
    GraphQLObjectType type = parameters.getTypeInfo().castType(GraphQLObjectType.class);
    ExecutionNode root = new ExecutionNode(type, parameters.getTypeInfo(), parameters.getFields(), singletonList(MapOrList.createMap(new LinkedHashMap<>())), Collections.singletonList(parameters.getSource()));
    Queue<ExecutionNode> nodes = new ArrayDeque<>();
    CompletableFuture<ExecutionResult> result = new CompletableFuture<>();
    executeImpl(executionContext, parameters, root, root, nodes, root.getFields().keySet().iterator(), result);
    executionStrategyCtx.onDispatched(result);
    result.whenComplete(executionStrategyCtx::onCompleted);
    return result;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) GraphQLObjectType(graphql.schema.GraphQLObjectType) ExecutionResult(graphql.ExecutionResult) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters) ArrayDeque(java.util.ArrayDeque)

Example 13 with ExecutionResult

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

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

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

the class ExecutionStrategy method completeField.

/**
 * Called to complete a field based on the type of the field.
 * <p>
 * If the field is a scalar type, then it will be coerced  and returned.  However if the field type is an complex object type, then
 * the execution strategy will be called recursively again to execute the fields of that type before returning.
 * <p>
 * Graphql fragments mean that for any give logical field can have one or more {@link Field} values associated with it
 * in the query, hence the fieldList.  However the first entry is representative of the field for most purposes.
 *
 * @param executionContext contains the top level execution parameters
 * @param parameters       contains the parameters holding the fields to be executed and source object
 * @param fetchedValue     the fetched raw value
 *
 * @return an {@link ExecutionResult}
 *
 * @throws NonNullableFieldWasNullException if a non null field resolves to a null value
 */
protected CompletableFuture<ExecutionResult> completeField(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object fetchedValue) {
    Field field = parameters.getField().get(0);
    GraphQLObjectType parentType = parameters.getTypeInfo().castType(GraphQLObjectType.class);
    GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, field);
    ExecutionTypeInfo fieldTypeInfo = fieldTypeInfo(parameters, fieldDef);
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, fieldDef, fieldTypeInfo, fetchedValue);
    InstrumentationContext<ExecutionResult> ctxCompleteField = instrumentation.beginFieldComplete(instrumentationParams);
    GraphqlFieldVisibility fieldVisibility = executionContext.getGraphQLSchema().getFieldVisibility();
    Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldVisibility, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables());
    NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, fieldTypeInfo);
    ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.typeInfo(fieldTypeInfo).arguments(argumentValues).source(fetchedValue).nonNullFieldValidator(nonNullableFieldValidator));
    log.debug("'{}' completing field '{}'...", executionContext.getExecutionId(), fieldTypeInfo.getPath());
    CompletableFuture<ExecutionResult> cf = completeValue(executionContext, newParameters);
    ctxCompleteField.onDispatched(cf);
    cf.whenComplete(ctxCompleteField::onCompleted);
    return cf;
}
Also used : InstrumentationFieldCompleteParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) Field(graphql.language.Field) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) GraphQLObjectType(graphql.schema.GraphQLObjectType)

Aggregations

ExecutionResult (graphql.ExecutionResult)42 GraphQL (graphql.GraphQL)17 List (java.util.List)10 GraphQLSchema (graphql.schema.GraphQLSchema)9 ExecutionInput (graphql.ExecutionInput)8 Instrumentation (graphql.execution.instrumentation.Instrumentation)8 Field (graphql.language.Field)8 ExecutionResultImpl (graphql.ExecutionResultImpl)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 GraphQLError (graphql.GraphQLError)6 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)6 LinkedHashMap (java.util.LinkedHashMap)6 GraphQLObjectType (graphql.schema.GraphQLObjectType)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 InstrumentationExecutionStrategyParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)4 HashMap (java.util.HashMap)4 Collectors.toList (java.util.stream.Collectors.toList)3 InstrumentationFieldCompleteParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters)2 GraphQLList (graphql.schema.GraphQLList)2