Java Code Examples for graphql.execution.DataFetcherResult#Builder

The following examples show how to use graphql.execution.DataFetcherResult#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractDataFetcher.java    From smallrye-graphql with Apache License 2.0 6 votes vote down vote up
protected final DataFetcherResult.Builder<Object> appendPartialResult(
        DataFetcherResult.Builder<Object> resultBuilder,
        DataFetchingEnvironment dfe,
        GraphQLException graphQLException) {
    DataFetcherExceptionHandlerParameters handlerParameters = DataFetcherExceptionHandlerParameters
            .newExceptionParameters()
            .dataFetchingEnvironment(dfe)
            .exception(graphQLException)
            .build();

    SourceLocation sourceLocation = handlerParameters.getSourceLocation();
    ExecutionPath path = handlerParameters.getPath();
    GraphQLExceptionWhileDataFetching error = new GraphQLExceptionWhileDataFetching(path, graphQLException,
            sourceLocation);

    return resultBuilder
            .data(graphQLException.getPartialResults())
            .error(error);
}
 
Example 2
Source File: TransformException.java    From smallrye-graphql with Apache License 2.0 6 votes vote down vote up
@Override
public DataFetcherResult.Builder<Object> appendDataFetcherResult(DataFetcherResult.Builder<Object> builder,
        DataFetchingEnvironment dfe) {
    DataFetcherExceptionHandlerParameters handlerParameters = DataFetcherExceptionHandlerParameters
            .newExceptionParameters()
            .dataFetchingEnvironment(dfe)
            .exception(super.getCause())
            .build();

    SourceLocation sourceLocation = getSourceLocation(dfe, handlerParameters);

    List<String> paths = toPathList(handlerParameters.getPath());

    ValidationError error = new ValidationError(ValidationErrorType.WrongType,
            sourceLocation, "argument '" + field.getName() + "' with value 'StringValue{value='" + parameterValue
                    + "'}' is not a valid '" + getScalarTypeName() + "'",
            paths);

    return builder.error(error);
}
 
Example 3
Source File: ReflectionDataFetcher.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
/**
 * This makes the call on the method. We do the following:
 * 1) Get the correct instance of the class we want to make the call in using CDI. That allow the developer to still use
 * Scopes in the bean.
 * 2) Get the argument values (if any) from graphql-java and make sue they are in the correct type, and if needed,
 * transformed.
 * 3) Make a call on the method with the correct arguments
 * 4) get the result and if needed transform it before we return it.
 * 
 * @param dfe the Data Fetching Environment from graphql-java
 * @return the result from the call.
 */
@Override
public DataFetcherResult<Object> get(DataFetchingEnvironment dfe) throws Exception {
    //TODO: custom context object?
    final GraphQLContext context = GraphQLContext.newContext().build();
    final DataFetcherResult.Builder<Object> resultBuilder = DataFetcherResult.newResult().localContext(context);

    Class<?> operationClass = classloadingService.loadClass(operation.getClassName());
    Object declaringObject = lookupService.getInstance(operationClass);
    Method m = getMethod(operationClass);

    try {
        Object[] transformedArguments = argumentHelper.getArguments(dfe);

        ExecutionContextImpl executionContext = new ExecutionContextImpl(declaringObject, m, transformedArguments, context,
                dfe,
                decorators.iterator());

        Object resultFromMethodCall = execute(executionContext);

        // See if we need to transform on the way out
        resultBuilder.data(fieldHelper.transformResponse(resultFromMethodCall));
    } catch (AbstractDataFetcherException pe) {
        //Arguments or result couldn't be transformed
        pe.appendDataFetcherResult(resultBuilder, dfe);
    } catch (GraphQLException graphQLException) {
        appendPartialResult(resultBuilder, dfe, graphQLException);
    } catch (SecurityException | IllegalAccessException | IllegalArgumentException ex) {
        //m.invoke failed
        throw msg.dataFetcherException(operation, ex);
    }

    return resultBuilder.build();
}
 
Example 4
Source File: AbstractDataFetcherException.java    From smallrye-graphql with Apache License 2.0 4 votes vote down vote up
public abstract DataFetcherResult.Builder<Object> appendDataFetcherResult(DataFetcherResult.Builder<Object> builder,
DataFetchingEnvironment dfe);