Search in sources :

Example 1 with ExecutionStrategyInstrumentationContext

use of graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext in project graphql-java by graphql-java.

the class SubscriptionExecutionStrategy method execute.

@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext, parameters);
    ExecutionStrategyInstrumentationContext executionStrategyCtx = instrumentation.beginExecutionStrategy(instrumentationParameters);
    CompletableFuture<Publisher<Object>> sourceEventStream = createSourceEventStream(executionContext, parameters);
    // 
    // when the upstream source event stream completes, subscribe to it and wire in our adapter
    CompletableFuture<ExecutionResult> overallResult = sourceEventStream.thenApply((publisher) -> {
        if (publisher == null) {
            return new ExecutionResultImpl(null, executionContext.getErrors());
        }
        Function<Object, CompletionStage<ExecutionResult>> mapperFunction = eventPayload -> executeSubscriptionEvent(executionContext, parameters, eventPayload);
        SubscriptionPublisher mapSourceToResponse = new SubscriptionPublisher(publisher, mapperFunction);
        return new ExecutionResultImpl(mapSourceToResponse, executionContext.getErrors());
    });
    // dispatched the subscription query
    executionStrategyCtx.onDispatched(overallResult);
    overallResult.whenComplete(executionStrategyCtx::onCompleted);
    return overallResult;
}
Also used : GraphQLObjectType(graphql.schema.GraphQLObjectType) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters) Publisher(org.reactivestreams.Publisher) InstrumentationExecutionParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters) SubscriptionPublisher(graphql.execution.reactive.SubscriptionPublisher) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) CompletableFuture(java.util.concurrent.CompletableFuture) Field(graphql.language.Field) Function(java.util.function.Function) ExecutionResult(graphql.ExecutionResult) InstrumentationContext(graphql.execution.instrumentation.InstrumentationContext) CompletionStage(java.util.concurrent.CompletionStage) Instrumentation(graphql.execution.instrumentation.Instrumentation) Assert.assertTrue(graphql.Assert.assertTrue) PublicApi(graphql.PublicApi) Collections.singletonMap(java.util.Collections.singletonMap) InstrumentationFieldParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldParameters) ExecutionStrategyInstrumentationContext(graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext) ExecutionResultImpl(graphql.ExecutionResultImpl) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) Publisher(org.reactivestreams.Publisher) SubscriptionPublisher(graphql.execution.reactive.SubscriptionPublisher) ExecutionStrategyInstrumentationContext(graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext) ExecutionResultImpl(graphql.ExecutionResultImpl) SubscriptionPublisher(graphql.execution.reactive.SubscriptionPublisher) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters) CompletionStage(java.util.concurrent.CompletionStage)

Example 2 with ExecutionStrategyInstrumentationContext

use of graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext in project graphql-java by graphql-java.

the class FieldLevelTrackingApproach method beginExecutionStrategy.

ExecutionStrategyInstrumentationContext beginExecutionStrategy(InstrumentationExecutionStrategyParameters parameters) {
    CallStack callStack = parameters.getInstrumentationState();
    ResultPath path = parameters.getExecutionStrategyParameters().getPath();
    int parentLevel = path.getLevel();
    int curLevel = parentLevel + 1;
    int fieldCount = parameters.getExecutionStrategyParameters().getFields().size();
    synchronized (callStack) {
        callStack.increaseExpectedFetchCount(curLevel, fieldCount);
        callStack.increaseHappenedStrategyCalls(curLevel);
    }
    return new ExecutionStrategyInstrumentationContext() {

        @Override
        public void onDispatched(CompletableFuture<ExecutionResult> result) {
        }

        @Override
        public void onCompleted(ExecutionResult result, Throwable t) {
        }

        @Override
        public void onFieldValuesInfo(List<FieldValueInfo> fieldValueInfoList) {
            boolean dispatchNeeded;
            synchronized (callStack) {
                dispatchNeeded = handleOnFieldValuesInfo(fieldValueInfoList, callStack, curLevel);
            }
            if (dispatchNeeded) {
                dispatch();
            }
        }

        @Override
        public void onFieldValuesException() {
            synchronized (callStack) {
                callStack.increaseHappenedOnFieldValueCalls(curLevel);
            }
        }
    };
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionStrategyInstrumentationContext(graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext) ResultPath(graphql.execution.ResultPath) ExecutionResult(graphql.ExecutionResult) List(java.util.List)

Example 3 with ExecutionStrategyInstrumentationContext

use of graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext in project graphql-java by graphql-java.

the class AsyncExecutionStrategy method execute.

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext, parameters);
    ExecutionStrategyInstrumentationContext executionStrategyCtx = instrumentation.beginExecutionStrategy(instrumentationParameters);
    MergedSelectionSet fields = parameters.getFields();
    Set<String> fieldNames = fields.keySet();
    List<CompletableFuture<FieldValueInfo>> futures = new ArrayList<>(fieldNames.size());
    List<String> resolvedFields = new ArrayList<>(fieldNames.size());
    for (String fieldName : fieldNames) {
        MergedField currentField = fields.getSubField(fieldName);
        ResultPath fieldPath = parameters.getPath().segment(mkNameForPath(currentField));
        ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.field(currentField).path(fieldPath).parent(parameters));
        resolvedFields.add(fieldName);
        CompletableFuture<FieldValueInfo> future = resolveFieldWithInfo(executionContext, newParameters);
        futures.add(future);
    }
    CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
    executionStrategyCtx.onDispatched(overallResult);
    Async.each(futures).whenComplete((completeValueInfos, throwable) -> {
        BiConsumer<List<ExecutionResult>, Throwable> handleResultsConsumer = handleResults(executionContext, resolvedFields, overallResult);
        if (throwable != null) {
            handleResultsConsumer.accept(null, throwable.getCause());
            return;
        }
        List<CompletableFuture<ExecutionResult>> executionResultFuture = map(completeValueInfos, FieldValueInfo::getFieldValue);
        executionStrategyCtx.onFieldValuesInfo(completeValueInfos);
        Async.each(executionResultFuture).whenComplete(handleResultsConsumer);
    }).exceptionally((ex) -> {
        // if there are any issues with combining/handling the field results,
        // complete the future at all costs and bubble up any thrown exception so
        // the execution does not hang.
        executionStrategyCtx.onFieldValuesException();
        overallResult.completeExceptionally(ex);
        return null;
    });
    overallResult.whenComplete(executionStrategyCtx::onCompleted);
    return overallResult;
}
Also used : ExecutionResult(graphql.ExecutionResult) List(java.util.List) ImmutableKit.map(graphql.collect.ImmutableKit.map) Instrumentation(graphql.execution.instrumentation.Instrumentation) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters) PublicApi(graphql.PublicApi) BiConsumer(java.util.function.BiConsumer) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionStrategyInstrumentationContext(graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionStrategyInstrumentationContext(graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters) List(java.util.List) ArrayList(java.util.ArrayList) InstrumentationExecutionStrategyParameters(graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters) BiConsumer(java.util.function.BiConsumer)

Aggregations

ExecutionResult (graphql.ExecutionResult)3 ExecutionStrategyInstrumentationContext (graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 PublicApi (graphql.PublicApi)2 Instrumentation (graphql.execution.instrumentation.Instrumentation)2 InstrumentationExecutionStrategyParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)2 List (java.util.List)2 Assert.assertTrue (graphql.Assert.assertTrue)1 ExecutionResultImpl (graphql.ExecutionResultImpl)1 ImmutableKit.map (graphql.collect.ImmutableKit.map)1 ResultPath (graphql.execution.ResultPath)1 InstrumentationContext (graphql.execution.instrumentation.InstrumentationContext)1 InstrumentationExecutionParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters)1 InstrumentationFieldParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldParameters)1 SubscriptionPublisher (graphql.execution.reactive.SubscriptionPublisher)1 Field (graphql.language.Field)1 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)1 GraphQLObjectType (graphql.schema.GraphQLObjectType)1 ArrayList (java.util.ArrayList)1 Collections.singletonMap (java.util.Collections.singletonMap)1