Search in sources :

Example 1 with InstrumentationExecutionStrategyParameters

use of graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters 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 2 with InstrumentationExecutionStrategyParameters

use of graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters in project graphql-java by graphql-java.

the class AsyncSerialExecutionStrategy method execute.

@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext, parameters);
    InstrumentationContext<ExecutionResult> executionStrategyCtx = instrumentation.beginExecutionStrategy(instrumentationParameters);
    Map<String, List<Field>> fields = parameters.getFields();
    List<String> fieldNames = new ArrayList<>(fields.keySet());
    CompletableFuture<List<ExecutionResult>> resultsFuture = Async.eachSequentially(fieldNames, (fieldName, index, prevResults) -> {
        List<Field> currentField = fields.get(fieldName);
        ExecutionPath fieldPath = parameters.getPath().segment(fieldName);
        ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.field(currentField).path(fieldPath));
        return resolveField(executionContext, newParameters);
    });
    CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
    executionStrategyCtx.onDispatched(overallResult);
    resultsFuture.whenComplete(handleResults(executionContext, fieldNames, overallResult));
    overallResult.whenComplete(executionStrategyCtx::onCompleted);
    return overallResult;
}
Also used : ArrayList(java.util.ArrayList) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) Field(graphql.language.Field) CompletableFuture(java.util.concurrent.CompletableFuture) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters) List(java.util.List) ArrayList(java.util.ArrayList) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)

Example 3 with InstrumentationExecutionStrategyParameters

use of graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters 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 4 with InstrumentationExecutionStrategyParameters

use of graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters in project graphql-java by graphql-java.

the class AsyncExecutionStrategy method execute.

@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext, parameters);
    InstrumentationContext<ExecutionResult> executionStrategyCtx = instrumentation.beginExecutionStrategy(instrumentationParameters);
    Map<String, List<Field>> fields = parameters.getFields();
    List<String> fieldNames = new ArrayList<>(fields.keySet());
    List<CompletableFuture<ExecutionResult>> futures = new ArrayList<>();
    for (String fieldName : fieldNames) {
        List<Field> currentField = fields.get(fieldName);
        ExecutionPath fieldPath = parameters.getPath().segment(fieldName);
        ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.field(currentField).path(fieldPath));
        if (isDeferred(executionContext, newParameters, currentField)) {
            continue;
        }
        CompletableFuture<ExecutionResult> future = resolveField(executionContext, newParameters);
        futures.add(future);
    }
    CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
    executionStrategyCtx.onDispatched(overallResult);
    Async.each(futures).whenComplete(handleResults(executionContext, fieldNames, overallResult));
    overallResult.whenComplete(executionStrategyCtx::onCompleted);
    return overallResult;
}
Also used : ArrayList(java.util.ArrayList) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) Field(graphql.language.Field) CompletableFuture(java.util.concurrent.CompletableFuture) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters) List(java.util.List) ArrayList(java.util.ArrayList) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)

Aggregations

ExecutionResult (graphql.ExecutionResult)4 InstrumentationExecutionStrategyParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Instrumentation (graphql.execution.instrumentation.Instrumentation)3 Field (graphql.language.Field)3 List (java.util.List)3 ArrayList (java.util.ArrayList)2 ExecutionResultImpl (graphql.ExecutionResultImpl)1 GraphQLException (graphql.GraphQLException)1 GraphQLObjectType (graphql.schema.GraphQLObjectType)1 ArrayDeque (java.util.ArrayDeque)1 LinkedHashMap (java.util.LinkedHashMap)1 CompletionException (java.util.concurrent.CompletionException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1