Search in sources :

Example 6 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 7 with InputValueWithState

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

the class VariableTypesMatchRule method checkVariable.

@Override
public void checkVariable(VariableReference variableReference) {
    VariableDefinition variableDefinition = variableDefinitionMap.get(variableReference.getName());
    if (variableDefinition == null) {
        return;
    }
    GraphQLType variableType = TypeFromAST.getTypeFromAST(getValidationContext().getSchema(), variableDefinition.getType());
    if (variableType == null) {
        return;
    }
    GraphQLInputType expectedType = getValidationContext().getInputType();
    Optional<InputValueWithState> schemaDefault = Optional.ofNullable(getValidationContext().getArgument()).map(v -> v.getArgumentDefaultValue());
    Value schemaDefaultValue = null;
    if (schemaDefault.isPresent() && schemaDefault.get().isLiteral()) {
        schemaDefaultValue = (Value) schemaDefault.get().getValue();
    } else if (schemaDefault.isPresent() && schemaDefault.get().isSet()) {
        schemaDefaultValue = ValuesResolver.valueToLiteral(schemaDefault.get(), expectedType);
    }
    if (expectedType == null) {
        // we must have a unknown variable say to not have a known type
        return;
    }
    if (!variablesTypesMatcher.doesVariableTypesMatch(variableType, variableDefinition.getDefaultValue(), expectedType) && !variablesTypesMatcher.doesVariableTypesMatch(variableType, schemaDefaultValue, expectedType)) {
        GraphQLType effectiveType = variablesTypesMatcher.effectiveType(variableType, variableDefinition.getDefaultValue());
        String message = String.format("Variable type '%s' doesn't match expected type '%s'", GraphQLTypeUtil.simplePrint(effectiveType), GraphQLTypeUtil.simplePrint(expectedType));
        addError(ValidationErrorType.VariableTypeMismatch, variableReference.getSourceLocation(), message);
    }
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) InputValueWithState(graphql.schema.InputValueWithState) VariableDefinition(graphql.language.VariableDefinition) GraphQLType(graphql.schema.GraphQLType) Value(graphql.language.Value)

Example 8 with InputValueWithState

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

the class SchemaPrinter method argsString.

String argsString(Class<? extends GraphQLSchemaElement> parent, List<GraphQLArgument> arguments) {
    boolean hasDescriptions = arguments.stream().anyMatch(this::hasDescription);
    String halfPrefix = hasDescriptions ? "  " : "";
    String prefix = hasDescriptions ? "    " : "";
    int count = 0;
    StringBuilder sb = new StringBuilder();
    Comparator<? super GraphQLSchemaElement> comparator = getComparator(parent, GraphQLArgument.class);
    arguments = arguments.stream().sorted(comparator).filter(options.getIncludeSchemaElement()).collect(toList());
    for (GraphQLArgument argument : arguments) {
        if (count == 0) {
            sb.append("(");
        } else {
            sb.append(", ");
        }
        if (hasDescriptions) {
            sb.append("\n");
        }
        sb.append(printComments(argument, prefix));
        sb.append(prefix).append(argument.getName()).append(": ").append(typeString(argument.getType()));
        if (argument.hasSetDefaultValue()) {
            InputValueWithState defaultValue = argument.getArgumentDefaultValue();
            sb.append(" = ");
            sb.append(printAst(defaultValue, argument.getType()));
        }
        DirectivesUtil.toAppliedDirectives(argument).stream().filter(options.getIncludeSchemaElement()).map(this::directiveString).filter(it -> !it.isEmpty()).forEach(directiveString -> sb.append(" ").append(directiveString));
        count++;
    }
    if (count > 0) {
        if (hasDescriptions) {
            sb.append("\n");
        }
        sb.append(halfPrefix).append(")");
    }
    return sb.toString();
}
Also used : InputValueWithState(graphql.schema.InputValueWithState) Arrays(java.util.Arrays) GraphqlTypeComparatorRegistry(graphql.schema.GraphqlTypeComparatorRegistry) GraphQLDirectiveContainer(graphql.schema.GraphQLDirectiveContainer) ValuesResolver(graphql.execution.ValuesResolver) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) InputValueDefinition(graphql.language.InputValueDefinition) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLUnionType(graphql.schema.GraphQLUnionType) GraphQLEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition) GraphQLAppliedDirective(graphql.schema.GraphQLAppliedDirective) InputValueWithState(graphql.schema.InputValueWithState) EnumTypeDefinition(graphql.language.EnumTypeDefinition) Description(graphql.language.Description) EnumValueDefinition(graphql.language.EnumValueDefinition) Map(java.util.Map) DefaultGraphqlTypeComparatorRegistry(graphql.schema.DefaultGraphqlTypeComparatorRegistry) GraphQLObjectType(graphql.schema.GraphQLObjectType) PrintWriter(java.io.PrintWriter) GraphQLDirective(graphql.schema.GraphQLDirective) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) GraphQLNamedOutputType(graphql.schema.GraphQLNamedOutputType) Predicate(java.util.function.Predicate) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) FieldDefinition(graphql.language.FieldDefinition) GraphQLInputType(graphql.schema.GraphQLInputType) TypeDefinition(graphql.language.TypeDefinition) GraphQLArgument(graphql.schema.GraphQLArgument) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) AstPrinter(graphql.language.AstPrinter) List(java.util.List) Stream(java.util.stream.Stream) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphqlTypeComparatorEnvironment(graphql.schema.GraphqlTypeComparatorEnvironment) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLNamedType(graphql.schema.GraphQLNamedType) GraphQLScalarType(graphql.schema.GraphQLScalarType) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) GraphQLType(graphql.schema.GraphQLType) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) UnionTypeDefinition(graphql.language.UnionTypeDefinition) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLTypeUtil(graphql.schema.GraphQLTypeUtil) DeprecatedDirective(graphql.Directives.DeprecatedDirective) GraphQLAppliedDirectiveArgument(graphql.schema.GraphQLAppliedDirectiveArgument) Assert(graphql.Assert) DirectivesUtil(graphql.DirectivesUtil) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) EscapeUtil.escapeJsonString(graphql.util.EscapeUtil.escapeJsonString) DEFAULT_FIELD_VISIBILITY(graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY) Optional.ofNullable(java.util.Optional.ofNullable) StringWriter(java.io.StringWriter) GraphQLOutputType(graphql.schema.GraphQLOutputType) Document(graphql.language.Document) Collectors.toList(java.util.stream.Collectors.toList) PublicApi(graphql.PublicApi) Comparator(java.util.Comparator) GraphQLArgument(graphql.schema.GraphQLArgument) EscapeUtil.escapeJsonString(graphql.util.EscapeUtil.escapeJsonString)

Example 9 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)

Example 10 with InputValueWithState

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

the class DefaultValuesAreValid method visitGraphQLArgument.

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

Aggregations

InputValueWithState (graphql.schema.InputValueWithState)11 GraphQLInputType (graphql.schema.GraphQLInputType)6 GraphQLSchema (graphql.schema.GraphQLSchema)6 GraphQLArgument (graphql.schema.GraphQLArgument)5 GraphQLType (graphql.schema.GraphQLType)5 LinkedHashMap (java.util.LinkedHashMap)5 PublicApi (graphql.PublicApi)3 ValuesResolver (graphql.execution.ValuesResolver)3 AstPrinter (graphql.language.AstPrinter)3 IntValue (graphql.language.IntValue)3 GraphQLDirective (graphql.schema.GraphQLDirective)3 GraphQLSchemaElement (graphql.schema.GraphQLSchemaElement)3 Assert (graphql.Assert)2 DeprecatedDirective (graphql.Directives.DeprecatedDirective)2 DirectivesUtil (graphql.DirectivesUtil)2 Value (graphql.language.Value)2 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)2 GraphQLInputObjectField (graphql.schema.GraphQLInputObjectField)2 GraphQLNamedType (graphql.schema.GraphQLNamedType)2 GraphQLObjectType (graphql.schema.GraphQLObjectType)2