Search in sources :

Example 6 with GraphQLInputObjectType

use of graphql.schema.GraphQLInputObjectType 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)

Example 7 with GraphQLInputObjectType

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

the class Relay method mutation.

public GraphQLFieldDefinition mutation(String name, String fieldName, List<GraphQLInputObjectField> inputFields, List<GraphQLFieldDefinition> outputFields, DataFetcher dataFetcher) {
    GraphQLInputObjectType inputObjectType = newInputObject().name(name + "Input").fields(inputFields).build();
    GraphQLObjectType outputType = newObject().name(name + "Payload").fields(outputFields).build();
    return newFieldDefinition().name(fieldName).type(outputType).argument(newArgument().name("input").type(new GraphQLNonNull(inputObjectType))).dataFetcher(dataFetcher).build();
}
Also used : GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull)

Aggregations

GraphQLInputObjectType (graphql.schema.GraphQLInputObjectType)7 GraphQLNonNull (graphql.schema.GraphQLNonNull)4 GraphQLEnumType (graphql.schema.GraphQLEnumType)2 GraphQLInputObjectField (graphql.schema.GraphQLInputObjectField)2 GraphQLInputType (graphql.schema.GraphQLInputType)2 GraphQLList (graphql.schema.GraphQLList)2 GraphQLObjectType (graphql.schema.GraphQLObjectType)2 GraphQLScalarType (graphql.schema.GraphQLScalarType)2 GraphQLType (graphql.schema.GraphQLType)2 AssertException (graphql.AssertException)1 ArrayValue (graphql.language.ArrayValue)1 EnumValue (graphql.language.EnumValue)1 NullValue (graphql.language.NullValue)1 ObjectValue (graphql.language.ObjectValue)1 GraphQLArgument (graphql.schema.GraphQLArgument)1 GraphQLSchema (graphql.schema.GraphQLSchema)1 GraphQLUnmodifiedType (graphql.schema.GraphQLUnmodifiedType)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1