Java源码示例:graphql.execution.DataFetcherResult

示例1
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);
}
 
示例2
@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);
}
 
示例3
@SuppressWarnings("unchecked")
public <T, S> S convertOutput(T output, AnnotatedElement element, AnnotatedType type) {
    if (output == null) {
        return null;
    }

    // Transparently handle unexpected wrapped results. This enables elegant exception handling, partial results etc.
    if (DataFetcherResult.class.equals(output.getClass()) && !DataFetcherResult.class.equals(resolver.getRawReturnType())) {
        DataFetcherResult<?> result = (DataFetcherResult<?>) output;
        if (result.getData() != null) {
            Object convertedData = convert(result.getData(), element, type);
            return (S) DataFetcherResult.newResult()
                    .data(convertedData)
                    .errors(result.getErrors())
                    .localContext(result.getLocalContext())
                    .mapRelativeErrors(result.isMapRelativeErrors())
                    .build();
        }
    }

    return convert(output, element, type);
}
 
示例4
@NotNull
private List<GraphQLError> convertErrors(Throwable throwable, Object result) {
    ArrayList<GraphQLError> graphQLErrors = new ArrayList<>();

    if (throwable != null) {
        if (throwable instanceof GraphQLError) {
            graphQLErrors.add((GraphQLError) throwable);
        } else {
            String message = throwable.getMessage();
            if (message == null) {
                message = "(null)";
            }
            GraphqlErrorBuilder errorBuilder = GraphqlErrorBuilder.newError()
                    .message(message);

            if (throwable instanceof InvalidSyntaxException) {
                errorBuilder.location(((InvalidSyntaxException) throwable).getLocation());
            }

            graphQLErrors.add(errorBuilder.build());
        }
    }

    if (result instanceof DataFetcherResult<?>) {
        DataFetcherResult<?> theResult = (DataFetcherResult) result;
        if (theResult.hasErrors()) {
            graphQLErrors.addAll(theResult.getErrors());
        }
    }

    return graphQLErrors;
}
 
示例5
/**
 * 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();
}
 
示例6
public static Object mkDFRFromFetchedResult(List<GraphQLError> errors, Object value) {
  if (value instanceof CompletionStage) {
    return ((CompletionStage<?>) value).thenApply(v -> mkDFRFromFetchedResult(errors, v));
  } else if (value instanceof DataFetcherResult) {
    DataFetcherResult df = (DataFetcherResult) value;
    return mkDFR(df.getData(), concat(errors, df.getErrors()), df.getLocalContext());
  } else {
    return mkDFR(value, errors, null);
  }
}
 
示例7
private <R> CompletableFuture<DataFetcherResult<List<R>>> collect(Publisher<R> publisher, ExecutionStepInfo step) {
    CompletableFuture<DataFetcherResult<List<R>>> promise = new CompletableFuture<>();

    executor.execute(() -> publisher.subscribe(new Subscriber<R>() {

        private List<R> buffer = new ArrayList<>();

        @Override
        public void onSubscribe(Subscription subscription) {
            subscription.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(R result) {
            buffer.add(result);
        }

        @Override
        public void onError(Throwable error) {
            ExceptionWhileDataFetching wrapped = new ExceptionWhileDataFetching(step.getPath(), error, step.getField().getSingleField().getSourceLocation());
            promise.complete(DataFetcherResult.<List<R>>newResult()
                    .data(buffer)
                    .error(wrapped)
                    .build());
        }

        @Override
        public void onComplete() {
            promise.complete(DataFetcherResult.<List<R>>newResult().data(buffer).build());
        }
    }));
    return promise;
}
 
示例8
@Override
public Object aroundInvoke(InvocationContext context, ResolverInterceptor.Continuation continuation) throws Exception {
    return DataFetcherResult.newResult()
            .data(continuation.proceed(context))
            .error(GraphqlErrorBuilder.newError(context.getResolutionEnvironment().dataFetchingEnvironment)
                    .message("Test error")
                    .errorType(ErrorType.DataFetchingException)
                    .build())
            .build();
}
 
示例9
public abstract DataFetcherResult.Builder<Object> appendDataFetcherResult(DataFetcherResult.Builder<Object> builder,
DataFetchingEnvironment dfe);
 
示例10
public static DataFetcherResult<Object> mkDFR(Object value, List<GraphQLError> errors, Object localContext) {
return DataFetcherResult.newResult().data(value).errors(errors).localContext(localContext).build();
}
 
示例11
@Override
public GraphQLInputType toGraphQLInputType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    throw new UnsupportedOperationException(DataFetcherResult.class.getSimpleName() + " can not be used as an input type");
}
 
示例12
@Override
public AnnotatedType getSubstituteType(AnnotatedType original) {
    AnnotatedType innerType = GenericTypeReflector.getTypeParameter(original, DataFetcherResult.class.getTypeParameters()[0]);
    return ClassUtils.addAnnotations(innerType, original.getAnnotations());
}
 
示例13
@Override
public boolean supports(AnnotatedElement element, AnnotatedType type) {
    return ClassUtils.isSuperClass(DataFetcherResult.class, type);
}