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