public CompletableFuture<FetchedValue> fetchValue(ExecutionContext executionContext, Object source, Object localContext, MergedField sameFields, ExecutionStepInfo executionInfo) { Field field = sameFields.getSingleField(); GraphQLFieldDefinition fieldDef = executionInfo.getFieldDefinition(); GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); GraphQLFieldsContainer parentType = getFieldsContainer(executionInfo); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables()); GraphQLOutputType fieldType = fieldDef.getType(); DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext, fieldType, sameFields); DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext) .source(source) .localContext(localContext) .arguments(argumentValues) .fieldDefinition(fieldDef) .mergedField(sameFields) .fieldType(fieldType) .executionStepInfo(executionInfo) .parentType(parentType) .selectionSet(fieldCollector) .build(); ExecutionId executionId = executionContext.getExecutionId(); ExecutionPath path = executionInfo.getPath(); return callDataFetcher(codeRegistry, parentType, fieldDef, environment, executionId, path) .thenApply(rawFetchedValue -> FetchedValue.newFetchedValue() .fetchedValue(rawFetchedValue) .rawFetchedValue(rawFetchedValue) .build()) .exceptionally(exception -> handleExceptionWhileFetching(field, path, exception)) .thenApply(result -> unboxPossibleDataFetcherResult(sameFields, path, result, localContext)) .thenApply(this::unboxPossibleOptional); }
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(); } }
.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();
.fetchedValue(dataFetcherResult.getData()) .rawFetchedValue(result.getRawFetchedValue()) .errors(newErrors) .localContext(newLocalContext) .build(); } else { return result;
public CompletableFuture<FetchedValue> fetchValue(ExecutionContext executionContext, Object source, Object localContext, MergedField sameFields, ExecutionStepInfo executionInfo) { Field field = sameFields.getSingleField(); GraphQLFieldDefinition fieldDef = executionInfo.getFieldDefinition(); GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); GraphQLFieldsContainer parentType = getFieldsContainer(executionInfo); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables()); GraphQLOutputType fieldType = fieldDef.getType(); DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext, fieldType, sameFields); DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext) .source(source) .localContext(localContext) .arguments(argumentValues) .fieldDefinition(fieldDef) .mergedField(sameFields) .fieldType(fieldType) .executionStepInfo(executionInfo) .parentType(parentType) .selectionSet(fieldCollector) .build(); ExecutionId executionId = executionContext.getExecutionId(); ExecutionPath path = executionInfo.getPath(); return callDataFetcher(codeRegistry, parentType, fieldDef, environment, executionId, path) .thenApply(rawFetchedValue -> FetchedValue.newFetchedValue() .fetchedValue(rawFetchedValue) .rawFetchedValue(rawFetchedValue) .build()) .exceptionally(exception -> handleExceptionWhileFetching(field, path, exception)) .thenApply(result -> unboxPossibleDataFetcherResult(sameFields, path, result, localContext)) .thenApply(this::unboxPossibleOptional); }
@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; }
public static Builder newFetchedValue() { return new Builder(); }
private FetchedValue handleExceptionWhileFetching(Field field, ExecutionPath path, Throwable exception) { ExceptionWhileDataFetching exceptionWhileDataFetching = new ExceptionWhileDataFetching(path, exception, field.getSourceLocation()); return FetchedValue.newFetchedValue().errors(singletonList(exceptionWhileDataFetching)).build(); }
private FetchedValue unboxPossibleOptional(FetchedValue result) { return result.transform( builder -> builder.fetchedValue(UnboxPossibleOptional.unboxPossibleOptional(result.getFetchedValue())) ); }
public FetchedValue transform(Consumer<Builder> builderConsumer) { Builder builder = newFetchedValue(this); builderConsumer.accept(builder); return builder.build(); }
private CompletableFuture<ExecutionResult> executeSubscriptionEvent(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object eventPayload) { ExecutionContext newExecutionContext = executionContext.transform(builder -> builder.root(eventPayload)); ExecutionStrategyParameters newParameters = firstFieldOfSubscriptionSelection(parameters); FetchedValue fetchedValue = FetchedValue.newFetchedValue().fetchedValue(eventPayload) .rawFetchedValue(eventPayload) .localContext(parameters.getLocalContext()) .build(); return completeField(newExecutionContext, newParameters, fetchedValue).getFieldValue() .thenApply(executionResult -> wrapWithRootFieldName(newParameters, executionResult)); }
public static Builder newFetchedValue(FetchedValue otherValue) { return new Builder() .fetchedValue(otherValue.getFetchedValue()) .rawFetchedValue(otherValue.getRawFetchedValue()) .errors(otherValue.getErrors()) .localContext(otherValue.getLocalContext()) ; }
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; } } }
@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; }
public static Builder newFetchedValue() { return new Builder(); }
private CompletableFuture<ExecutionResult> executeSubscriptionEvent(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object eventPayload) { ExecutionContext newExecutionContext = executionContext.transform(builder -> builder.root(eventPayload)); ExecutionStrategyParameters newParameters = firstFieldOfSubscriptionSelection(parameters); FetchedValue fetchedValue = FetchedValue.newFetchedValue().fetchedValue(eventPayload) .rawFetchedValue(eventPayload) .localContext(parameters.getLocalContext()) .build(); return completeField(newExecutionContext, newParameters, fetchedValue).getFieldValue() .thenApply(executionResult -> wrapWithRootFieldName(newParameters, executionResult)); }
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()) ; }
private FetchedValue unboxPossibleOptional(FetchedValue result) { return result.transform( builder -> builder.fetchedValue(UnboxPossibleOptional.unboxPossibleOptional(result.getFetchedValue())) ); }
public FetchedValue transform(Consumer<Builder> builderConsumer) { Builder builder = newFetchedValue(this); builderConsumer.accept(builder); return builder.build(); }