Search in sources :

Example 1 with InstrumentationFieldCompleteParameters

use of graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters 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;
}
Also used : GraphQLList(graphql.schema.GraphQLList) InstrumentationFieldCompleteParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters) ArrayList(java.util.ArrayList) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionResultImpl(graphql.ExecutionResultImpl) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) GraphQLList(graphql.schema.GraphQLList)

Example 2 with InstrumentationFieldCompleteParameters

use of graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters 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;
}
Also used : InstrumentationFieldCompleteParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) Field(graphql.language.Field) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) GraphQLObjectType(graphql.schema.GraphQLObjectType)

Aggregations

ExecutionResult (graphql.ExecutionResult)2 Instrumentation (graphql.execution.instrumentation.Instrumentation)2 InstrumentationFieldCompleteParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters)2 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)2 ExecutionResultImpl (graphql.ExecutionResultImpl)1 Field (graphql.language.Field)1 GraphQLList (graphql.schema.GraphQLList)1 GraphQLObjectType (graphql.schema.GraphQLObjectType)1 GraphqlFieldVisibility (graphql.schema.visibility.GraphqlFieldVisibility)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Collectors.toList (java.util.stream.Collectors.toList)1