@PublicSpi public interface Coercing<I,O>
GraphQLScalarType
s to parse and serialise object values.
There are two major responsibilities, result coercion and input coercion.
Result coercion is taking a value from a Java object and coercing it into the constraints of the scalar type. For example imagine a DateTime scalar, the result coercion would need to take an object and turn it into a ISO date or throw an exception if it cant.
Input coercion is taking a value that came in from requests variables or hard coded query literals and coercing them into a Java object value that is acceptable to the scalar type. Again using the DateTime example, the input coercion would try to parse an ISO date time object or throw an exception if it cant. See http://facebook.github.io/graphql/#sec-Scalars
Modifier and Type | Method and Description |
---|---|
I |
parseLiteral(java.lang.Object input)
Called during query validation to convert a query input AST node into a Java object acceptable for the scalar type.
|
default I |
parseLiteral(java.lang.Object input,
java.util.Map<java.lang.String,java.lang.Object> variables)
Called during query execution to convert a query input AST node into a Java object acceptable for the scalar type.
|
I |
parseValue(java.lang.Object input)
Called to resolve an input from a query variable into a Java object acceptable for the scalar type.
|
O |
serialize(java.lang.Object dataFetcherResult)
Called to convert a Java object result of a DataFetcher to a valid runtime value for the scalar type.
|
O serialize(java.lang.Object dataFetcherResult) throws CoercingSerializeException
Note : Throw CoercingSerializeException
if there is fundamental
problem during serialisation, don't return null to indicate failure.
Note : You should not allow RuntimeException
s to come out of your serialize method, but rather
catch them and fire them as CoercingSerializeException
instead as per the method contract.
dataFetcherResult
- is never nullCoercingSerializeException
- if value input can't be serializedI parseValue(java.lang.Object input) throws CoercingParseValueException
Note : You should not allow RuntimeException
s to come out of your parseValue method, but rather
catch them and fire them as CoercingParseValueException
instead as per the method contract.
input
- is never nullCoercingParseValueException
- if value input can't be parsedI parseLiteral(java.lang.Object input) throws CoercingParseLiteralException
Value
.
Note : You should not allow RuntimeException
s to come out of your parseLiteral method, but rather
catch them and fire them as CoercingParseLiteralException
instead as per the method contract.
input
- is never nullCoercingParseLiteralException
- if input literal can't be parseddefault I parseLiteral(java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> variables) throws CoercingParseLiteralException
Value
.
Note : You should not allow RuntimeException
s to come out of your parseLiteral method, but rather
catch them and fire them as CoercingParseLiteralException
instead as per the method contract.
Many scalar types don't need to implement this method because they don't take AST VariableReference
objects and convert them into actual values. But for those scalar types that want to do this, then this
method should be implemented.
input
- is never nullvariables
- the resolved variables passed to the queryCoercingParseLiteralException
- if input literal can't be parsed