Search in sources :

Example 1 with SmallRyeContext

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;
}
Also used : SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext)

Example 2 with 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);
        }
    });
}
Also used : SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) ThreadContext(org.eclipse.microprofile.context.ThreadContext) ExecutionResult(graphql.ExecutionResult)

Example 3 with SmallRyeContext

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);
    }
}
Also used : SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) DataLoaderRegistry(org.dataloader.DataLoaderRegistry) GraphQL(graphql.GraphQL) Builder(graphql.ExecutionInput.Builder) ExecutionInput(graphql.ExecutionInput) HashMap(java.util.HashMap) Map(java.util.Map) ExecutionId(graphql.execution.ExecutionId)

Example 4 with SmallRyeContext

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);
}
Also used : SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) DataFetcherResult(graphql.execution.DataFetcherResult) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException)

Example 5 with SmallRyeContext

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;
}
Also used : SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext)

Aggregations

SmallRyeContext (io.smallrye.graphql.execution.context.SmallRyeContext)13 AbstractDataFetcherException (io.smallrye.graphql.transformation.AbstractDataFetcherException)5 List (java.util.List)5 ThreadContext (org.eclipse.microprofile.context.ThreadContext)5 GraphQLException (org.eclipse.microprofile.graphql.GraphQLException)5 DataFetcherResult (graphql.execution.DataFetcherResult)4 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)4 SmallRyeGraphQLServerMessages (io.smallrye.graphql.SmallRyeGraphQLServerMessages)3 Operation (io.smallrye.graphql.schema.model.Operation)3 CompletionStage (java.util.concurrent.CompletionStage)3 BatchLoaderEnvironment (org.dataloader.BatchLoaderEnvironment)3 Multi (io.smallrye.mutiny.Multi)2 Infrastructure (io.smallrye.mutiny.infrastructure.Infrastructure)2 Function (java.util.function.Function)2 ExecutionInput (graphql.ExecutionInput)1 Builder (graphql.ExecutionInput.Builder)1 ExecutionResult (graphql.ExecutionResult)1 GraphQL (graphql.GraphQL)1 ExecutionId (graphql.execution.ExecutionId)1 Uni (io.smallrye.mutiny.Uni)1