Search in sources :

Example 1 with InputValueWithState

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

the class ValuesResolver method externalValueToInternalValueForObject.

/**
 * performs validation
 */
private Object externalValueToInternalValueForObject(GraphqlFieldVisibility fieldVisibility, GraphQLInputObjectType inputObjectType, Map<String, Object> inputMap) throws NonNullableValueCoercedAsNullException, CoercingParseValueException {
    List<GraphQLInputObjectField> fieldDefinitions = fieldVisibility.getFieldDefinitions(inputObjectType);
    List<String> fieldNames = map(fieldDefinitions, GraphQLInputObjectField::getName);
    for (String providedFieldName : inputMap.keySet()) {
        if (!fieldNames.contains(providedFieldName)) {
            throw new InputMapDefinesTooManyFieldsException(inputObjectType, providedFieldName);
        }
    }
    Map<String, Object> coercedValues = new LinkedHashMap<>();
    for (GraphQLInputObjectField inputFieldDefinition : fieldDefinitions) {
        GraphQLInputType fieldType = inputFieldDefinition.getType();
        String fieldName = inputFieldDefinition.getName();
        InputValueWithState defaultValue = inputFieldDefinition.getInputFieldDefaultValue();
        boolean hasValue = inputMap.containsKey(fieldName);
        Object value;
        Object fieldValue = inputMap.getOrDefault(fieldName, null);
        value = fieldValue;
        if (!hasValue && inputFieldDefinition.hasSetDefaultValue()) {
            Object coercedDefaultValue = defaultValueToInternalValue(fieldVisibility, defaultValue, fieldType);
            coercedValues.put(fieldName, coercedDefaultValue);
        } else if (isNonNull(fieldType) && (!hasValue || value == null)) {
            throw new NonNullableValueCoercedAsNullException(fieldName, emptyList(), fieldType);
        } else if (hasValue) {
            if (value == null) {
                coercedValues.put(fieldName, null);
            } else {
                value = externalValueToInternalValue(fieldVisibility, fieldType, value);
                coercedValues.put(fieldName, value);
            }
        }
    }
    return coercedValues;
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) InputValueWithState(graphql.schema.InputValueWithState) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with InputValueWithState

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

the class ValuesResolver method getArgumentValuesImpl.

private Map<String, Object> getArgumentValuesImpl(GraphqlFieldVisibility fieldVisibility, List<GraphQLArgument> argumentTypes, List<Argument> arguments, Map<String, Object> coercedVariables) {
    if (argumentTypes.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<String, Object> coercedValues = new LinkedHashMap<>();
    Map<String, Argument> argumentMap = argumentMap(arguments);
    for (GraphQLArgument argumentDefinition : argumentTypes) {
        GraphQLInputType argumentType = argumentDefinition.getType();
        String argumentName = argumentDefinition.getName();
        Argument argument = argumentMap.get(argumentName);
        InputValueWithState defaultValue = argumentDefinition.getArgumentDefaultValue();
        boolean hasValue = argument != null;
        Object value;
        Value argumentValue = argument != null ? argument.getValue() : null;
        if (argumentValue instanceof VariableReference) {
            String variableName = ((VariableReference) argumentValue).getName();
            hasValue = coercedVariables.containsKey(variableName);
            value = coercedVariables.get(variableName);
        } else {
            value = argumentValue;
        }
        if (!hasValue && argumentDefinition.hasSetDefaultValue()) {
            Object coercedDefaultValue = defaultValueToInternalValue(fieldVisibility, defaultValue, argumentType);
            coercedValues.put(argumentName, coercedDefaultValue);
        } else if (isNonNull(argumentType) && (!hasValue || isNullValue(value))) {
            throw new NonNullableValueCoercedAsNullException(argumentDefinition);
        } else if (hasValue) {
            if (isNullValue(value)) {
                coercedValues.put(argumentName, value);
            } else if (argumentValue instanceof VariableReference) {
                coercedValues.put(argumentName, value);
            } else {
                value = literalToInternalValue(fieldVisibility, argumentType, argument.getValue(), coercedVariables);
                coercedValues.put(argumentName, value);
            }
        }
    }
    return coercedValues;
}
Also used : GraphQLArgument(graphql.schema.GraphQLArgument) Argument(graphql.language.Argument) VariableReference(graphql.language.VariableReference) GraphQLArgument(graphql.schema.GraphQLArgument) LinkedHashMap(java.util.LinkedHashMap) GraphQLInputType(graphql.schema.GraphQLInputType) InputValueWithState(graphql.schema.InputValueWithState) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) NormalizedInputValue(graphql.normalized.NormalizedInputValue) ArrayValue(graphql.language.ArrayValue) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) EnumValue(graphql.language.EnumValue) NullValue.newNullValue(graphql.language.NullValue.newNullValue) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) BooleanValue(graphql.language.BooleanValue)

Example 3 with InputValueWithState

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

the class IntrospectionWithDirectivesSupport method addAppliedDirectives.

private GraphQLObjectType addAppliedDirectives(GraphQLObjectType originalType, GraphQLCodeRegistry.Builder codeRegistry, GraphQLObjectType appliedDirectiveType, GraphQLObjectType directiveArgumentType) {
    GraphQLObjectType objectType = originalType.transform(bld -> bld.field(fld -> fld.name("appliedDirectives").type(nonNull(list(nonNull(appliedDirectiveType))))));
    DataFetcher<?> df = env -> {
        Object source = env.getSource();
        GraphQLSchema schema = env.getGraphQLSchema();
        if (source instanceof GraphQLDirectiveContainer) {
            GraphQLDirectiveContainer type = env.getSource();
            List<GraphQLAppliedDirective> appliedDirectives = DirectivesUtil.toAppliedDirectives(type);
            return filterAppliedDirectives(schema, false, type, appliedDirectives);
        }
        if (source instanceof GraphQLSchema) {
            List<GraphQLAppliedDirective> appliedDirectives = DirectivesUtil.toAppliedDirectives(schema.getSchemaAppliedDirectives(), schema.getSchemaDirectives());
            return filterAppliedDirectives(schema, true, null, appliedDirectives);
        }
        return assertShouldNeverHappen("What directive containing element have we not considered? - %s", originalType);
    };
    DataFetcher<?> argsDF = env -> {
        final GraphQLAppliedDirective directive = env.getSource();
        // we only show directive arguments that have values set on them
        return directive.getArguments().stream().filter(arg -> arg.getArgumentValue().isSet());
    };
    DataFetcher<?> argValueDF = env -> {
        final GraphQLAppliedDirectiveArgument argument = env.getSource();
        InputValueWithState value = argument.getArgumentValue();
        Node<?> literal = ValuesResolver.valueToLiteral(value, argument.getType());
        return AstPrinter.printAst(literal);
    };
    codeRegistry.dataFetcher(coordinates(objectType, "appliedDirectives"), df);
    codeRegistry.dataFetcher(coordinates(appliedDirectiveType, "args"), argsDF);
    codeRegistry.dataFetcher(coordinates(directiveArgumentType, "value"), argValueDF);
    return objectType;
}
Also used : GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) Node(graphql.language.Node) SchemaTransformer(graphql.schema.SchemaTransformer) GraphQLString(graphql.Scalars.GraphQLString) GraphQLDirectiveContainer(graphql.schema.GraphQLDirectiveContainer) TraversalControl(graphql.util.TraversalControl) ValuesResolver(graphql.execution.ValuesResolver) Introspection.__Type(graphql.introspection.Introspection.__Type) GraphQLType(graphql.schema.GraphQLType) TraverserContext(graphql.util.TraverserContext) GraphQLList.list(graphql.schema.GraphQLList.list) GraphQLAppliedDirective(graphql.schema.GraphQLAppliedDirective) InputValueWithState(graphql.schema.InputValueWithState) Introspection.__EnumValue(graphql.introspection.Introspection.__EnumValue) Assert.assertShouldNeverHappen(graphql.Assert.assertShouldNeverHappen) GraphQLNonNull.nonNull(graphql.schema.GraphQLNonNull.nonNull) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLAppliedDirectiveArgument(graphql.schema.GraphQLAppliedDirectiveArgument) DirectivesUtil(graphql.DirectivesUtil) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLDirective(graphql.schema.GraphQLDirective) ImmutableSet(com.google.common.collect.ImmutableSet) PublicSpi(graphql.PublicSpi) CONTINUE(graphql.util.TraversalControl.CONTINUE) Set(java.util.Set) GraphQLTypeVisitorStub(graphql.schema.GraphQLTypeVisitorStub) FieldCoordinates.coordinates(graphql.schema.FieldCoordinates.coordinates) Introspection.__Schema(graphql.introspection.Introspection.__Schema) AstPrinter(graphql.language.AstPrinter) Introspection.__InputValue(graphql.introspection.Introspection.__InputValue) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) PublicApi(graphql.PublicApi) Assert.assertNotNull(graphql.Assert.assertNotNull) GraphQLObjectType.newObject(graphql.schema.GraphQLObjectType.newObject) Introspection.__Field(graphql.introspection.Introspection.__Field) NotNull(org.jetbrains.annotations.NotNull) GraphQLDirectiveContainer(graphql.schema.GraphQLDirectiveContainer) InputValueWithState(graphql.schema.InputValueWithState) Node(graphql.language.Node) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLAppliedDirectiveArgument(graphql.schema.GraphQLAppliedDirectiveArgument) GraphQLObjectType.newObject(graphql.schema.GraphQLObjectType.newObject) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) GraphQLAppliedDirective(graphql.schema.GraphQLAppliedDirective) GraphQLSchema(graphql.schema.GraphQLSchema)

Example 4 with InputValueWithState

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

the class DefaultValuesAreValid method visitGraphQLInputObjectField.

@Override
public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField inputObjectField, TraverserContext<GraphQLSchemaElement> context) {
    if (!inputObjectField.hasSetDefaultValue()) {
        return TraversalControl.CONTINUE;
    }
    SchemaValidationErrorCollector errorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class);
    GraphQLSchema schema = context.getVarFromParents(GraphQLSchema.class);
    InputValueWithState defaultValue = inputObjectField.getInputFieldDefaultValue();
    boolean invalid = false;
    if (defaultValue.isLiteral() && !validationUtil.isValidLiteralValue((Value<?>) defaultValue.getValue(), inputObjectField.getType(), schema)) {
        invalid = true;
    } else if (defaultValue.isExternal() && !isValidExternalValue(schema, defaultValue.getValue(), inputObjectField.getType())) {
        invalid = true;
    }
    if (invalid) {
        String message = format("Invalid default value %s for type %s", defaultValue.getValue(), simplePrint(inputObjectField.getType()));
        errorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.InvalidDefaultValue, message));
    }
    return TraversalControl.CONTINUE;
}
Also used : InputValueWithState(graphql.schema.InputValueWithState) GraphQLSchema(graphql.schema.GraphQLSchema)

Example 5 with InputValueWithState

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

the class AppliedDirectiveArgumentsAreValid method checkArgument.

private void checkArgument(GraphQLDirective directive, GraphQLArgument argument, TraverserContext<GraphQLSchemaElement> context) {
    if (!argument.hasSetValue()) {
        return;
    }
    GraphQLSchema schema = context.getVarFromParents(GraphQLSchema.class);
    SchemaValidationErrorCollector errorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class);
    InputValueWithState argumentValue = argument.getArgumentValue();
    boolean invalid = false;
    if (argumentValue.isLiteral() && !validationUtil.isValidLiteralValue((Value<?>) argumentValue.getValue(), argument.getType(), schema)) {
        invalid = true;
    } else if (argumentValue.isExternal() && !isValidExternalValue(schema, argumentValue.getValue(), argument.getType())) {
        invalid = true;
    }
    if (invalid) {
        String message = format("Invalid argument '%s' for applied directive of name '%s'", argument.getName(), directive.getName());
        errorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.InvalidAppliedDirectiveArgument, message));
    }
}
Also used : InputValueWithState(graphql.schema.InputValueWithState) GraphQLSchema(graphql.schema.GraphQLSchema)

Aggregations

InputValueWithState (graphql.schema.InputValueWithState)8 GraphQLSchema (graphql.schema.GraphQLSchema)5 GraphQLInputType (graphql.schema.GraphQLInputType)4 DirectivesUtil (graphql.DirectivesUtil)2 PublicApi (graphql.PublicApi)2 ValuesResolver (graphql.execution.ValuesResolver)2 AstPrinter (graphql.language.AstPrinter)2 Value (graphql.language.Value)2 GraphQLAppliedDirective (graphql.schema.GraphQLAppliedDirective)2 GraphQLAppliedDirectiveArgument (graphql.schema.GraphQLAppliedDirectiveArgument)2 GraphQLArgument (graphql.schema.GraphQLArgument)2 GraphQLDirective (graphql.schema.GraphQLDirective)2 GraphQLDirectiveContainer (graphql.schema.GraphQLDirectiveContainer)2 GraphQLType (graphql.schema.GraphQLType)2 LinkedHashMap (java.util.LinkedHashMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Assert (graphql.Assert)1 Assert.assertNotNull (graphql.Assert.assertNotNull)1 Assert.assertShouldNeverHappen (graphql.Assert.assertShouldNeverHappen)1 DeprecatedDirective (graphql.Directives.DeprecatedDirective)1