Search in sources :

Example 6 with GraphQLOutputType

use of graphql.schema.GraphQLOutputType in project graphql-java by graphql-java.

the class ScalarLeafs method checkField.

@Override
public void checkField(Field field) {
    GraphQLOutputType type = getValidationContext().getOutputType();
    if (type == null)
        return;
    if (schemaUtil.isLeafType(type)) {
        if (field.getSelectionSet() != null) {
            String message = String.format("Sub selection not allowed on leaf type %s of field %s", type.getName(), field.getName());
            addError(ValidationErrorType.SubSelectionNotAllowed, field.getSourceLocation(), message);
        }
    } else {
        if (field.getSelectionSet() == null) {
            String message = String.format("Sub selection required for type %s of field %s", type.getName(), field.getName());
            addError(ValidationErrorType.SubSelectionRequired, field.getSourceLocation(), message);
        }
    }
}
Also used : GraphQLOutputType(graphql.schema.GraphQLOutputType)

Example 7 with GraphQLOutputType

use of graphql.schema.GraphQLOutputType in project graphql-java by graphql-java.

the class EchoingWiringFactory method fakeObjectValue.

private static Object fakeObjectValue(GraphQLObjectType fieldType) {
    Map<String, Object> map = new LinkedHashMap<>();
    fieldType.getFieldDefinitions().forEach(fldDef -> {
        GraphQLOutputType innerFieldType = fldDef.getType();
        Object obj = null;
        if (innerFieldType instanceof GraphQLObjectType) {
            obj = fakeObjectValue((GraphQLObjectType) innerFieldType);
        } else if (innerFieldType instanceof GraphQLScalarType) {
            obj = fakeScalarValue(fldDef.getName(), (GraphQLScalarType) innerFieldType);
        }
        map.put(fldDef.getName(), obj);
    });
    return map;
}
Also used : GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLObjectType(graphql.schema.GraphQLObjectType) LinkedHashMap(java.util.LinkedHashMap) GraphQLScalarType(graphql.schema.GraphQLScalarType)

Example 8 with GraphQLOutputType

use of graphql.schema.GraphQLOutputType in project graphql-java by graphql-java.

the class BatchedExecutionStrategy method handleList.

@SuppressWarnings("unchecked")
private List<ExecutionNode> handleList(ExecutionContext executionContext, Map<String, Object> argumentValues, FetchedValues fetchedValues, String fieldName, List<Field> fields, ExecutionTypeInfo typeInfo) {
    GraphQLList listType = (GraphQLList) typeInfo.getType();
    List<FetchedValue> flattenedValues = new ArrayList<>();
    for (FetchedValue value : fetchedValues.getValues()) {
        MapOrList mapOrList = value.getParentResult();
        if (value.getValue() == null) {
            mapOrList.putOrAdd(fieldName, null);
            continue;
        }
        MapOrList listResult = mapOrList.createAndPutList(fieldName);
        for (Object rawValue : toIterable(value.getValue())) {
            rawValue = unboxPossibleOptional(rawValue);
            flattenedValues.add(new FetchedValue(listResult, rawValue));
        }
    }
    GraphQLOutputType innerSubType = (GraphQLOutputType) listType.getWrappedType();
    ExecutionTypeInfo newTypeInfo = typeInfo.treatAs(innerSubType);
    FetchedValues flattenedFetchedValues = new FetchedValues(flattenedValues, newTypeInfo, fetchedValues.getPath());
    return completeValues(executionContext, flattenedFetchedValues, newTypeInfo, fieldName, fields, argumentValues);
}
Also used : GraphQLList(graphql.schema.GraphQLList) GraphQLOutputType(graphql.schema.GraphQLOutputType) ExecutionTypeInfo(graphql.execution.ExecutionTypeInfo) ArrayList(java.util.ArrayList)

Example 9 with GraphQLOutputType

use of graphql.schema.GraphQLOutputType in project admin-console-beta by connexta.

the class GraphQLTransformOutput method fieldToGraphQLOutputType.

@SuppressWarnings("squid:S00112")
public GraphQLOutputType fieldToGraphQLOutputType(Field field) {
    if (outputTypeProvider.isTypePresent(field.getFieldType())) {
        return outputTypeProvider.getType(field.getFieldType());
    }
    GraphQLOutputType type = null;
    if (field instanceof ObjectField) {
        type = fieldToGraphQLObjectType((ObjectField) field);
    } else if (field instanceof EnumField) {
        type = transformEnum.enumFieldToGraphQLEnumType((EnumField) field);
    } else if (field instanceof ListField) {
        try {
            type = new GraphQLList(fieldToGraphQLOutputType(((ListField<Field>) field).createListEntry()));
        } catch (Exception e) {
            throw new RuntimeException("Unable to build field list content type for output type: " + field.getFieldName());
        }
    } else if (field instanceof ScalarField) {
        type = transformScalar.resolveScalarType((ScalarField) field);
    }
    if (type == null) {
        throw new RuntimeException("Error transforming output field to GraphQLOutputType. Unknown field base type: " + field.getClass());
    }
    outputTypeProvider.addType(field.getFieldType(), type);
    return type;
}
Also used : GraphQLList(graphql.schema.GraphQLList) EnumField(org.codice.ddf.admin.api.fields.EnumField) Field(org.codice.ddf.admin.api.Field) ObjectField(org.codice.ddf.admin.api.fields.ObjectField) EnumField(org.codice.ddf.admin.api.fields.EnumField) ListField(org.codice.ddf.admin.api.fields.ListField) ScalarField(org.codice.ddf.admin.api.fields.ScalarField) FunctionField(org.codice.ddf.admin.api.fields.FunctionField) GraphQLOutputType(graphql.schema.GraphQLOutputType) ScalarField(org.codice.ddf.admin.api.fields.ScalarField) ObjectField(org.codice.ddf.admin.api.fields.ObjectField) ListField(org.codice.ddf.admin.api.fields.ListField) FunctionDataFetcherException(org.codice.ddf.graphql.FunctionDataFetcherException)

Example 10 with GraphQLOutputType

use of graphql.schema.GraphQLOutputType 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)

Aggregations

GraphQLOutputType (graphql.schema.GraphQLOutputType)14 GraphQLObjectType (graphql.schema.GraphQLObjectType)5 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)4 GraphQLList (graphql.schema.GraphQLList)4 GraphQLInterfaceType (graphql.schema.GraphQLInterfaceType)3 GraphQLScalarType (graphql.schema.GraphQLScalarType)3 GraphQLUnionType (graphql.schema.GraphQLUnionType)3 ArrayList (java.util.ArrayList)3 Instrumentation (graphql.execution.instrumentation.Instrumentation)2 InstrumentationFieldFetchParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters)2 Introspection (graphql.introspection.Introspection)2 Field (graphql.language.Field)2 TypeName (graphql.language.TypeName)2 DataFetcher (graphql.schema.DataFetcher)2 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)2 DataFetchingEnvironmentBuilder.newDataFetchingEnvironment (graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment)2 DataFetchingFieldSelectionSet (graphql.schema.DataFetchingFieldSelectionSet)2 GraphQLEnumType (graphql.schema.GraphQLEnumType)2 GraphQLSchema (graphql.schema.GraphQLSchema)2 GraphQLType (graphql.schema.GraphQLType)2