Search in sources :

Example 6 with GraphQLNonNull

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);
        }
    }
}
Also used : GraphQLArgument(graphql.schema.GraphQLArgument) Argument(graphql.language.Argument) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLArgument(graphql.schema.GraphQLArgument) GraphQLDirective(graphql.schema.GraphQLDirective)

Example 7 with GraphQLNonNull

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);
        }
    }
}
Also used : GraphQLArgument(graphql.schema.GraphQLArgument) Argument(graphql.language.Argument) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLArgument(graphql.schema.GraphQLArgument)

Example 8 with GraphQLNonNull

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;
    }
}
Also used : GraphQLList(graphql.schema.GraphQLList) GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLUnionType(graphql.schema.GraphQLUnionType) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType)

Example 9 with GraphQLNonNull

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;
}
Also used : GraphQLList(graphql.schema.GraphQLList) ObjectValue(graphql.language.ObjectValue) NullValue(graphql.language.NullValue) GraphQLEnumType(graphql.schema.GraphQLEnumType) EnumValue(graphql.language.EnumValue) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLType(graphql.schema.GraphQLType) ArrayValue(graphql.language.ArrayValue) GraphQLScalarType(graphql.schema.GraphQLScalarType)

Example 10 with GraphQLNonNull

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);
}
Also used : GraphQLList(graphql.schema.GraphQLList) AssertException(graphql.AssertException) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphQLScalarType(graphql.schema.GraphQLScalarType) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) BigInteger(java.math.BigInteger)

Aggregations

GraphQLNonNull (graphql.schema.GraphQLNonNull)11 GraphQLList (graphql.schema.GraphQLList)6 GraphQLInputObjectType (graphql.schema.GraphQLInputObjectType)4 GraphQLType (graphql.schema.GraphQLType)4 GraphQLArgument (graphql.schema.GraphQLArgument)3 GraphQLEnumType (graphql.schema.GraphQLEnumType)3 GraphQLObjectType (graphql.schema.GraphQLObjectType)3 Argument (graphql.language.Argument)2 GraphQLInterfaceType (graphql.schema.GraphQLInterfaceType)2 GraphQLScalarType (graphql.schema.GraphQLScalarType)2 Stack (java.util.Stack)2 AssertException (graphql.AssertException)1 GraphQLString (graphql.Scalars.GraphQLString)1 ArrayValue (graphql.language.ArrayValue)1 EnumValue (graphql.language.EnumValue)1 NullValue (graphql.language.NullValue)1 ObjectValue (graphql.language.ObjectValue)1 DataFetcher (graphql.schema.DataFetcher)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 GraphQLDirective (graphql.schema.GraphQLDirective)1