use of io.smallrye.graphql.execution.context.SmallRyeContext in project smallrye-graphql by smallrye.
the class ContextHelper method updateSmallRyeContextWithField.
public SmallRyeContext updateSmallRyeContextWithField(final DataFetchingEnvironment dfe, final Field field) {
SmallRyeContext smallRyeContext = getSmallRyeContext(dfe);
smallRyeContext = smallRyeContext.withDataFromFetcher(dfe, field);
setSmallRyeContext(dfe, smallRyeContext);
return smallRyeContext;
}
use of io.smallrye.graphql.execution.context.SmallRyeContext 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 io.smallrye.graphql.execution.context.SmallRyeContext in project smallrye-graphql by smallrye.
the class ExecutionService method execute.
public void execute(JsonObject jsonInput, Map<String, Object> context, ExecutionResponseWriter writer, boolean async) {
SmallRyeContext smallRyeContext = new SmallRyeContext(jsonInput);
// ExecutionId
ExecutionId finalExecutionId = ExecutionId.from(executionIdPrefix + executionId.getAndIncrement());
try {
String query = smallRyeContext.getQuery();
Optional<Map<String, Object>> variables = smallRyeContext.getVariables();
if (query == null || query.isEmpty()) {
throw new RuntimeException("Query can not be null");
}
if (payloadOption.equals(LogPayloadOption.queryOnly)) {
log.payloadIn(query);
} else if (payloadOption.equals(LogPayloadOption.queryAndVariables)) {
log.payloadIn(query);
log.payloadIn(variables.toString());
}
GraphQL g = getGraphQL();
if (g != null) {
// Query
Builder executionBuilder = ExecutionInput.newExecutionInput().query(query).executionId(finalExecutionId);
// Variables
smallRyeContext.getVariables().ifPresent(executionBuilder::variables);
// Operation name
smallRyeContext.getOperationName().ifPresent(executionBuilder::operationName);
// DataLoaders
if (batchOperations != null && !batchOperations.isEmpty()) {
DataLoaderRegistry dataLoaderRegistry = getDataLoaderRegistry(batchOperations);
executionBuilder.dataLoaderRegistry(dataLoaderRegistry);
}
ExecutionInput executionInput = executionBuilder.build();
// Update context with execution data
smallRyeContext = smallRyeContext.withDataFromExecution(executionInput, queryCache);
// Context
context.put(ContextHelper.CONTEXT, smallRyeContext);
executionInput.getGraphQLContext().putAll(context);
// Notify before
eventEmitter.fireBeforeExecute(smallRyeContext);
// Execute
if (async) {
writeAsync(g, executionInput, context, writer);
} else {
writeSync(g, executionInput, context, writer);
}
} else {
log.noGraphQLMethodsFound();
}
} catch (Throwable t) {
eventEmitter.fireOnExecuteError(finalExecutionId.toString(), t);
writer.fail(t);
}
}
use of io.smallrye.graphql.execution.context.SmallRyeContext in project smallrye-graphql by smallrye.
the class AbstractDataFetcher method get.
@Override
public T get(final DataFetchingEnvironment dfe) throws Exception {
// update the context
SmallRyeContext context = initSmallRyeContext(dfe);
final DataFetcherResult.Builder<Object> resultBuilder = DataFetcherResult.newResult().localContext(dfe.getGraphQlContext());
try {
Object[] transformedArguments = argumentHelper.getArguments(dfe);
return invokeAndTransform(dfe, resultBuilder, transformedArguments);
} catch (AbstractDataFetcherException abstractDataFetcherException) {
// Arguments or result couldn't be transformed
abstractDataFetcherException.appendDataFetcherResult(resultBuilder, dfe);
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), abstractDataFetcherException);
} catch (GraphQLException graphQLException) {
errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), graphQLException);
} catch (Throwable ex) {
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), ex);
throw ex;
} finally {
eventEmitter.fireAfterDataFetch(context);
}
return invokeFailure(resultBuilder);
}
use of io.smallrye.graphql.execution.context.SmallRyeContext in project smallrye-graphql by smallrye.
the class AbstractDataFetcher method initSmallRyeContext.
private SmallRyeContext initSmallRyeContext(final DataFetchingEnvironment dfe) {
// update the context
SmallRyeContext context = contextHelper.updateSmallRyeContextWithField(dfe, operation);
eventEmitter.fireBeforeDataFetch(context);
return context;
}
Aggregations