use of graphql.ExecutionResultImpl 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.ExecutionResultImpl in project graphql-java by graphql-java.
the class ExecutionStrategy method completeValueForEnum.
/**
* Called to turn an object into a enum value according to the {@link GraphQLEnumType} by asking that enum type to coerce the object into a valid value
*
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
* @param enumType the type of the enum
* @param result the result to be coerced
*
* @return an {@link ExecutionResult}
*/
protected CompletableFuture<ExecutionResult> completeValueForEnum(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLEnumType enumType, Object result) {
Object serialized;
try {
serialized = enumType.getCoercing().serialize(result);
} catch (CoercingSerializeException e) {
serialized = handleCoercionProblem(executionContext, parameters, e);
}
serialized = parameters.getNonNullFieldValidator().validate(parameters.getPath(), serialized);
return completedFuture(new ExecutionResultImpl(serialized, null));
}
use of graphql.ExecutionResultImpl 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.ExecutionResultImpl in project graphql-java by graphql-java.
the class ExecutionStrategy method completeValueForScalar.
/**
* Called to turn an object into a scalar value according to the {@link GraphQLScalarType} by asking that scalar type to coerce the object
* into a valid value
*
* @param executionContext contains the top level execution parameters
* @param parameters contains the parameters holding the fields to be executed and source object
* @param scalarType the type of the scalar
* @param result the result to be coerced
*
* @return an {@link ExecutionResult}
*/
protected CompletableFuture<ExecutionResult> completeValueForScalar(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLScalarType scalarType, Object result) {
Object serialized;
try {
serialized = scalarType.getCoercing().serialize(result);
} catch (CoercingSerializeException e) {
serialized = handleCoercionProblem(executionContext, parameters, e);
}
// 6.6.1 http://facebook.github.io/graphql/#sec-Field-entries
if (serialized instanceof Double && ((Double) serialized).isNaN()) {
serialized = null;
}
serialized = parameters.getNonNullFieldValidator().validate(parameters.getPath(), serialized);
return completedFuture(new ExecutionResultImpl(serialized, null));
}
use of graphql.ExecutionResultImpl in project graphql-java by graphql-java.
the class ExecutionStrategy method handleNonNullException.
protected ExecutionResult handleNonNullException(ExecutionContext executionContext, CompletableFuture<ExecutionResult> result, Throwable e) {
ExecutionResult executionResult = null;
if (e instanceof NonNullableFieldWasNullException) {
assertNonNullFieldPrecondition((NonNullableFieldWasNullException) e, result);
if (!result.isDone()) {
executionResult = new ExecutionResultImpl(null, executionContext.getErrors());
result.complete(executionResult);
}
} else if (e instanceof CompletionException && e.getCause() instanceof NonNullableFieldWasNullException) {
assertNonNullFieldPrecondition((NonNullableFieldWasNullException) e.getCause(), result);
if (!result.isDone()) {
executionResult = new ExecutionResultImpl(null, executionContext.getErrors());
result.complete(executionResult);
}
} else {
result.completeExceptionally(e);
}
return executionResult;
}
Aggregations