use of graphql.ExecutionResult in project graphql-java by graphql-java.
the class BatchedExecutionStrategy method resolveField.
private CompletableFuture<List<ExecutionNode>> resolveField(ExecutionContext executionContext, ExecutionStrategyParameters parameters, String fieldName, ExecutionNode node) {
GraphQLObjectType parentType = node.getType();
List<Field> fields = node.getFields().get(fieldName);
GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, fields.get(0));
Instrumentation instrumentation = executionContext.getInstrumentation();
ExecutionTypeInfo typeInfo = parameters.getTypeInfo();
InstrumentationContext<ExecutionResult> fieldCtx = instrumentation.beginField(new InstrumentationFieldParameters(executionContext, fieldDef, typeInfo));
CompletableFuture<FetchedValues> fetchedData = fetchData(executionContext, parameters, fieldName, node, fieldDef);
CompletableFuture<List<ExecutionNode>> result = fetchedData.thenApply((fetchedValues) -> {
GraphqlFieldVisibility fieldVisibility = executionContext.getGraphQLSchema().getFieldVisibility();
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldVisibility, fieldDef.getArguments(), fields.get(0).getArguments(), executionContext.getVariables());
return completeValues(executionContext, fetchedValues, typeInfo, fieldName, fields, argumentValues);
});
fieldCtx.onDispatched(null);
result.whenComplete((nodes, throwable) -> fieldCtx.onCompleted(null, throwable));
return result;
}
use of graphql.ExecutionResult in project graphql-java by graphql-java.
the class BatchedExecutionStrategy method execute.
@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
InstrumentationContext<ExecutionResult> executionStrategyCtx = executionContext.getInstrumentation().beginExecutionStrategy(new InstrumentationExecutionStrategyParameters(executionContext, parameters));
GraphQLObjectType type = parameters.getTypeInfo().castType(GraphQLObjectType.class);
ExecutionNode root = new ExecutionNode(type, parameters.getTypeInfo(), parameters.getFields(), singletonList(MapOrList.createMap(new LinkedHashMap<>())), Collections.singletonList(parameters.getSource()));
Queue<ExecutionNode> nodes = new ArrayDeque<>();
CompletableFuture<ExecutionResult> result = new CompletableFuture<>();
executeImpl(executionContext, parameters, root, root, nodes, root.getFields().keySet().iterator(), result);
executionStrategyCtx.onDispatched(result);
result.whenComplete(executionStrategyCtx::onCompleted);
return result;
}
use of graphql.ExecutionResult in project graphql-java by graphql-java.
the class Execution method executeOperation.
private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext executionContext, InstrumentationExecutionParameters instrumentationExecutionParameters, Object root, OperationDefinition operationDefinition) {
InstrumentationExecuteOperationParameters instrumentationParams = new InstrumentationExecuteOperationParameters(executionContext);
InstrumentationContext<ExecutionResult> executeOperationCtx = instrumentation.beginExecuteOperation(instrumentationParams);
OperationDefinition.Operation operation = operationDefinition.getOperation();
GraphQLObjectType operationRootType;
try {
operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operationDefinition);
} catch (RuntimeException rte) {
if (rte instanceof GraphQLError) {
ExecutionResult executionResult = new ExecutionResultImpl(Collections.singletonList((GraphQLError) rte));
CompletableFuture<ExecutionResult> resultCompletableFuture = completedFuture(executionResult);
executeOperationCtx.onDispatched(resultCompletableFuture);
executeOperationCtx.onCompleted(executionResult, rte);
return resultCompletableFuture;
}
throw rte;
}
FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters().schema(executionContext.getGraphQLSchema()).objectType(operationRootType).fragments(executionContext.getFragmentsByName()).variables(executionContext.getVariables()).build();
Map<String, List<Field>> fields = fieldCollector.collectFields(collectorParameters, operationDefinition.getSelectionSet());
ExecutionPath path = ExecutionPath.rootPath();
ExecutionTypeInfo typeInfo = newTypeInfo().type(operationRootType).path(path).build();
NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, typeInfo);
ExecutionStrategyParameters parameters = newParameters().typeInfo(typeInfo).source(root).fields(fields).nonNullFieldValidator(nonNullableFieldValidator).path(path).build();
CompletableFuture<ExecutionResult> result;
try {
ExecutionStrategy executionStrategy;
if (operation == OperationDefinition.Operation.MUTATION) {
executionStrategy = mutationStrategy;
} else if (operation == SUBSCRIPTION) {
executionStrategy = subscriptionStrategy;
} else {
executionStrategy = queryStrategy;
}
log.debug("Executing '{}' query operation: '{}' using '{}' execution strategy", executionContext.getExecutionId(), operation, executionStrategy.getClass().getName());
result = executionStrategy.execute(executionContext, parameters);
} catch (NonNullableFieldWasNullException e) {
// this means it was non null types all the way from an offending non null type
// up to the root object type and there was a a null value some where.
//
// The spec says we should return null for the data in this case
//
// http://facebook.github.io/graphql/#sec-Errors-and-Non-Nullability
//
result = completedFuture(new ExecutionResultImpl(null, executionContext.getErrors()));
}
// note this happens NOW - not when the result completes
executeOperationCtx.onDispatched(result);
result = result.whenComplete(executeOperationCtx::onCompleted);
return deferSupport(executionContext, result);
}
use of graphql.ExecutionResult in project graphql-java by graphql-java.
the class ExecutionStrategy method completeValueForList.
/**
* 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 iterableValues the values to complete, can't be null
*
* @return an {@link ExecutionResult}
*/
protected CompletableFuture<ExecutionResult> completeValueForList(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Iterable<Object> iterableValues) {
ExecutionTypeInfo typeInfo = parameters.getTypeInfo();
GraphQLList fieldType = typeInfo.castType(GraphQLList.class);
GraphQLFieldDefinition fieldDef = parameters.getTypeInfo().getFieldDefinition();
InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, fieldDef, fieldTypeInfo(parameters, fieldDef), iterableValues);
Instrumentation instrumentation = executionContext.getInstrumentation();
InstrumentationContext<ExecutionResult> completeListCtx = instrumentation.beginFieldListComplete(instrumentationParams);
CompletableFuture<List<ExecutionResult>> resultsFuture = Async.each(iterableValues, (item, index) -> {
ExecutionPath indexedPath = parameters.getPath().segment(index);
ExecutionTypeInfo wrappedTypeInfo = ExecutionTypeInfo.newTypeInfo().parentInfo(typeInfo).type(fieldType.getWrappedType()).path(indexedPath).fieldDefinition(fieldDef).build();
NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, wrappedTypeInfo);
ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.typeInfo(wrappedTypeInfo).nonNullFieldValidator(nonNullableFieldValidator).path(indexedPath).source(item));
return completeValue(executionContext, newParameters);
});
CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
completeListCtx.onDispatched(overallResult);
resultsFuture.whenComplete((results, exception) -> {
if (exception != null) {
ExecutionResult executionResult = handleNonNullException(executionContext, overallResult, exception);
completeListCtx.onCompleted(executionResult, exception);
return;
}
List<Object> completedResults = new ArrayList<>();
for (ExecutionResult completedValue : results) {
completedResults.add(completedValue.getData());
}
ExecutionResultImpl executionResult = new ExecutionResultImpl(completedResults, null);
overallResult.complete(executionResult);
});
overallResult.whenComplete(completeListCtx::onCompleted);
return overallResult;
}
use of graphql.ExecutionResult in project graphql-java by graphql-java.
the class ExecutionStrategy method completeField.
/**
* Called to complete a field based on the type of the field.
* <p>
* If the field is a scalar type, then it will be coerced and returned. However if the field type is an complex object type, then
* the execution strategy will be called recursively again to execute the fields of that type before returning.
* <p>
* Graphql fragments mean that for any give logical field can have one or more {@link Field} values associated with it
* in the query, hence the fieldList. However the first entry is representative of the field for most purposes.
*
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
* @param fetchedValue the fetched raw value
*
* @return an {@link ExecutionResult}
*
* @throws NonNullableFieldWasNullException if a non null field resolves to a null value
*/
protected CompletableFuture<ExecutionResult> completeField(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Object fetchedValue) {
Field field = parameters.getField().get(0);
GraphQLObjectType parentType = parameters.getTypeInfo().castType(GraphQLObjectType.class);
GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, field);
ExecutionTypeInfo fieldTypeInfo = fieldTypeInfo(parameters, fieldDef);
Instrumentation instrumentation = executionContext.getInstrumentation();
InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, fieldDef, fieldTypeInfo, fetchedValue);
InstrumentationContext<ExecutionResult> ctxCompleteField = instrumentation.beginFieldComplete(instrumentationParams);
GraphqlFieldVisibility fieldVisibility = executionContext.getGraphQLSchema().getFieldVisibility();
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldVisibility, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables());
NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, fieldTypeInfo);
ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.typeInfo(fieldTypeInfo).arguments(argumentValues).source(fetchedValue).nonNullFieldValidator(nonNullableFieldValidator));
log.debug("'{}' completing field '{}'...", executionContext.getExecutionId(), fieldTypeInfo.getPath());
CompletableFuture<ExecutionResult> cf = completeValue(executionContext, newParameters);
ctxCompleteField.onDispatched(cf);
cf.whenComplete(ctxCompleteField::onCompleted);
return cf;
}
Aggregations