Search in sources :

Example 6 with GraphqlFieldVisibility

use of graphql.schema.visibility.GraphqlFieldVisibility 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 fetched object
 *
 * @throws NonNullableFieldWasNullException if a non null field resolves to a null value
 */
protected CompletableFuture<Object> fetchField(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
    Field field = parameters.getField().get(0);
    GraphQLObjectType parentType = parameters.getTypeInfo().castType(GraphQLObjectType.class);
    GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, field);
    GraphqlFieldVisibility fieldVisibility = executionContext.getGraphQLSchema().getFieldVisibility();
    Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldVisibility, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables());
    GraphQLOutputType fieldType = fieldDef.getType();
    DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext, fieldType, parameters.getField());
    ExecutionTypeInfo fieldTypeInfo = fieldTypeInfo(parameters, fieldDef);
    DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext).source(parameters.getSource()).arguments(argumentValues).fieldDefinition(fieldDef).fields(parameters.getField()).fieldType(fieldType).fieldTypeInfo(fieldTypeInfo).parentType(parentType).selectionSet(fieldCollector).build();
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationFieldFetchParameters instrumentationFieldFetchParams = new InstrumentationFieldFetchParameters(executionContext, fieldDef, environment);
    InstrumentationContext<Object> fetchCtx = instrumentation.beginFieldFetch(instrumentationFieldFetchParams);
    CompletableFuture<Object> fetchedValue;
    DataFetcher dataFetcher = fieldDef.getDataFetcher();
    dataFetcher = instrumentation.instrumentDataFetcher(dataFetcher, instrumentationFieldFetchParams);
    ExecutionId executionId = executionContext.getExecutionId();
    try {
        log.debug("'{}' fetching field '{}' using data fetcher '{}'...", executionId, fieldTypeInfo.getPath(), dataFetcher.getClass().getName());
        Object fetchedValueRaw = dataFetcher.get(environment);
        log.debug("'{}' field '{}' fetch returned '{}'", executionId, fieldTypeInfo.getPath(), fetchedValueRaw == null ? "null" : fetchedValueRaw.getClass().getName());
        fetchedValue = Async.toCompletableFuture(fetchedValueRaw);
    } catch (Exception e) {
        log.debug(String.format("'%s', field '%s' fetch threw exception", executionId, fieldTypeInfo.getPath()), e);
        fetchedValue = new CompletableFuture<>();
        fetchedValue.completeExceptionally(e);
    }
    fetchCtx.onDispatched(fetchedValue);
    return fetchedValue.handle((result, exception) -> {
        fetchCtx.onCompleted(result, exception);
        if (exception != null) {
            handleFetchingException(executionContext, parameters, field, fieldDef, argumentValues, environment, exception);
            return null;
        } else {
            return result;
        }
    }).thenApply(result -> unboxPossibleDataFetcherResult(executionContext, parameters, result)).thenApply(this::unboxPossibleOptional);
}
Also used : IntStream(java.util.stream.IntStream) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentBuilder.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment) Array(java.lang.reflect.Array) GraphQLScalarType(graphql.schema.GraphQLScalarType) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) LoggerFactory(org.slf4j.LoggerFactory) OptionalDouble(java.util.OptionalDouble) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) GraphQLUnionType(graphql.schema.GraphQLUnionType) CompletableFuture(java.util.concurrent.CompletableFuture) GraphQLType(graphql.schema.GraphQLType) OptionalInt(java.util.OptionalInt) ArrayList(java.util.ArrayList) ExecutionResult(graphql.ExecutionResult) Introspection(graphql.introspection.Introspection) OptionalLong(java.util.OptionalLong) InstrumentationFieldFetchParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters) Map(java.util.Map) InstrumentationFieldCompleteParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) ExecutionTypeInfo.newTypeInfo(graphql.execution.ExecutionTypeInfo.newTypeInfo) ExecutionResultImpl(graphql.ExecutionResultImpl) TypeMismatchError(graphql.TypeMismatchError) SerializationError(graphql.SerializationError) TypeResolutionEnvironment(graphql.TypeResolutionEnvironment) GraphQLObjectType(graphql.schema.GraphQLObjectType) CoercingSerializeException(graphql.schema.CoercingSerializeException) Logger(org.slf4j.Logger) PublicSpi(graphql.PublicSpi) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) GraphQLOutputType(graphql.schema.GraphQLOutputType) CompletionException(java.util.concurrent.CompletionException) Field(graphql.language.Field) DataFetchingFieldSelectionSetImpl(graphql.schema.DataFetchingFieldSelectionSetImpl) InstrumentationContext(graphql.execution.instrumentation.InstrumentationContext) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) GraphQLList(graphql.schema.GraphQLList) Instrumentation(graphql.execution.instrumentation.Instrumentation) DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) Optional(java.util.Optional) InstrumentationFieldParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldParameters) GraphQLEnumType(graphql.schema.GraphQLEnumType) FieldCollectorParameters.newParameters(graphql.execution.FieldCollectorParameters.newParameters) DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Instrumentation(graphql.execution.instrumentation.Instrumentation) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentBuilder.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment) CoercingSerializeException(graphql.schema.CoercingSerializeException) CompletionException(java.util.concurrent.CompletionException) Field(graphql.language.Field) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) GraphQLOutputType(graphql.schema.GraphQLOutputType) CompletableFuture(java.util.concurrent.CompletableFuture) GraphQLObjectType(graphql.schema.GraphQLObjectType) InstrumentationFieldFetchParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters) DataFetcher(graphql.schema.DataFetcher)

Example 7 with GraphqlFieldVisibility

use of graphql.schema.visibility.GraphqlFieldVisibility in project graphql-java by graphql-java.

the class BatchedExecutionStrategy method fetchData.

private CompletableFuture<FetchedValues> fetchData(ExecutionContext executionContext, ExecutionStrategyParameters parameters, String fieldName, ExecutionNode node, GraphQLFieldDefinition fieldDef) {
    GraphQLObjectType parentType = node.getType();
    List<Field> fields = node.getFields().get(fieldName);
    List<MapOrList> parentResults = node.getParentResults();
    GraphqlFieldVisibility fieldVisibility = executionContext.getGraphQLSchema().getFieldVisibility();
    Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldVisibility, fieldDef.getArguments(), fields.get(0).getArguments(), executionContext.getVariables());
    GraphQLOutputType fieldType = fieldDef.getType();
    DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext, fieldType, fields);
    DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext).source(node.getSources()).arguments(argumentValues).fieldDefinition(fieldDef).fields(fields).fieldType(fieldDef.getType()).fieldTypeInfo(parameters.getTypeInfo()).parentType(parentType).selectionSet(fieldCollector).build();
    Instrumentation instrumentation = executionContext.getInstrumentation();
    InstrumentationFieldFetchParameters instrumentationFieldFetchParameters = new InstrumentationFieldFetchParameters(executionContext, fieldDef, environment);
    InstrumentationContext<Object> fetchCtx = instrumentation.beginFieldFetch(instrumentationFieldFetchParameters);
    CompletableFuture<Object> fetchedValue;
    try {
        DataFetcher<?> dataFetcher = instrumentation.instrumentDataFetcher(getDataFetcher(fieldDef), instrumentationFieldFetchParameters);
        Object fetchedValueRaw = dataFetcher.get(environment);
        fetchedValue = Async.toCompletableFuture(fetchedValueRaw);
    } catch (Exception e) {
        fetchedValue = new CompletableFuture<>();
        fetchedValue.completeExceptionally(e);
    }
    return fetchedValue.thenApply((result) -> assertResult(parentResults, result)).whenComplete(fetchCtx::onCompleted).handle(handleResult(executionContext, parameters, parentResults, fields, fieldDef, argumentValues, environment));
}
Also used : DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) Instrumentation(graphql.execution.instrumentation.Instrumentation) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentBuilder.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment) CompletionException(java.util.concurrent.CompletionException) Field(graphql.language.Field) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) GraphQLOutputType(graphql.schema.GraphQLOutputType) CompletableFuture(java.util.concurrent.CompletableFuture) GraphQLObjectType(graphql.schema.GraphQLObjectType) InstrumentationFieldFetchParameters(graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters)

Example 8 with GraphqlFieldVisibility

use of graphql.schema.visibility.GraphqlFieldVisibility in project graphql-java by graphql-java.

the class ValidationUtil method isValidLiteralValue.

private boolean isValidLiteralValue(Value value, GraphQLInputObjectType type, GraphQLSchema schema) {
    if (!(value instanceof ObjectValue)) {
        handleNotObjectError(value, type);
        return false;
    }
    GraphqlFieldVisibility fieldVisibility = schema.getFieldVisibility();
    ObjectValue objectValue = (ObjectValue) value;
    Map<String, ObjectField> objectFieldMap = fieldMap(objectValue);
    Set<String> missingFields = getMissingFields(type, objectFieldMap, fieldVisibility);
    if (!missingFields.isEmpty()) {
        handleMissingFieldsError(value, type, missingFields);
        return false;
    }
    for (ObjectField objectField : objectValue.getObjectFields()) {
        GraphQLInputObjectField inputObjectField = fieldVisibility.getFieldDefinition(type, objectField.getName());
        if (inputObjectField == null) {
            handleExtraFieldError(value, type, objectField);
            return false;
        }
        if (!isValidLiteralValue(objectField.getValue(), inputObjectField.getType(), schema)) {
            handleFieldNotValidError(objectField, type);
            return false;
        }
    }
    return true;
}
Also used : GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) ObjectValue(graphql.language.ObjectValue) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) ObjectField(graphql.language.ObjectField)

Aggregations

GraphqlFieldVisibility (graphql.schema.visibility.GraphqlFieldVisibility)8 Instrumentation (graphql.execution.instrumentation.Instrumentation)4 Field (graphql.language.Field)4 GraphQLObjectType (graphql.schema.GraphQLObjectType)4 ExecutionResult (graphql.ExecutionResult)3 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)3 GraphQLType (graphql.schema.GraphQLType)3 InstrumentationFieldCompleteParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters)2 InstrumentationFieldFetchParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters)2 InstrumentationFieldParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldParameters)2 ObjectValue (graphql.language.ObjectValue)2 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)2 DataFetchingEnvironmentBuilder.newDataFetchingEnvironment (graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment)2 DataFetchingFieldSelectionSet (graphql.schema.DataFetchingFieldSelectionSet)2 GraphQLList (graphql.schema.GraphQLList)2 GraphQLOutputType (graphql.schema.GraphQLOutputType)2 GraphQLSchema (graphql.schema.GraphQLSchema)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2