Search in sources :

Example 1 with AbstractDataFetcherException

use of io.smallrye.graphql.transformation.AbstractDataFetcherException in project smallrye-graphql by smallrye.

the class FieldHelper method singleAdapting.

/**
 * By now this is a 'leaf' value, i.e not a collection of array, so we just adapt to if needed.
 *
 * @param argumentValue the value to map
 * @param field the field as created while scanning
 * @return mapped value
 */
@Override
Object singleAdapting(Object argumentValue, Field field, DataFetchingEnvironment dfe) throws AbstractDataFetcherException {
    if (argumentValue == null) {
        return null;
    }
    if (field.isAdaptingWith()) {
        AdaptWith adaptWith = field.getAdaptWith();
        ReflectionInvoker reflectionInvoker = getReflectionInvokerForOutput(adaptWith);
        try {
            Object adaptedObject = reflectionInvoker.invoke(argumentValue);
            return adaptedObject;
        } catch (Exception ex) {
            log.transformError(ex);
            throw new TransformException(ex, field, argumentValue);
        }
    } else if (field.hasWrapper() && field.getWrapper().isMap()) {
        Object key = null;
        Map<String, Object> arguments = dfe.getArguments();
        if (arguments != null && arguments.size() > 0 && arguments.containsKey(KEY)) {
            key = arguments.get(KEY);
        }
        Set entrySet = mapAdapter.to((Map) argumentValue, (List) key, field);
        return recursiveAdapting(entrySet, mapAdapter.getAdaptedField(field), dfe);
    }
    return argumentValue;
}
Also used : Set(java.util.Set) AdaptWith(io.smallrye.graphql.schema.model.AdaptWith) TransformException(io.smallrye.graphql.transformation.TransformException) List(java.util.List) Map(java.util.Map) TransformException(io.smallrye.graphql.transformation.TransformException) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException)

Example 2 with AbstractDataFetcherException

use of io.smallrye.graphql.transformation.AbstractDataFetcherException in project smallrye-graphql by smallrye.

the class ArgumentHelper method adaptInputWith.

private Object adaptInputWith(Field field, Object argumentValue, DataFetchingEnvironment dfe) throws TransformException, AbstractDataFetcherException {
    if (argumentValue == null) {
        return null;
    }
    if (field.isAdaptingWith()) {
        AdaptWith adaptWith = field.getAdaptWith();
        ReflectionInvoker reflectionInvoker = getReflectionInvokerForInput(adaptWith);
        if (Map.class.isAssignableFrom(argumentValue.getClass())) {
            argumentValue = correctComplexObjectFromMap((Map) argumentValue, field, dfe);
        }
        try {
            Object adaptedObject = reflectionInvoker.invoke(argumentValue);
            return adaptedObject;
        } catch (Exception ex) {
            log.transformError(ex);
            throw new TransformException(ex, field, argumentValue);
        }
    }
    return argumentValue;
}
Also used : AdaptWith(io.smallrye.graphql.schema.model.AdaptWith) TransformException(io.smallrye.graphql.transformation.TransformException) HashMap(java.util.HashMap) Map(java.util.Map) JsonbException(javax.json.bind.JsonbException) TransformException(io.smallrye.graphql.transformation.TransformException) AbstractDataFetcherException(io.smallrye.graphql.transformation.AbstractDataFetcherException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with AbstractDataFetcherException

use of io.smallrye.graphql.transformation.AbstractDataFetcherException 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 4 with AbstractDataFetcherException

use of io.smallrye.graphql.transformation.AbstractDataFetcherException 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 AbstractDataFetcherException

use of io.smallrye.graphql.transformation.AbstractDataFetcherException 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)

Aggregations

AbstractDataFetcherException (io.smallrye.graphql.transformation.AbstractDataFetcherException)7 SmallRyeContext (io.smallrye.graphql.execution.context.SmallRyeContext)5 GraphQLException (org.eclipse.microprofile.graphql.GraphQLException)5 DataFetcherResult (graphql.execution.DataFetcherResult)4 List (java.util.List)4 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)3 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 AdaptWith (io.smallrye.graphql.schema.model.AdaptWith)2 TransformException (io.smallrye.graphql.transformation.TransformException)2 Multi (io.smallrye.mutiny.Multi)2 Infrastructure (io.smallrye.mutiny.infrastructure.Infrastructure)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Uni (io.smallrye.mutiny.Uni)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1