Search in sources :

Example 1 with TrivialDataFetcher

use of graphql.TrivialDataFetcher in project graphql-java by graphql-java.

the class ExecutionStrategy method fetchField.

/**
 * Called to fetch a value for a field from the {@link DataFetcher} associated with the field
 * {@link GraphQLFieldDefinition}.
 * <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
 *
 * @return a promise to a fetched object
 *
 * @throws NonNullableFieldWasNullException in the future if a non null field resolves to a null value
 */
protected CompletableFuture<FetchedValue> fetchField(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
    MergedField field = parameters.getField();
    GraphQLObjectType parentType = (GraphQLObjectType) parameters.getExecutionStepInfo().getUnwrappedNonNullType();
    GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, field.getSingleField());
    GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
    GraphQLOutputType fieldType = fieldDef.getType();
    // if the DF (like PropertyDataFetcher) does not use the arguments of execution step info then dont build any
    Supplier<ExecutionStepInfo> executionStepInfo = FpKit.intraThreadMemoize(() -> createExecutionStepInfo(executionContext, parameters, fieldDef, parentType));
    Supplier<Map<String, Object>> argumentValues = () -> executionStepInfo.get().getArguments();
    Supplier<ExecutableNormalizedField> normalizedFieldSupplier = getNormalizedField(executionContext, parameters, executionStepInfo);
    // DataFetchingFieldSelectionSet and QueryDirectives is a supplier of sorts - eg a lazy pattern
    DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext.getGraphQLSchema(), fieldType, normalizedFieldSupplier);
    QueryDirectives queryDirectives = new QueryDirectivesImpl(field, executionContext.getGraphQLSchema(), executionContext.getVariables());
    DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext).source(parameters.getSource()).localContext(parameters.getLocalContext()).arguments(argumentValues).fieldDefinition(fieldDef).mergedField(parameters.getField()).fieldType(fieldType).executionStepInfo(executionStepInfo).parentType(parentType).selectionSet(fieldCollector).queryDirectives(queryDirectives).build();
    DataFetcher<?> dataFetcher = codeRegistry.getDataFetcher(parentType, fieldDef);
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationFieldFetchParameters instrumentationFieldFetchParams = new InstrumentationFieldFetchParameters(executionContext, environment, parameters, dataFetcher instanceof TrivialDataFetcher);
    InstrumentationContext<Object> fetchCtx = instrumentation.beginFieldFetch(instrumentationFieldFetchParams);
    CompletableFuture<Object> fetchedValue;
    dataFetcher = instrumentation.instrumentDataFetcher(dataFetcher, instrumentationFieldFetchParams);
    ExecutionId executionId = executionContext.getExecutionId();
    try {
        Object fetchedValueRaw = dataFetcher.get(environment);
        fetchedValue = Async.toCompletableFuture(fetchedValueRaw);
    } catch (Exception e) {
        if (logNotSafe.isDebugEnabled()) {
            logNotSafe.debug(String.format("'%s', field '%s' fetch threw exception", executionId, executionStepInfo.get().getPath()), e);
        }
        fetchedValue = new CompletableFuture<>();
        fetchedValue.completeExceptionally(e);
    }
    fetchCtx.onDispatched(fetchedValue);
    return fetchedValue.handle((result, exception) -> {
        fetchCtx.onCompleted(result, exception);
        if (exception != null) {
            return handleFetchingException(executionContext, environment, exception);
        } else {
            return CompletableFuture.completedFuture(result);
        }
    }).thenCompose(Function.identity()).thenApply(result -> unboxPossibleDataFetcherResult(executionContext, parameters, result));
}
Also used : GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Instrumentation(graphql.execution.instrumentation.Instrumentation) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionStepInfo.newExecutionStepInfo(graphql.execution.ExecutionStepInfo.newExecutionStepInfo) ExecutableNormalizedField(graphql.normalized.ExecutableNormalizedField) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) QueryDirectives(graphql.execution.directives.QueryDirectives) CoercingSerializeException(graphql.schema.CoercingSerializeException) CompletionException(java.util.concurrent.CompletionException) GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLObjectType(graphql.schema.GraphQLObjectType) QueryDirectivesImpl(graphql.execution.directives.QueryDirectivesImpl) InstrumentationFieldFetchParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters) TrivialDataFetcher(graphql.TrivialDataFetcher) Map(java.util.Map)

Aggregations

TrivialDataFetcher (graphql.TrivialDataFetcher)1 ExecutionStepInfo.newExecutionStepInfo (graphql.execution.ExecutionStepInfo.newExecutionStepInfo)1 QueryDirectives (graphql.execution.directives.QueryDirectives)1 QueryDirectivesImpl (graphql.execution.directives.QueryDirectivesImpl)1 Instrumentation (graphql.execution.instrumentation.Instrumentation)1 InstrumentationFieldFetchParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters)1 ExecutableNormalizedField (graphql.normalized.ExecutableNormalizedField)1 CoercingSerializeException (graphql.schema.CoercingSerializeException)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 DataFetchingEnvironmentImpl.newDataFetchingEnvironment (graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment)1 DataFetchingFieldSelectionSet (graphql.schema.DataFetchingFieldSelectionSet)1 GraphQLCodeRegistry (graphql.schema.GraphQLCodeRegistry)1 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)1 GraphQLObjectType (graphql.schema.GraphQLObjectType)1 GraphQLOutputType (graphql.schema.GraphQLOutputType)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1