Search in sources :

Example 1 with ThreadContext

use of org.eclipse.microprofile.context.ThreadContext in project smallrye-graphql by smallrye.

the class ExecutionService method writeAsync.

private void writeAsync(GraphQL graphQL, ExecutionInput executionInput, Map<String, Object> context, ExecutionResponseWriter writer) {
    // Execute
    ThreadContext threadContext = ThreadContext.builder().build();
    CompletionStage<ExecutionResult> executionResult = threadContext.withContextCapture(graphQL.executeAsync(executionInput));
    executionResult.whenComplete((t, u) -> {
        executionInput.getGraphQLContext().putAll(context);
        SmallRyeContext smallryeContext = (SmallRyeContext) context.get(ContextHelper.CONTEXT);
        SmallRyeContext.setContext(smallryeContext);
        // Notify after
        eventEmitter.fireAfterExecute(smallryeContext);
        ExecutionResponse executionResponse = new ExecutionResponse(t);
        if (!payloadOption.equals(LogPayloadOption.off)) {
            log.payloadOut(executionResponse.toString());
        }
        writer.write(executionResponse);
        if (u != null) {
            writer.fail(u);
        }
    });
}
Also used : SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) ThreadContext(org.eclipse.microprofile.context.ThreadContext) ExecutionResult(graphql.ExecutionResult)

Example 2 with ThreadContext

use of org.eclipse.microprofile.context.ThreadContext in project smallrye-graphql by smallrye.

the class BatchDataFetcher method get.

@Override
public T get(final DataFetchingEnvironment dfe) throws Exception {
    SmallRyeContext smallryeContext = contextHelper.updateSmallRyeContextWithField(dfe, operation);
    eventEmitter.fireBeforeDataFetch(smallryeContext);
    Object[] transformedArguments = argumentHelper.getArguments(dfe, true);
    Object source = dfe.getSource();
    DataLoader<Object, Object> dataLoader = dfe.getDataLoader(batchLoaderName);
    batchLoaderHelper.setDataFetchingEnvironment(dataLoader, dfe);
    try {
        SmallRyeContext.setContext(smallryeContext);
        ThreadContext threadContext = ThreadContext.builder().build();
        return (T) threadContext.withContextCapture(dataLoader.load(source, transformedArguments));
    } finally {
        SmallRyeContext.remove();
    }
}
Also used : SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) ThreadContext(org.eclipse.microprofile.context.ThreadContext)

Example 3 with ThreadContext

use of org.eclipse.microprofile.context.ThreadContext in project smallrye-graphql by smallrye.

the class CompletionStageDataFetcher method load.

@Override
public CompletionStage<List<T>> load(List<K> keys, BatchLoaderEnvironment ble) {
    Object[] arguments = batchLoaderHelper.getArguments(keys, ble);
    final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    final SmallRyeContext smallRyeContext = contextHelper.getSmallRyeContext(ble);
    final ThreadContext threadContext = ThreadContext.builder().build();
    try {
        SmallRyeContext.setContext(smallRyeContext);
        return threadContext.withContextCapture((CompletableFuture<List<T>>) operationInvoker.invokePrivileged(tccl, arguments));
    } finally {
        SmallRyeContext.remove();
    }
}
Also used : SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) ThreadContext(org.eclipse.microprofile.context.ThreadContext) List(java.util.List)

Example 4 with ThreadContext

use of org.eclipse.microprofile.context.ThreadContext in project smallrye-graphql by smallrye.

the class CompletionStageDataFetcher method invokeAndTransform.

@Override
protected <T> T invokeAndTransform(DataFetchingEnvironment dfe, DataFetcherResult.Builder<Object> resultBuilder, Object[] transformedArguments) throws AbstractDataFetcherException, Exception {
    SmallRyeContext context = contextHelper.getSmallRyeContext(dfe);
    ThreadContext threadContext = ThreadContext.builder().build();
    SmallRyeContext.setContext(context);
    try {
        CompletionStage<Object> futureResultFromMethodCall = threadContext.withContextCapture(operationInvoker.invoke(transformedArguments));
        return (T) futureResultFromMethodCall.handle((result, throwable) -> {
            if (throwable != null) {
                eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
                if (throwable instanceof GraphQLException) {
                    GraphQLException graphQLException = (GraphQLException) throwable;
                    errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
                } else if (throwable instanceof Exception) {
                    throw SmallRyeGraphQLServerMessages.msg.dataFetcherException(operation, throwable);
                } else if (throwable instanceof Error) {
                    throw ((Error) throwable);
                }
            } else {
                try {
                    resultBuilder.data(fieldHelper.transformOrAdaptResponse(result, dfe));
                } catch (AbstractDataFetcherException te) {
                    te.appendDataFetcherResult(resultBuilder, dfe);
                }
            }
            return resultBuilder.build();
        });
    } finally {
        SmallRyeContext.remove();
    }
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) BatchLoaderEnvironment(org.dataloader.BatchLoaderEnvironment) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) SmallRyeGraphQLServerMessages(io.smallrye.graphql.SmallRyeGraphQLServerMessages) Operation(io.smallrye.graphql.schema.model.Operation) CompletableFuture(java.util.concurrent.CompletableFuture) DataFetcherResult(graphql.execution.DataFetcherResult) ThreadContext(org.eclipse.microprofile.context.ThreadContext) SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) ThreadContext(org.eclipse.microprofile.context.ThreadContext) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException)

Example 5 with ThreadContext

use of org.eclipse.microprofile.context.ThreadContext in project smallrye-graphql by smallrye.

the class DefaultDataFetcher method load.

@Override
public CompletionStage<List<T>> load(List<K> keys, BatchLoaderEnvironment ble) {
    final Object[] arguments = batchLoaderHelper.getArguments(keys, ble);
    final DataFetchingEnvironment dfe = batchLoaderHelper.getDataFetchingEnvironment(ble);
    final SmallRyeContext smallRyeContext = contextHelper.getSmallRyeContext(dfe);
    final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    ThreadContext threadContext = ThreadContext.builder().build();
    try {
        SmallRyeContext.setContext(smallRyeContext);
        CompletableFuture<List<T>> reflectionSupplier = CompletableFuture.supplyAsync(() -> {
            try {
                return (List<T>) operationInvoker.invokePrivileged(tccl, arguments);
            } catch (Exception e) {
                if (e instanceof RuntimeException && e.getCause() != null && !(e.getCause() instanceof RuntimeException)) {
                    throw msg.dataFetcherException(operation, e.getCause());
                } else if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                } else {
                    throw msg.dataFetcherException(operation, e);
                }
            }
        }, threadContext.currentContextExecutor());
        return threadContext.withContextCapture(reflectionSupplier);
    } finally {
        SmallRyeContext.remove();
    }
}
Also used : SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) ThreadContext(org.eclipse.microprofile.context.ThreadContext) List(java.util.List) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment)

Aggregations

SmallRyeContext (io.smallrye.graphql.execution.context.SmallRyeContext)5 ThreadContext (org.eclipse.microprofile.context.ThreadContext)5 List (java.util.List)3 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)2 ExecutionResult (graphql.ExecutionResult)1 DataFetcherResult (graphql.execution.DataFetcherResult)1 SmallRyeGraphQLServerMessages (io.smallrye.graphql.SmallRyeGraphQLServerMessages)1 Operation (io.smallrye.graphql.schema.model.Operation)1 AbstractDataFetcherException (io.smallrye.graphql.transformation.AbstractDataFetcherException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 BatchLoaderEnvironment (org.dataloader.BatchLoaderEnvironment)1 GraphQLException (org.eclipse.microprofile.graphql.GraphQLException)1