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