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;
}
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);
}
}
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();
}
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));
}
}
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;
}
Aggregations