use of graphql.schema.GraphQLNonNull in project graphql-java by graphql-java.
the class ProvidedNonNullArguments method checkDirective.
@Override
public void checkDirective(Directive directive, List<Node> ancestors) {
GraphQLDirective graphQLDirective = getValidationContext().getDirective();
if (graphQLDirective == null)
return;
Map<String, Argument> argumentMap = argumentMap(directive.getArguments());
for (GraphQLArgument graphQLArgument : graphQLDirective.getArguments()) {
Argument argument = argumentMap.get(graphQLArgument.getName());
if (argument == null && (graphQLArgument.getType() instanceof GraphQLNonNull)) {
String message = String.format("Missing directive argument %s", graphQLArgument.getName());
addError(ValidationErrorType.MissingDirectiveArgument, directive.getSourceLocation(), message);
}
}
}
use of graphql.schema.GraphQLNonNull in project graphql-java by graphql-java.
the class ProvidedNonNullArguments method checkField.
@Override
public void checkField(Field field) {
GraphQLFieldDefinition fieldDef = getValidationContext().getFieldDef();
if (fieldDef == null)
return;
Map<String, Argument> argumentMap = argumentMap(field.getArguments());
for (GraphQLArgument graphQLArgument : fieldDef.getArguments()) {
Argument argument = argumentMap.get(graphQLArgument.getName());
if (argument == null && (graphQLArgument.getType() instanceof GraphQLNonNull)) {
String message = String.format("Missing field argument %s", graphQLArgument.getName());
addError(ValidationErrorType.MissingFieldArgument, field.getSourceLocation(), message);
}
}
}
use of graphql.schema.GraphQLNonNull in project graphql-java by graphql-java.
the class ObjectsImplementInterfaces method isCompatible.
/**
* @return {@code true} if the specified objectType satisfies the constraintType.
*/
boolean isCompatible(GraphQLOutputType constraintType, GraphQLOutputType objectType) {
if (isSameType(constraintType, objectType)) {
return true;
} else if (constraintType instanceof GraphQLUnionType) {
return objectIsMemberOfUnion((GraphQLUnionType) constraintType, objectType);
} else if (constraintType instanceof GraphQLInterfaceType && objectType instanceof GraphQLObjectType) {
return objectImplementsInterface((GraphQLInterfaceType) constraintType, (GraphQLObjectType) objectType);
} else if (constraintType instanceof GraphQLList && objectType instanceof GraphQLList) {
GraphQLOutputType wrappedConstraintType = (GraphQLOutputType) ((GraphQLList) constraintType).getWrappedType();
GraphQLOutputType wrappedObjectType = (GraphQLOutputType) ((GraphQLList) objectType).getWrappedType();
return isCompatible(wrappedConstraintType, wrappedObjectType);
} else if (objectType instanceof GraphQLNonNull) {
GraphQLOutputType nullableConstraint;
if (constraintType instanceof GraphQLNonNull) {
nullableConstraint = (GraphQLOutputType) ((GraphQLNonNull) constraintType).getWrappedType();
} else {
nullableConstraint = constraintType;
}
GraphQLOutputType nullableObjectType = (GraphQLOutputType) ((GraphQLNonNull) objectType).getWrappedType();
return isCompatible(nullableConstraint, nullableObjectType);
} else {
return false;
}
}
use of graphql.schema.GraphQLNonNull in project graphql-java by graphql-java.
the class SchemaGeneratorHelper method buildValue.
public Object buildValue(Value value, GraphQLType requiredType) {
Object result = null;
if (requiredType instanceof GraphQLNonNull) {
requiredType = ((GraphQLNonNull) requiredType).getWrappedType();
}
if (requiredType instanceof GraphQLScalarType) {
result = parseLiteral(value, (GraphQLScalarType) requiredType);
} else if (value instanceof EnumValue && requiredType instanceof GraphQLEnumType) {
result = ((EnumValue) value).getName();
} else if (value instanceof ArrayValue && requiredType instanceof GraphQLList) {
ArrayValue arrayValue = (ArrayValue) value;
GraphQLType wrappedType = ((GraphQLList) requiredType).getWrappedType();
result = arrayValue.getValues().stream().map(item -> this.buildValue(item, wrappedType)).collect(Collectors.toList());
} else if (value instanceof ObjectValue && requiredType instanceof GraphQLInputObjectType) {
result = buildObjectValue((ObjectValue) value, (GraphQLInputObjectType) requiredType);
} else if (value != null && !(value instanceof NullValue)) {
Assert.assertShouldNeverHappen("cannot build value of %s from %s", requiredType.getName(), String.valueOf(value));
}
return result;
}
use of graphql.schema.GraphQLNonNull in project graphql-java by graphql-java.
the class AstValueHelper method astFromValue.
/**
* Produces a GraphQL Value AST given a Java value.
*
* A GraphQL type must be provided, which will be used to interpret different
* Java values.
*
* <pre>
* | Value | GraphQL Value |
* | ------------- | -------------------- |
* | Object | Input Object |
* | Array | List |
* | Boolean | Boolean |
* | String | String / Enum Value |
* | Number | Int / Float |
* | Mixed | Enum Value |
* </pre>
*
* @param value - the java value to be converted into graphql ast
* @param type the graphql type of the object
*
* @return a grapql language ast {@link Value}
*/
public static Value astFromValue(Object value, GraphQLType type) {
if (value == null) {
return null;
}
if (type instanceof GraphQLNonNull) {
return handleNonNull(value, (GraphQLNonNull) type);
}
// the value is not an array, convert the value using the list's item type.
if (type instanceof GraphQLList) {
return handleList(value, (GraphQLList) type);
}
// in the JavaScript object according to the fields in the input type.
if (type instanceof GraphQLInputObjectType) {
return handleInputObject(value, (GraphQLInputObjectType) type);
}
if (!(type instanceof GraphQLScalarType || type instanceof GraphQLEnumType)) {
throw new AssertException("Must provide Input Type, cannot use: " + type.getClass());
}
// Since value is an internally represented value, it must be serialized
// to an externally represented value before converting into an AST.
final Object serialized = serialize(type, value);
if (isNullish(serialized)) {
return null;
}
// Others serialize based on their corresponding JavaScript scalar types.
if (serialized instanceof Boolean) {
return new BooleanValue((Boolean) serialized);
}
String stringValue = serialized.toString();
// numbers can be Int or Float values.
if (serialized instanceof Number) {
return handleNumber(stringValue);
}
if (serialized instanceof String) {
// Enum types use Enum literals.
if (type instanceof GraphQLEnumType) {
return new EnumValue(stringValue);
}
// ID types can use Int literals.
if (type == Scalars.GraphQLID && stringValue.matches("^[0-9]+$")) {
return new IntValue(new BigInteger(stringValue));
}
// String types are just strings but JSON'ised
return new StringValue(jsonStringify(stringValue));
}
throw new AssertException("'Cannot convert value to AST: " + serialized);
}
Aggregations