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);
}
});
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations