@SuppressWarnings("unchecked") private List<FetchedValue> extractBatchedValues(FetchedValue fetchedValueContainingList, int expectedSize) { List<Object> list = (List<Object>) fetchedValueContainingList.getFetchedValue(); Assert.assertTrue(list.size() == expectedSize, "Unexpected result size"); List<FetchedValue> result = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { List<GraphQLError> errors; if (i == 0) { errors = fetchedValueContainingList.getErrors(); } else { errors = Collections.emptyList(); } FetchedValue fetchedValue = FetchedValue.newFetchedValue() .fetchedValue(list.get(i)) .rawFetchedValue(fetchedValueContainingList.getRawFetchedValue()) .errors(errors) .localContext(fetchedValueContainingList.getLocalContext()) .build(); result.add(fetchedValue); } return result; }
private FetchedValue unboxPossibleDataFetcherResult(MergedField sameField, ExecutionPath executionPath, FetchedValue result, Object localContext) { if (result.getFetchedValue() instanceof DataFetcherResult) { DataFetcherResult<?> dataFetcherResult = (DataFetcherResult) result.getFetchedValue(); List<AbsoluteGraphQLError> addErrors = dataFetcherResult.getErrors().stream() .map(relError -> new AbsoluteGraphQLError(sameField, executionPath, relError)) .collect(Collectors.toList()); List<GraphQLError> newErrors = new ArrayList<>(result.getErrors()); newErrors.addAll(addErrors); Object newLocalContext = dataFetcherResult.getLocalContext(); if (newLocalContext == null) { // if the field returns nothing then they get the context of their parent field newLocalContext = localContext; } return FetchedValue.newFetchedValue() .fetchedValue(dataFetcherResult.getData()) .rawFetchedValue(result.getRawFetchedValue()) .errors(newErrors) .localContext(newLocalContext) .build(); } else { return result; } } }
private FetchedValue handleExceptionWhileFetching(Field field, ExecutionPath path, Throwable exception) { ExceptionWhileDataFetching exceptionWhileDataFetching = new ExceptionWhileDataFetching(path, exception, field.getSourceLocation()); return FetchedValue.newFetchedValue().errors(singletonList(exceptionWhileDataFetching)).build(); }
.fetchedValue(UnboxPossibleOptional.unboxPossibleOptional(dataFetcherResult.getData())) .rawFetchedValue(dataFetcherResult.getData()) .errors(dataFetcherResult.getErrors()) .localContext(localContext) .build();
.fetchedValue(dataFetcherResult.getData()) .rawFetchedValue(result.getRawFetchedValue()) .errors(newErrors) .localContext(newLocalContext) .build();
@SuppressWarnings("unchecked") private List<FetchedValue> extractBatchedValues(FetchedValue fetchedValueContainingList, int expectedSize) { List<Object> list = (List<Object>) fetchedValueContainingList.getFetchedValue(); Assert.assertTrue(list.size() == expectedSize, "Unexpected result size"); List<FetchedValue> result = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { List<GraphQLError> errors; if (i == 0) { errors = fetchedValueContainingList.getErrors(); } else { errors = Collections.emptyList(); } FetchedValue fetchedValue = FetchedValue.newFetchedValue() .fetchedValue(list.get(i)) .rawFetchedValue(fetchedValueContainingList.getRawFetchedValue()) .errors(errors) .localContext(fetchedValueContainingList.getLocalContext()) .build(); result.add(fetchedValue); } return result; }
private FetchedValue handleExceptionWhileFetching(Field field, ExecutionPath path, Throwable exception) { ExceptionWhileDataFetching exceptionWhileDataFetching = new ExceptionWhileDataFetching(path, exception, field.getSourceLocation()); return FetchedValue.newFetchedValue().errors(singletonList(exceptionWhileDataFetching)).build(); }
public static Builder newFetchedValue(FetchedValue otherValue) { return new Builder() .fetchedValue(otherValue.getFetchedValue()) .rawFetchedValue(otherValue.getRawFetchedValue()) .errors(otherValue.getErrors()) .localContext(otherValue.getLocalContext()) ; }
FetchedValue unboxPossibleDataFetcherResult(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object result) { if (result instanceof DataFetcherResult) { //noinspection unchecked DataFetcherResult<?> dataFetcherResult = (DataFetcherResult) result; dataFetcherResult.getErrors().stream() .map(relError -> new AbsoluteGraphQLError(parameters, relError)) .forEach(executionContext::addError); Object localContext = dataFetcherResult.getLocalContext(); if (localContext == null) { // if the field returns nothing then they get the context of their parent field localContext = parameters.getLocalContext(); } return FetchedValue.newFetchedValue() .fetchedValue(UnboxPossibleOptional.unboxPossibleOptional(dataFetcherResult.getData())) .rawFetchedValue(dataFetcherResult.getData()) .errors(dataFetcherResult.getErrors()) .localContext(localContext) .build(); } else { return FetchedValue.newFetchedValue() .fetchedValue(UnboxPossibleOptional.unboxPossibleOptional(result)) .rawFetchedValue(result) .build(); } }
public static Builder newFetchedValue(FetchedValue otherValue) { return new Builder() .fetchedValue(otherValue.getFetchedValue()) .rawFetchedValue(otherValue.getRawFetchedValue()) .errors(otherValue.getErrors()) .localContext(otherValue.getLocalContext()) ; }