graphql.Scalars Maven / Gradle / Ivy
package graphql;
import graphql.language.BooleanValue;
import graphql.language.FloatValue;
import graphql.language.IntValue;
import graphql.language.StringValue;
import graphql.schema.Coercing;
import graphql.schema.GraphQLScalarType;
import java.math.BigDecimal;
import java.math.BigInteger;
public class Scalars {
private static final BigInteger LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);
private static final BigInteger LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE);
private static final BigInteger INT_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
private static final BigInteger INT_MIN = BigInteger.valueOf(Integer.MIN_VALUE);
private static final BigInteger BYTE_MAX = BigInteger.valueOf(Byte.MAX_VALUE);
private static final BigInteger BYTE_MIN = BigInteger.valueOf(Byte.MIN_VALUE);
private static final BigInteger SHORT_MAX = BigInteger.valueOf(Short.MAX_VALUE);
private static final BigInteger SHORT_MIN = BigInteger.valueOf(Short.MIN_VALUE);
public static GraphQLScalarType GraphQLInt = new GraphQLScalarType("Int", "Built-in Int", new Coercing() {
@Override
public Object serialize(Object input) {
if (input instanceof String) {
return Integer.parseInt((String) input);
} else if (input instanceof Integer) {
return input;
} else {
return null;
}
}
@Override
public Object parseValue(Object input) {
return serialize(input);
}
@Override
public Object parseLiteral(Object input) {
if (!(input instanceof IntValue)) return null;
BigInteger value = ((IntValue) input).getValue();
if (value.compareTo(INT_MIN) == -1 || value.compareTo(INT_MAX) == 1) {
throw new GraphQLException("Int literal is too big or too small");
}
return value.intValue();
}
});
public static GraphQLScalarType GraphQLLong = new GraphQLScalarType("Long", "Long type", new Coercing() {
@Override
public Object serialize(Object input) {
if (input instanceof String) {
return Long.parseLong((String) input);
} else if (input instanceof Long) {
return input;
} else if (input instanceof Integer) {
return ((Integer) input).longValue();
} else {
return null;
}
}
@Override
public Object parseValue(Object input) {
return serialize(input);
}
@Override
public Object parseLiteral(Object input) {
if (input instanceof StringValue) {
return Long.parseLong(((StringValue) input).getValue());
} else if (input instanceof IntValue) {
BigInteger value = ((IntValue) input).getValue();
// Check if out of bounds.
if (value.compareTo(LONG_MIN) < 0 || value.compareTo(LONG_MAX) > 0) {
throw new GraphQLException("Int literal is too big or too small for a long, would cause overflow");
}
return value.longValue();
}
return null;
}
});
public static GraphQLScalarType GraphQLFloat = new GraphQLScalarType("Float", "Built-in Float", new Coercing() {
@Override
public Double serialize(Object input) {
if (input instanceof String) {
return Double.parseDouble((String) input);
} else if (input instanceof Double) {
return (Double) input;
} else if (input instanceof Float) {
return (double) (Float) input;
} else if (input instanceof Integer) {
return (double) (Integer) input;
} else {
return null;
}
}
@Override
public Object parseValue(Object input) {
return serialize(input);
}
@Override
public Object parseLiteral(Object input) {
if (input instanceof IntValue) {
return ((IntValue) input).getValue().doubleValue();
} else if (input instanceof FloatValue) {
return ((FloatValue) input).getValue().doubleValue();
} else {
return null;
}
}
});
public static GraphQLScalarType GraphQLString = new GraphQLScalarType("String", "Built-in String", new Coercing() {
@Override
public Object serialize(Object input) {
return input == null ? null : input.toString();
}
@Override
public Object parseValue(Object input) {
return serialize(input);
}
@Override
public Object parseLiteral(Object input) {
if (!(input instanceof StringValue)) return null;
return ((StringValue) input).getValue();
}
});
public static GraphQLScalarType GraphQLBoolean = new GraphQLScalarType("Boolean", "Built-in Boolean", new Coercing() {
@Override
public Object serialize(Object input) {
if (input instanceof Boolean) {
return input;
} else if (input instanceof Integer) {
return (Integer) input > 0;
} else if (input instanceof String) {
return Boolean.parseBoolean((String) input);
} else {
return null;
}
}
@Override
public Object parseValue(Object input) {
return serialize(input);
}
@Override
public Object parseLiteral(Object input) {
if (!(input instanceof BooleanValue)) return null;
return ((BooleanValue) input).isValue();
}
});
public static GraphQLScalarType GraphQLID = new GraphQLScalarType("ID", "Built-in ID", new Coercing() {
@Override
public Object serialize(Object input) {
if (input instanceof String) {
return input;
}
return null;
}
@Override
public Object parseValue(Object input) {
return serialize(input);
}
@Override
public Object parseLiteral(Object input) {
if (!(input instanceof StringValue)) return null;
return ((StringValue) input).getValue();
}
});
public static GraphQLScalarType GraphQLBigInteger = new GraphQLScalarType("BigInteger", "Built-in java.math.BigInteger", new Coercing() {
@Override
public BigInteger serialize(Object input) {
if (input instanceof BigInteger) {
return (BigInteger) input;
} else if (input instanceof String) {
return new BigInteger((String) input);
} else if (input instanceof Integer) {
return BigInteger.valueOf((Integer) input);
} else if (input instanceof Long) {
return BigInteger.valueOf((Long) input);
} else {
return null;
}
}
@Override
public BigInteger parseValue(Object input) {
return serialize(input);
}
@Override
public BigInteger parseLiteral(Object input) {
if (input instanceof StringValue) {
return new BigInteger(((StringValue) input).getValue());
} else if (input instanceof IntValue) {
return ((IntValue) input).getValue();
}
return null;
}
});
public static GraphQLScalarType GraphQLBigDecimal = new GraphQLScalarType("BigDecimal", "Built-in java.math.BigDecimal", new Coercing() {
@Override
public BigDecimal serialize(Object input) {
if (input instanceof BigDecimal) {
return (BigDecimal) input;
} else if (input instanceof String) {
return new BigDecimal((String) input);
} else if (input instanceof Float) {
return BigDecimal.valueOf((Float) input);
} else if (input instanceof Double) {
return BigDecimal.valueOf((Double) input);
} else if (input instanceof Integer) {
return BigDecimal.valueOf((Integer) input);
} else if (input instanceof Long) {
return BigDecimal.valueOf((Long) input);
} else {
return null;
}
}
@Override
public BigDecimal parseValue(Object input) {
return serialize(input);
}
@Override
public BigDecimal parseLiteral(Object input) {
if (input instanceof StringValue) {
return new BigDecimal(((StringValue) input).getValue());
} else if (input instanceof IntValue) {
return new BigDecimal(((IntValue) input).getValue());
} else if (input instanceof FloatValue) {
return ((FloatValue) input).getValue();
}
return null;
}
});
public static GraphQLScalarType GraphQLByte = new GraphQLScalarType("Byte", "Built-in Byte as Int", new Coercing() {
@Override
public Byte serialize(Object input) {
if (input instanceof String) {
return Byte.parseByte((String) input);
} else if (input instanceof Byte) {
return (Byte) input;
} else {
return null;
}
}
@Override
public Byte parseValue(Object input) {
return serialize(input);
}
@Override
public Byte parseLiteral(Object input) {
if (!(input instanceof IntValue)) return null;
BigInteger value = ((IntValue) input).getValue();
if (value.compareTo(BYTE_MIN) < 0 || value.compareTo(BYTE_MAX) > 0) {
throw new GraphQLException("Int literal is too big or too small for a byte, would cause overflow");
}
return value.byteValue();
}
});
public static GraphQLScalarType GraphQLShort = new GraphQLScalarType("Short", "Built-in Short as Int", new Coercing() {
@Override
public Short serialize(Object input) {
if (input instanceof String) {
return Short.parseShort((String) input);
} else if (input instanceof Short) {
return (Short) input;
} else {
return null;
}
}
@Override
public Short parseValue(Object input) {
return serialize(input);
}
@Override
public Short parseLiteral(Object input) {
if (!(input instanceof IntValue)) return null;
BigInteger value = ((IntValue) input).getValue();
if (value.compareTo(SHORT_MIN) < 0 || value.compareTo(SHORT_MAX) > 0) {
throw new GraphQLException("Int literal is too big or too small for a short, would cause overflow");
}
return value.shortValue();
}
});
public static GraphQLScalarType GraphQLChar = new GraphQLScalarType("Char", "Built-in Char as Character", new Coercing() {
@Override
public Character serialize(Object input) {
if (input instanceof String) {
return ((String) input).length() != 1 ?
null : ((String) input).charAt(0);
} else if (input instanceof Character) {
return (Character) input;
} else {
return null;
}
}
@Override
public Character parseValue(Object input) {
return serialize(input);
}
@Override
public Character parseLiteral(Object input) {
if (!(input instanceof StringValue)) return null;
String value = ((StringValue) input).getValue();
if (value == null || value.length() != 1) return null;
return value.charAt(0);
}
});
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy