private void completeValue(ExecutionContext executionContext, Map<String, Object> results, String fieldName, FetchedValue fetchedValue, ExecutionStrategyParameters newParameters) { ExecutionResult resolvedResult = completeField(executionContext, newParameters, fetchedValue).getFieldValue().join(); results.put(fieldName, resolvedResult != null ? resolvedResult.getData() : null); }
public FieldValueInfo build() { return new FieldValueInfo(completeValueType, executionResultFuture, listInfos); } }
private int getCountForList(FieldValueInfo fieldValueInfo) { int result = 0; for (FieldValueInfo cvi : fieldValueInfo.getFieldValueInfos()) { if (cvi.getCompleteValueType() == FieldValueInfo.CompleteValueType.OBJECT) { result++; } else if (cvi.getCompleteValueType() == FieldValueInfo.CompleteValueType.LIST) { result += getCountForList(cvi); } } return result; }
CompletableFuture<List<ExecutionResult>> resultsFuture = Async.each(fieldValueInfos, (item, i) -> item.getFieldValue()); overallResult.whenComplete(completeListCtx::onCompleted); return FieldValueInfo.newFieldValueInfo(LIST) .fieldValue(overallResult) .fieldValueInfos(fieldValueInfos)
/** * Called to complete a list of value for a field based on a list type. This iterates the values and calls * {@link #completeValue(ExecutionContext, ExecutionStrategyParameters)} for each value. * * @param executionContext contains the top level execution parameters * @param parameters contains the parameters holding the fields to be executed and source object * @param result the result to complete, raw result * * @return a {@link FieldValueInfo} */ protected FieldValueInfo completeValueForList(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object result) { Iterable<Object> resultIterable = toIterable(executionContext, parameters, result); try { resultIterable = parameters.getNonNullFieldValidator().validate(parameters.getPath(), resultIterable); } catch (NonNullableFieldWasNullException e) { return FieldValueInfo.newFieldValueInfo(LIST).fieldValue(exceptionallyCompletedFuture(e)).build(); } if (resultIterable == null) { return FieldValueInfo.newFieldValueInfo(LIST).fieldValue(completedFuture(new ExecutionResultImpl(null, null))).build(); } return completeValueForList(executionContext, parameters, resultIterable); }
private boolean handleOnFieldValuesInfo(List<FieldValueInfo> fieldValueInfoList, CallStack callStack, int curLevel) { callStack.increaseHappenedOnFieldValueCalls(curLevel); int expectedStrategyCalls = 0; for (FieldValueInfo fieldValueInfo : fieldValueInfoList) { if (fieldValueInfo.getCompleteValueType() == FieldValueInfo.CompleteValueType.OBJECT) { expectedStrategyCalls++; } else if (fieldValueInfo.getCompleteValueType() == FieldValueInfo.CompleteValueType.LIST) { expectedStrategyCalls += getCountForList(fieldValueInfo); } } callStack.increaseExpectedStrategyCalls(curLevel + 1, expectedStrategyCalls); return dispatchIfNeeded(callStack, curLevel + 1); }
CompletableFuture<List<ExecutionResult>> resultsFuture = Async.each(fieldValueInfos, (item, i) -> item.getFieldValue()); overallResult.whenComplete(completeListCtx::onCompleted); return FieldValueInfo.newFieldValueInfo(LIST) .fieldValue(overallResult) .fieldValueInfos(fieldValueInfos)
return FieldValueInfo.newFieldValueInfo(NULL).fieldValue(fieldValue).build(); } else if (isList(fieldType)) { return completeValueForList(executionContext, parameters, result); } else if (fieldType instanceof GraphQLScalarType) { fieldValue = completeValueForScalar(executionContext, parameters, (GraphQLScalarType) fieldType, result); return FieldValueInfo.newFieldValueInfo(SCALAR).fieldValue(fieldValue).build(); } else if (fieldType instanceof GraphQLEnumType) { fieldValue = completeValueForEnum(executionContext, parameters, (GraphQLEnumType) fieldType, result); return FieldValueInfo.newFieldValueInfo(ENUM).fieldValue(fieldValue).build(); return FieldValueInfo.newFieldValueInfo(OBJECT).fieldValue(fieldValue).build();
private boolean handleOnFieldValuesInfo(List<FieldValueInfo> fieldValueInfoList, CallStack callStack, int curLevel) { callStack.increaseHappenedOnFieldValueCalls(curLevel); int expectedStrategyCalls = 0; for (FieldValueInfo fieldValueInfo : fieldValueInfoList) { if (fieldValueInfo.getCompleteValueType() == FieldValueInfo.CompleteValueType.OBJECT) { expectedStrategyCalls++; } else if (fieldValueInfo.getCompleteValueType() == FieldValueInfo.CompleteValueType.LIST) { expectedStrategyCalls += getCountForList(fieldValueInfo); } } callStack.increaseExpectedStrategyCalls(curLevel + 1, expectedStrategyCalls); return dispatchIfNeeded(callStack, curLevel + 1); }
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())); }
private int getCountForList(FieldValueInfo fieldValueInfo) { int result = 0; for (FieldValueInfo cvi : fieldValueInfo.getFieldValueInfos()) { if (cvi.getCompleteValueType() == FieldValueInfo.CompleteValueType.OBJECT) { result++; } else if (cvi.getCompleteValueType() == FieldValueInfo.CompleteValueType.LIST) { result += getCountForList(cvi); } } return result; }
/** * Called to complete a list of value for a field based on a list type. This iterates the values and calls * {@link #completeValue(ExecutionContext, ExecutionStrategyParameters)} for each value. * * @param executionContext contains the top level execution parameters * @param parameters contains the parameters holding the fields to be executed and source object * @param result the result to complete, raw result * * @return a {@link FieldValueInfo} */ protected FieldValueInfo completeValueForList(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object result) { Iterable<Object> resultIterable = toIterable(executionContext, parameters, result); try { resultIterable = parameters.getNonNullFieldValidator().validate(parameters.getPath(), resultIterable); } catch (NonNullableFieldWasNullException e) { return FieldValueInfo.newFieldValueInfo(LIST).fieldValue(exceptionallyCompletedFuture(e)).build(); } if (resultIterable == null) { return FieldValueInfo.newFieldValueInfo(LIST).fieldValue(completedFuture(new ExecutionResultImpl(null, null))).build(); } return completeValueForList(executionContext, parameters, resultIterable); }
public FieldValueInfo build() { return new FieldValueInfo(completeValueType, executionResultFuture, listInfos); } }
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)); }
return FieldValueInfo.newFieldValueInfo(NULL).fieldValue(fieldValue).build(); } else if (isList(fieldType)) { return completeValueForList(executionContext, parameters, result); } else if (fieldType instanceof GraphQLScalarType) { fieldValue = completeValueForScalar(executionContext, parameters, (GraphQLScalarType) fieldType, result); return FieldValueInfo.newFieldValueInfo(SCALAR).fieldValue(fieldValue).build(); } else if (fieldType instanceof GraphQLEnumType) { fieldValue = completeValueForEnum(executionContext, parameters, (GraphQLEnumType) fieldType, result); return FieldValueInfo.newFieldValueInfo(ENUM).fieldValue(fieldValue).build(); return FieldValueInfo.newFieldValueInfo(OBJECT).fieldValue(fieldValue).build();
@SuppressWarnings("FutureReturnValueIgnored") private Supplier<CompletableFuture<ExecutionResult>> deferredExecutionResult(ExecutionContext executionContext, ExecutionStrategyParameters parameters) { return () -> { GraphQLFieldDefinition fieldDef = getFieldDef(executionContext, parameters, parameters.getField().getSingleField()); GraphQLObjectType fieldContainer = (GraphQLObjectType) parameters.getExecutionStepInfo().getUnwrappedNonNullType(); Instrumentation instrumentation = executionContext.getInstrumentation(); DeferredFieldInstrumentationContext fieldCtx = instrumentation.beginDeferredField( new InstrumentationDeferredFieldParameters(executionContext, parameters, fieldDef, createExecutionStepInfo(executionContext, parameters, fieldDef, fieldContainer)) ); CompletableFuture<ExecutionResult> result = new CompletableFuture<>(); fieldCtx.onDispatched(result); CompletableFuture<FieldValueInfo> fieldValueInfoFuture = resolveFieldWithInfo(executionContext, parameters); fieldValueInfoFuture.whenComplete((fieldValueInfo, throwable) -> { fieldCtx.onFieldValueInfo(fieldValueInfo); CompletableFuture<ExecutionResult> execResultFuture = fieldValueInfo.getFieldValue(); execResultFuture = execResultFuture.whenComplete(fieldCtx::onCompleted); Async.copyResults(execResultFuture, result); }); return result; }; } }
CompletableFuture<ExecutionResult> executionResultFuture = fieldValueInfo.getFieldValue(); ctxCompleteField.onDispatched(executionResultFuture); executionResultFuture.whenComplete(ctxCompleteField::onCompleted);
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)); }
@SuppressWarnings("FutureReturnValueIgnored") private Supplier<CompletableFuture<ExecutionResult>> deferredExecutionResult(ExecutionContext executionContext, ExecutionStrategyParameters parameters) { return () -> { GraphQLFieldDefinition fieldDef = getFieldDef(executionContext, parameters, parameters.getField().getSingleField()); GraphQLObjectType fieldContainer = (GraphQLObjectType) parameters.getExecutionStepInfo().getUnwrappedNonNullType(); Instrumentation instrumentation = executionContext.getInstrumentation(); DeferredFieldInstrumentationContext fieldCtx = instrumentation.beginDeferredField( new InstrumentationDeferredFieldParameters(executionContext, parameters, fieldDef, createExecutionStepInfo(executionContext, parameters, fieldDef, fieldContainer)) ); CompletableFuture<ExecutionResult> result = new CompletableFuture<>(); fieldCtx.onDispatched(result); CompletableFuture<FieldValueInfo> fieldValueInfoFuture = resolveFieldWithInfo(executionContext, parameters); fieldValueInfoFuture.whenComplete((fieldValueInfo, throwable) -> { fieldCtx.onFieldValueInfo(fieldValueInfo); CompletableFuture<ExecutionResult> execResultFuture = fieldValueInfo.getFieldValue(); execResultFuture = execResultFuture.whenComplete(fieldCtx::onCompleted); Async.copyResults(execResultFuture, result); }); return result; }; } }
return completeValue(executionContext, newParameters).getFieldValue(); } else { return super.resolveField(executionContext, parameters);