protected BiConsumer<List<ExecutionResult>, Throwable> handleResults(ExecutionContext executionContext, List<String> fieldNames, CompletableFuture<ExecutionResult> overallResult) { return (List<ExecutionResult> results, Throwable exception) -> { if (exception != null) { handleNonNullException(executionContext, overallResult, exception); return; } Map<String, Object> resolvedValuesByField = new LinkedHashMap<>(); int ix = 0; for (ExecutionResult executionResult : results) { String fieldName = fieldNames.get(ix++); resolvedValuesByField.put(fieldName, executionResult.getData()); } overallResult.complete(new ExecutionResultImpl(resolvedValuesByField, executionContext.getErrors())); }; } }
protected ExecutionResult handleNonNullException(ExecutionContext executionContext, CompletableFuture<ExecutionResult> result, Throwable e) { ExecutionResult executionResult = null; List<GraphQLError> errors = new ArrayList<>(executionContext.getErrors()); Throwable underlyingException = e; if (e instanceof CompletionException) { underlyingException = e.getCause(); } if (underlyingException instanceof NonNullableFieldWasNullException) { assertNonNullFieldPrecondition((NonNullableFieldWasNullException) underlyingException, result); if (!result.isDone()) { executionResult = new ExecutionResultImpl(null, errors); result.complete(executionResult); } } else if (underlyingException instanceof AbortExecutionException) { AbortExecutionException abortException = (AbortExecutionException) underlyingException; executionResult = abortException.toExecutionResult(); result.complete(executionResult); } else { result.completeExceptionally(e); } return executionResult; }
@Override public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException { CompletableFuture<Publisher<Object>> sourceEventStream = createSourceEventStream(executionContext, parameters); // // when the upstream source event stream completes, subscribe to it and wire in our adapter return sourceEventStream.thenApply((publisher) -> { if (publisher == null) { return new ExecutionResultImpl(null, executionContext.getErrors()); } CompletionStageMappingPublisher<ExecutionResult, Object> mapSourceToResponse = new CompletionStageMappingPublisher<>( publisher, eventPayload -> executeSubscriptionEvent(executionContext, parameters, eventPayload) ); return new ExecutionResultImpl(mapSourceToResponse, executionContext.getErrors()); }); }
private CompletableFuture<ExecutionResult> completeFields(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Map<String, FetchedValue> fetchedValues) { MergedSelectionSet fields = parameters.getFields(); // then for every fetched value, complete it, breath first Map<String, Object> results = new LinkedHashMap<>(); for (String fieldName : fetchedValues.keySet()) { ExecutionStrategyParameters newParameters = newParameters(parameters, fields, fieldName); FetchedValue fetchedValue = fetchedValues.get(fieldName); try { ExecutionResult resolvedResult = completeField(executionContext, newParameters, fetchedValue).getFieldValue().join(); results.put(fieldName, resolvedResult != null ? resolvedResult.getData() : null); } catch (NonNullableFieldWasNullException e) { assertNonNullFieldPrecondition(e); results = null; break; } } return CompletableFuture.completedFuture(new ExecutionResultImpl(results, executionContext.getErrors())); }
ExecutionResultImpl executionResult = new ExecutionResultImpl(results, executionContext.getErrors()); overallResult.complete(executionResult);
@Internal ExecutionContextBuilder(ExecutionContext other) { instrumentation = other.getInstrumentation(); executionId = other.getExecutionId(); instrumentationState = other.getInstrumentationState(); graphQLSchema = other.getGraphQLSchema(); queryStrategy = other.getQueryStrategy(); mutationStrategy = other.getMutationStrategy(); subscriptionStrategy = other.getSubscriptionStrategy(); context = other.getContext(); root = other.getRoot(); document = other.getDocument(); operationDefinition = other.getOperationDefinition(); variables = new HashMap<>(other.getVariables()); fragmentsByName = new HashMap<>(other.getFragmentsByName()); dataLoaderRegistry = other.getDataLoaderRegistry(); errors = new ArrayList<>(other.getErrors()); }
@Override public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException { MergedSelectionSet fields = parameters.getFields(); Map<String, FetchedValue> fetchedValues = new LinkedHashMap<>(); // first fetch every value for (String fieldName : fields.keySet()) { FetchedValue fetchedValue = fetchField(executionContext, parameters, fields, fieldName); fetchedValues.put(fieldName, fetchedValue); } // then for every fetched value, complete it Map<String, Object> results = new LinkedHashMap<>(); for (String fieldName : fetchedValues.keySet()) { MergedField currentField = fields.getSubField(fieldName); FetchedValue fetchedValue = fetchedValues.get(fieldName); ExecutionPath fieldPath = parameters.getPath().segment(fieldName); ExecutionStrategyParameters newParameters = parameters .transform(builder -> builder.field(currentField).path(fieldPath)); try { completeValue(executionContext, results, fieldName, fetchedValue, newParameters); } catch (NonNullableFieldWasNullException e) { assertNonNullFieldPrecondition(e); results = null; break; } } return CompletableFuture.completedFuture(new ExecutionResultImpl(results, executionContext.getErrors())); }
overallResult.complete(new ExecutionResultImpl(root.getParentResults().get(0).toObject(), executionContext.getErrors())); return;
result = completedFuture(new ExecutionResultImpl(null, executionContext.getErrors()));
public List<GraphQLError> getErrors() { return delegate.getErrors(); }
protected BiConsumer<List<ExecutionResult>, Throwable> handleResults(ExecutionContext executionContext, List<String> fieldNames, CompletableFuture<ExecutionResult> overallResult) { return (List<ExecutionResult> results, Throwable exception) -> { if (exception != null) { handleNonNullException(executionContext, overallResult, exception); return; } Map<String, Object> resolvedValuesByField = new LinkedHashMap<>(); int ix = 0; for (ExecutionResult executionResult : results) { String fieldName = fieldNames.get(ix++); resolvedValuesByField.put(fieldName, executionResult.getData()); } overallResult.complete(new ExecutionResultImpl(resolvedValuesByField, executionContext.getErrors())); }; } }
protected ExecutionResult handleNonNullException(ExecutionContext executionContext, CompletableFuture<ExecutionResult> result, Throwable e) { ExecutionResult executionResult = null; List<GraphQLError> errors = new ArrayList<>(executionContext.getErrors()); Throwable underlyingException = e; if (e instanceof CompletionException) { underlyingException = e.getCause(); } if (underlyingException instanceof NonNullableFieldWasNullException) { assertNonNullFieldPrecondition((NonNullableFieldWasNullException) underlyingException, result); if (!result.isDone()) { executionResult = new ExecutionResultImpl(null, errors); result.complete(executionResult); } } else if (underlyingException instanceof AbortExecutionException) { AbortExecutionException abortException = (AbortExecutionException) underlyingException; executionResult = abortException.toExecutionResult(); result.complete(executionResult); } else { result.completeExceptionally(e); } return executionResult; }
@Override public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException { CompletableFuture<Publisher<Object>> sourceEventStream = createSourceEventStream(executionContext, parameters); // // when the upstream source event stream completes, subscribe to it and wire in our adapter return sourceEventStream.thenApply((publisher) -> { if (publisher == null) { return new ExecutionResultImpl(null, executionContext.getErrors()); } CompletionStageMappingPublisher<ExecutionResult, Object> mapSourceToResponse = new CompletionStageMappingPublisher<>( publisher, eventPayload -> executeSubscriptionEvent(executionContext, parameters, eventPayload) ); return new ExecutionResultImpl(mapSourceToResponse, executionContext.getErrors()); }); }
public ExecutionResult doExecute(ExecutionContext executionContext, GraphQLObjectType parentType, Object source, Map<String, List<Field>> fields) { List<Observable<Pair<String, Object>>> observablesResult = new ArrayList<>(); List<Observable<Double>> observablesComplexity = new ArrayList<>(); for (String fieldName : fields.keySet()) { final List<Field> fieldList = fields.get(fieldName); ExecutionResult executionResult = resolveField(executionContext, parentType, source, fieldList); observablesResult.add(unwrapExecutionResult(fieldName, executionResult)); observablesComplexity.add(calculateFieldComplexity(executionContext, parentType, fieldList, executionResult != null ? ((GraphQLRxExecutionResult) executionResult).getComplexityObservable() : Observable.just(0.0))); } Observable<Map<String, Object>> result = Observable.merge(observablesResult) .toMap(Pair::getLeft, Pair::getRight); GraphQLRxExecutionResult graphQLRxExecutionResult = new GraphQLRxExecutionResult(result, Observable.just(executionContext.getErrors()), MathObservable.sumDouble(Observable.merge(observablesComplexity))); return graphQLRxExecutionResult; }
@Override public ExecutionResult execute(ExecutionContext executionContext, GraphQLObjectType parentType, Object source, Map<String, List<Field>> fields) { List<Observable<Pair<String, ?>>> observables = new ArrayList<>(); for (String fieldName : fields.keySet()) { final List<Field> fieldList = fields.get(fieldName); ExecutionResult executionResult = resolveField(executionContext, parentType, source, fieldList); if (executionResult instanceof RxExecutionResult) { RxExecutionResult rxResult = (RxExecutionResult)executionResult; Observable<?> unwrapped = rxResult.getDataObservable().flatMap(potentialResult -> { if (potentialResult instanceof RxExecutionResult) { return ((RxExecutionResult) potentialResult).getDataObservable(); } if (potentialResult instanceof ExecutionResult) { return Observable.just(((ExecutionResult) potentialResult).getData()); } return Observable.just(potentialResult); }); observables.add(Observable.zip(Observable.just(fieldName), unwrapped, Pair::of)); } else { observables.add(Observable.just(Pair.of(fieldName, executionResult != null ? executionResult.getData() : null))); } } Observable<Map<String, Object>> result = Observable.merge(observables) .toMap(Pair::getLeft, Pair::getRight); return new RxExecutionResult(result, Observable.just(executionContext.getErrors())); }
.toMap(Pair::getLeft, Pair::getRight); GraphQLRxExecutionResult graphQLRxExecutionResult = new GraphQLRxExecutionResult(result, Observable.just(executionContext.getErrors()), MathObservable.sumDouble(Observable.merge(observablesComplexity))); return graphQLRxExecutionResult;
}); return new ExecutionResultImpl(changesFlow, context.getErrors());
ExecutionResultImpl executionResult = new ExecutionResultImpl(results, executionContext.getErrors()); overallResult.complete(executionResult);
@Internal ExecutionContextBuilder(ExecutionContext other) { instrumentation = other.getInstrumentation(); executionId = other.getExecutionId(); instrumentationState = other.getInstrumentationState(); graphQLSchema = other.getGraphQLSchema(); queryStrategy = other.getQueryStrategy(); mutationStrategy = other.getMutationStrategy(); subscriptionStrategy = other.getSubscriptionStrategy(); context = other.getContext(); root = other.getRoot(); document = other.getDocument(); operationDefinition = other.getOperationDefinition(); variables = new HashMap<>(other.getVariables()); fragmentsByName = new HashMap<>(other.getFragmentsByName()); dataLoaderRegistry = other.getDataLoaderRegistry(); errors = new ArrayList<>(other.getErrors()); }
result = completedFuture(new ExecutionResultImpl(null, executionContext.getErrors()));