Search in sources :

Example 11 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 result           the result to complete, raw result
 *
 * @return an {@link ExecutionResult}
 */
protected CompletableFuture<ExecutionResult> completeValueForList(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object result) {
    Iterable<Object> resultIterable = toIterable(executionContext, parameters, result);
    resultIterable = parameters.getNonNullFieldValidator().validate(parameters.getPath(), resultIterable);
    if (resultIterable == null) {
        return completedFuture(new ExecutionResultImpl(null, null));
    }
    return completeValueForList(executionContext, parameters, resultIterable);
}
Also used : ExecutionResultImpl(graphql.ExecutionResultImpl)

Example 12 with ExecutionResultImpl

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

the class ExecutorServiceExecutionStrategy method execute.

@Override
public CompletableFuture<ExecutionResult> execute(final ExecutionContext executionContext, final ExecutionStrategyParameters parameters) {
    if (executorService == null) {
        return new AsyncExecutionStrategy().execute(executionContext, parameters);
    }
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext, parameters);
    InstrumentationContext<ExecutionResult> executionStrategyCtx = instrumentation.beginExecutionStrategy(instrumentationParameters);
    Map<String, List<Field>> fields = parameters.getFields();
    Map<String, Future<CompletableFuture<ExecutionResult>>> futures = new LinkedHashMap<>();
    for (String fieldName : fields.keySet()) {
        final List<Field> currentField = fields.get(fieldName);
        ExecutionPath fieldPath = parameters.getPath().segment(fieldName);
        ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.field(currentField).path(fieldPath));
        Callable<CompletableFuture<ExecutionResult>> resolveField = () -> resolveField(executionContext, newParameters);
        futures.put(fieldName, executorService.submit(resolveField));
    }
    CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
    executionStrategyCtx.onDispatched(overallResult);
    try {
        Map<String, Object> results = new LinkedHashMap<>();
        for (String fieldName : futures.keySet()) {
            ExecutionResult executionResult;
            try {
                executionResult = futures.get(fieldName).get().join();
            } catch (CompletionException e) {
                if (e.getCause() instanceof NonNullableFieldWasNullException) {
                    assertNonNullFieldPrecondition((NonNullableFieldWasNullException) e.getCause());
                    results = null;
                    break;
                } else {
                    throw e;
                }
            }
            results.put(fieldName, executionResult != null ? executionResult.getData() : null);
        }
        ExecutionResultImpl executionResult = new ExecutionResultImpl(results, executionContext.getErrors());
        overallResult.complete(executionResult);
        overallResult.whenComplete(executionStrategyCtx::onCompleted);
        return overallResult;
    } catch (InterruptedException | ExecutionException e) {
        executionStrategyCtx.onCompleted(null, e);
        throw new GraphQLException(e);
    }
}
Also used : Instrumentation(graphql.execution.instrumentation.Instrumentation) LinkedHashMap(java.util.LinkedHashMap) Field(graphql.language.Field) CompletableFuture(java.util.concurrent.CompletableFuture) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters) List(java.util.List) GraphQLException(graphql.GraphQLException) ExecutionException(java.util.concurrent.ExecutionException) ExecutionResult(graphql.ExecutionResult) CompletionException(java.util.concurrent.CompletionException) ExecutionResultImpl(graphql.ExecutionResultImpl) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)

Example 13 with ExecutionResultImpl

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

the class DeferredCall method addErrorsEncountered.

private ExecutionResult addErrorsEncountered(ExecutionResult executionResult) {
    List<GraphQLError> errorsEncountered = errorSupport.getErrors();
    if (errorsEncountered.isEmpty()) {
        return executionResult;
    }
    ExecutionResultImpl sourceResult = (ExecutionResultImpl) executionResult;
    ExecutionResultImpl.Builder builder = ExecutionResultImpl.newExecutionResult().from(sourceResult);
    builder.addErrors(errorsEncountered);
    return builder.build();
}
Also used : ExecutionResultImpl(graphql.ExecutionResultImpl) GraphQLError(graphql.GraphQLError)

Example 14 with ExecutionResultImpl

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

the class DataLoaderDispatcherInstrumentation method instrumentExecutionResult.

@Override
public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) {
    if (!options.isIncludeStatistics()) {
        return CompletableFuture.completedFuture(executionResult);
    }
    Map<Object, Object> currentExt = executionResult.getExtensions();
    Map<Object, Object> statsMap = new LinkedHashMap<>();
    statsMap.putAll(currentExt == null ? Collections.emptyMap() : currentExt);
    Map<Object, Object> dataLoaderStats = buildStatsMap();
    statsMap.put("dataloader", dataLoaderStats);
    log.debug("Data loader stats : {}", dataLoaderStats);
    return CompletableFuture.completedFuture(new ExecutionResultImpl(executionResult.getData(), executionResult.getErrors(), statsMap));
}
Also used : ExecutionResultImpl(graphql.ExecutionResultImpl) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with ExecutionResultImpl

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

the class BreadthFirstExecutionTestStrategy method execute.

@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
    Map<String, List<Field>> fields = parameters.getFields();
    Map<String, Object> fetchedValues = new LinkedHashMap<>();
    // first fetch every value
    for (String fieldName : fields.keySet()) {
        Object fetchedValue = fetchField(executionContext, parameters, fields, fieldName);
        fetchedValues.put(fieldName, fetchedValue);
    }
    // then for every fetched value, complete it
    Map<String, Object> results = new LinkedHashMap<>();
    for (String fieldName : fetchedValues.keySet()) {
        List<Field> currentField = fields.get(fieldName);
        Object fetchedValue = fetchedValues.get(fieldName);
        ExecutionPath fieldPath = parameters.getPath().segment(fieldName);
        ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.field(currentField).path(fieldPath));
        try {
            completeValue(executionContext, results, fieldName, fetchedValue, newParameters);
        } catch (NonNullableFieldWasNullException e) {
            assertNonNullFieldPrecondition(e);
            results = null;
            break;
        }
    }
    return CompletableFuture.completedFuture(new ExecutionResultImpl(results, executionContext.getErrors()));
}
Also used : Field(graphql.language.Field) ExecutionResultImpl(graphql.ExecutionResultImpl) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap)

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