Search in sources :

Example 1 with GraphQLException

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

the class ReflectionInvoker method invoke.

public <T> T invoke(Object... arguments) throws Exception {
    if (this.injectContextAt > -1) {
        arguments = injectContext(arguments);
    }
    try {
        ManagedInstance<?> operationInstance = lookupService.getInstance(operationClass);
        Object operationInstance1 = operationInstance.get();
        eventEmitter.fireBeforeMethodInvoke(new InvokeInfo(operationInstance1, method, arguments));
        T result = (T) method.invoke(operationInstance1, arguments);
        if (result instanceof Uni) {
            return (T) ((Uni) result).onTermination().invoke(() -> {
                operationInstance.destroyIfNecessary();
            });
        } else if (result instanceof Multi) {
            return (T) ((Multi) result).onTermination().invoke(() -> {
                operationInstance.destroyIfNecessary();
            });
        } else {
            operationInstance.destroyIfNecessary();
            return result;
        }
    } catch (InvocationTargetException ex) {
        // Invoked method has thrown something, unwrap
        Throwable throwable = ex.getCause();
        if (throwable instanceof Error) {
            throw (Error) throwable;
        } else if (throwable instanceof GraphQLException) {
            throw (GraphQLException) throwable;
        } else if (throwable instanceof Exception) {
            throw (Exception) throwable;
        } else {
            throw msg.generalDataFetcherException(operationClass.getName() + ": " + method.getName(), throwable);
        }
    }
}
Also used : Uni(io.smallrye.mutiny.Uni) InvokeInfo(io.smallrye.graphql.execution.event.InvokeInfo) Multi(io.smallrye.mutiny.Multi) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PrivilegedActionException(java.security.PrivilegedActionException) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with GraphQLException

use of org.eclipse.microprofile.graphql.GraphQLException 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 3 with GraphQLException

use of org.eclipse.microprofile.graphql.GraphQLException 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 4 with GraphQLException

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

the class MultiDataFetcher method invokeAndTransform.

@Override
@SuppressWarnings("unchecked")
protected <O> O invokeAndTransform(DataFetchingEnvironment dfe, DataFetcherResult.Builder<Object> resultBuilder, Object[] transformedArguments) throws Exception {
    SmallRyeContext context = contextHelper.getSmallRyeContext(dfe);
    try {
        SmallRyeContext.setContext(context);
        Multi<?> multi = operationInvoker.invoke(transformedArguments);
        return (O) multi.onItem().transform((t) -> {
            try {
                Object resultFromTransform = fieldHelper.transformOrAdaptResponse(t, dfe);
                resultBuilder.data(resultFromTransform);
                return (O) resultBuilder.build();
            } catch (AbstractDataFetcherException abstractDataFetcherException) {
                // Arguments or result couldn't be transformed
                abstractDataFetcherException.appendDataFetcherResult(resultBuilder, dfe);
                eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), abstractDataFetcherException);
                return (O) resultBuilder.build();
            }
        }).onFailure().recoverWithItem(new Function<Throwable, O>() {

            public O apply(Throwable throwable) {
                eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
                if (throwable instanceof GraphQLException) {
                    GraphQLException graphQLException = (GraphQLException) throwable;
                    errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
                } else if (throwable instanceof Exception) {
                    DataFetcherException dataFetcherException = SmallRyeGraphQLServerMessages.msg.dataFetcherException(operation, throwable);
                    errorResultHelper.appendException(resultBuilder, dfe, dataFetcherException);
                } else if (throwable instanceof Error) {
                    errorResultHelper.appendException(resultBuilder, dfe, throwable);
                }
                return (O) resultBuilder.build();
            }
        });
    } finally {
        SmallRyeContext.remove();
    }
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) SmallRyeGraphQLServerMessages(io.smallrye.graphql.SmallRyeGraphQLServerMessages) SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) Function(java.util.function.Function) Multi(io.smallrye.mutiny.Multi) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) BatchLoaderEnvironment(org.dataloader.BatchLoaderEnvironment) Operation(io.smallrye.graphql.schema.model.Operation) DataFetcherResult(graphql.execution.DataFetcherResult) Function(java.util.function.Function) SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) 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 GraphQLException

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

the class PublisherDataFetcher method invokeAndTransform.

@Override
@SuppressWarnings("unchecked")
protected <O> O invokeAndTransform(DataFetchingEnvironment dfe, DataFetcherResult.Builder<Object> resultBuilder, Object[] transformedArguments) throws Exception {
    SmallRyeContext context = contextHelper.getSmallRyeContext(dfe);
    try {
        SmallRyeContext.setContext(context);
        Publisher<?> publisher = operationInvoker.invoke(transformedArguments);
        Multi<?> multi = Multi.createFrom().publisher(publisher);
        return (O) multi.onItem().transform((t) -> {
            try {
                Object resultFromTransform = fieldHelper.transformOrAdaptResponse(t, dfe);
                resultBuilder.data(resultFromTransform);
                return (O) resultBuilder.build();
            } catch (AbstractDataFetcherException abstractDataFetcherException) {
                // Arguments or result couldn't be transformed
                abstractDataFetcherException.appendDataFetcherResult(resultBuilder, dfe);
                eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), abstractDataFetcherException);
                return (O) resultBuilder.build();
            }
        }).onFailure().recoverWithItem(new Function<Throwable, O>() {

            @Override
            public O apply(Throwable throwable) {
                eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
                if (throwable instanceof GraphQLException) {
                    GraphQLException graphQLException = (GraphQLException) throwable;
                    errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
                } else if (throwable instanceof Exception) {
                    DataFetcherException dataFetcherException = SmallRyeGraphQLServerMessages.msg.dataFetcherException(operation, throwable);
                    errorResultHelper.appendException(resultBuilder, dfe, dataFetcherException);
                } else if (throwable instanceof Error) {
                    errorResultHelper.appendException(resultBuilder, dfe, throwable);
                }
                return (O) resultBuilder.build();
            }
        });
    } finally {
        SmallRyeContext.remove();
    }
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) SmallRyeGraphQLServerMessages(io.smallrye.graphql.SmallRyeGraphQLServerMessages) Publisher(org.reactivestreams.Publisher) SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) Function(java.util.function.Function) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) BatchLoaderEnvironment(org.dataloader.BatchLoaderEnvironment) Operation(io.smallrye.graphql.schema.model.Operation) DataFetcherResult(graphql.execution.DataFetcherResult) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) Function(java.util.function.Function) SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) GraphQLException(org.eclipse.microprofile.graphql.GraphQLException)

Aggregations

GraphQLException (org.eclipse.microprofile.graphql.GraphQLException)7 SmallRyeContext (io.smallrye.graphql.execution.context.SmallRyeContext)5 AbstractDataFetcherException (io.smallrye.graphql.transformation.AbstractDataFetcherException)5 DataFetcherResult (graphql.execution.DataFetcherResult)4 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)3 SmallRyeGraphQLServerMessages (io.smallrye.graphql.SmallRyeGraphQLServerMessages)3 Operation (io.smallrye.graphql.schema.model.Operation)3 Multi (io.smallrye.mutiny.Multi)3 List (java.util.List)3 CompletionStage (java.util.concurrent.CompletionStage)3 BatchLoaderEnvironment (org.dataloader.BatchLoaderEnvironment)3 Uni (io.smallrye.mutiny.Uni)2 Infrastructure (io.smallrye.mutiny.infrastructure.Infrastructure)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Function (java.util.function.Function)2 InvokeInfo (io.smallrye.graphql.execution.event.InvokeInfo)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 ThreadContext (org.eclipse.microprofile.context.ThreadContext)1 Publisher (org.reactivestreams.Publisher)1