Imagine we create a scalar type Currency which basically parses a custom string to a specific type, if we take a look at graphql-java we could come up with something like this:
public static GraphQLScalarType GraphQLCurrency = new GraphQLScalarType("Currency", "Built-in Currency", new Coercing<Currency, Currency>() {
@Override
public Currency serialize(Object input) {
//
}
@Override
public Currency parseValue(Object input) {
//
}
@Override
public Currency parseLiteral(Object input) {
//
}
});
But at the moment there is no way to add that information to the type DSL. It would be nice to be able to do it, something like:
DSL.scalar('Currency', 'built in Currency') {
/**
* Called to convert a result of a DataFetcher to a valid runtime value.
*
* @param input is never null
* @return null if not possible/invalid
*/
serialize { input ->
//
}
/**
* Called to resolve a input from a variable.
* Null if not possible.
*
* @param input is never null
* @return null if not possible/invalid
*/
parseValue { input ->
//
}
/**
* Called to convert a AST node
*
* @param input is never null
* @return null if not possible/invalid
*/
parseLiteral { input ->
//
}
}
Basically the Coercing interface defines three methods and they're grouped in those which return an output and those that return something used as input:
serialize (O): Should be used to return a valid value to the client. Normally we use libraries that serializes the outcome from the fetcher, so it would make sense to return a full type, and let Jackson for instance to serialize a currency instance to a valid JSON object
parseValue (I): Parses a value coming from a query input
parseLiteral (I): Parses a value coming from a query literal
I'm not sure how the last two are triggered, I need to do some tests.
Imagine we create a scalar type
Currency
which basically parses a custom string to a specific type, if we take a look at graphql-java we could come up with something like this:But at the moment there is no way to add that information to the type DSL. It would be nice to be able to do it, something like:
Basically the Coercing interface defines three methods and they're grouped in those which return an output and those that return something used as input:
serialize (O)
: Should be used to return a valid value to the client. Normally we use libraries that serializes the outcome from the fetcher, so it would make sense to return a full type, and let Jackson for instance to serialize a currency instance to a valid JSON objectparseValue (I)
: Parses a value coming from a query inputparseLiteral (I)
: Parses a value coming from a query literalI'm not sure how the last two are triggered, I need to do some tests.