Interface Coercing<I,O>
-
- All Known Implementing Classes:
GraphqlBooleanCoercing
,GraphqlFloatCoercing
,GraphqlIDCoercing
,GraphqlIntCoercing
,GraphqlStringCoercing
@PublicSpi public interface Coercing<I,O>
The Coercing interface is used byGraphQLScalarType
s to parse and serialize 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 made out of three different methods
parseLiteral(Object)
which converts an literal Ast into an internal input value,parseValue(Object)
which converts an external input value into an internal one andvalueToLiteral(Object)
which is a translation between an external input value into a literal.
The relationship between these three methods is as follows: It is required that every valid external input values forparseValue(Object)
is also valid forvalueToLiteral(Object)
and vice versa. Furthermore the literals returned byvalueToLiteral(Object)
are required to be valid forparseLiteral(Object)
.
-
-
Method Summary
All Methods Instance Methods Default Methods Deprecated Methods Modifier and Type Method Description default I
parseLiteral(@NotNull Value<?> input, @NotNull CoercedVariables variables, @NotNull GraphQLContext graphQLContext, @NotNull java.util.Locale locale)
Called during query execution to convert a query input AST node into a Java object acceptable for the scalar type.default I
parseLiteral(@NotNull java.lang.Object input)
Deprecated.default I
parseLiteral(java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> variables)
Deprecated.default I
parseValue(@NotNull java.lang.Object input)
Deprecated.default I
parseValue(@NotNull java.lang.Object input, @NotNull GraphQLContext graphQLContext, @NotNull java.util.Locale locale)
Called to resolve an input from a query variable into a Java object acceptable for the scalar type.default O
serialize(@NotNull java.lang.Object dataFetcherResult)
Deprecated.default O
serialize(@NotNull java.lang.Object dataFetcherResult, @NotNull GraphQLContext graphQLContext, @NotNull java.util.Locale locale)
Called to convert a Java object result of a DataFetcher to a valid runtime value for the scalar type.default @NotNull Value
valueToLiteral(@NotNull java.lang.Object input)
Deprecated.default @NotNull Value<?>
valueToLiteral(@NotNull java.lang.Object input, @NotNull GraphQLContext graphQLContext, @NotNull java.util.Locale locale)
Converts an external input value to a literal (Ast Value).
-
-
-
Method Detail
-
serialize
@Deprecated(since="2022-08-22") @Nullable default O serialize(@NotNull @NotNull java.lang.Object dataFetcherResult) throws CoercingSerializeException
Deprecated.This is deprecated and you should implementserialize(Object, GraphQLContext, Locale)
insteadCalled to convert a Java object result of a DataFetcher to a valid runtime value for the scalar type.
Note : Throw
CoercingSerializeException
if there is fundamental problem during serialization, 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 asCoercingSerializeException
instead as per the method contract.- Parameters:
dataFetcherResult
- is never null- Returns:
- a serialized value which may be null.
- Throws:
CoercingSerializeException
- if value input can't be serialized
-
serialize
@Nullable default O serialize(@NotNull @NotNull java.lang.Object dataFetcherResult, @NotNull @NotNull GraphQLContext graphQLContext, @NotNull @NotNull java.util.Locale locale) throws CoercingSerializeException
Called to convert a Java object result of a DataFetcher to a valid runtime value for the scalar type.Note : Throw
CoercingSerializeException
if there is fundamental problem during serialization, 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 asCoercingSerializeException
instead as per the method contract.- Parameters:
dataFetcherResult
- is never nullgraphQLContext
- the graphql context in placelocale
- the locale to use- Returns:
- a serialized value which may be null.
- Throws:
CoercingSerializeException
- if value input can't be serialized
-
parseValue
@Deprecated(since="2022-08-22") @Nullable default I parseValue(@NotNull @NotNull java.lang.Object input) throws CoercingParseValueException
Deprecated.This is deprecated and you should implementparseValue(Object, GraphQLContext, Locale)
insteadCalled to resolve an input from a query variable into a Java object acceptable for the scalar type.
Note : You should not allow
RuntimeException
s to come out of your parseValue method, but rather catch them and fire them asCoercingParseValueException
instead as per the method contract.Note : if input is explicit/raw value null, input coercion will return null before this method is called
- Parameters:
input
- is never null- Returns:
- a parsed value which may be null
- Throws:
CoercingParseValueException
- if value input can't be parsed
-
parseValue
@Nullable default I parseValue(@NotNull @NotNull java.lang.Object input, @NotNull @NotNull GraphQLContext graphQLContext, @NotNull @NotNull java.util.Locale locale) throws CoercingParseValueException
Called to resolve an input from a query variable into a Java object acceptable for the scalar type.Note : You should not allow
RuntimeException
s to come out of your parseValue method, but rather catch them and fire them asCoercingParseValueException
instead as per the method contract. Note : if input is explicit/raw value null, input coercion will return null before this method is called- Parameters:
input
- is never nullgraphQLContext
- the graphql context in placelocale
- the locale to use- Returns:
- a parsed value which may be null
- Throws:
CoercingParseValueException
- if value input can't be parsed
-
parseLiteral
@Deprecated(since="2022-08-22") @Nullable default I parseLiteral(@NotNull @NotNull java.lang.Object input) throws CoercingParseLiteralException
Deprecated.This is deprecated and you should implementparseLiteral(Value, CoercedVariables, GraphQLContext, Locale)
insteadCalled during query validation to convert a query input AST node into a Java object acceptable for the scalar type. The input object will be an instance of
Value
.Note : You should not allow
RuntimeException
s to come out of your parseLiteral method, but rather catch them and fire them asCoercingParseLiteralException
instead as per the method contract.Note : if input is literal
NullValue
, input coercion will return null before this method is called- Parameters:
input
- is never null- Returns:
- a parsed value which may be null
- Throws:
CoercingParseLiteralException
- if input literal can't be parsed
-
parseLiteral
@Deprecated(since="2022-08-22") @Nullable default I parseLiteral(java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> variables) throws CoercingParseLiteralException
Deprecated.This is deprecated and you should implementparseLiteral(Value, CoercedVariables, GraphQLContext, Locale)
insteadCalled during query execution to convert a query input AST node into a Java object acceptable for the scalar type. The input object will be an instance of
Value
.Note : You should not allow
RuntimeException
s to come out of your parseLiteral method, but rather catch them and fire them asCoercingParseLiteralException
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. Note : if input is literalNullValue
, input coercion will return null before this method is called- Parameters:
input
- is never nullvariables
- the resolved variables passed to the query- Returns:
- a parsed value which may be null
- Throws:
CoercingParseLiteralException
- if input literal can't be parsed
-
parseLiteral
@Nullable default I parseLiteral(@NotNull @NotNull Value<?> input, @NotNull @NotNull CoercedVariables variables, @NotNull @NotNull GraphQLContext graphQLContext, @NotNull @NotNull java.util.Locale locale) throws CoercingParseLiteralException
Called during query execution to convert a query input AST node into a Java object acceptable for the scalar type. The input object will be an instance ofValue
.Note : You should not allow
RuntimeException
s to come out of your parseLiteral method, but rather catch them and fire them asCoercingParseLiteralException
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. Note : if input is literalNullValue
, input coercion will return null before this method is called- Parameters:
input
- is never nullvariables
- the resolved variables passed to the querygraphQLContext
- the graphql context in placelocale
- the locale to use- Returns:
- a parsed value which may be null
- Throws:
CoercingParseLiteralException
- if input literal can't be parsed
-
valueToLiteral
@Deprecated(since="2022-08-22") @NotNull default @NotNull Value valueToLiteral(@NotNull @NotNull java.lang.Object input)
Deprecated.This is deprecated and you should implementvalueToLiteral(Object, GraphQLContext, Locale)
insteadConverts an external input value to a literal (Ast Value).
IMPORTANT: the argument is validated before by calling
parseValue(Object)
.- Parameters:
input
- an external input value- Returns:
- The literal matching the external input value.
-
valueToLiteral
@NotNull default @NotNull Value<?> valueToLiteral(@NotNull @NotNull java.lang.Object input, @NotNull @NotNull GraphQLContext graphQLContext, @NotNull @NotNull java.util.Locale locale)
Converts an external input value to a literal (Ast Value).IMPORTANT: the argument is validated before by calling
parseValue(Object)
.- Parameters:
input
- an external input valuegraphQLContext
- the graphql context in placelocale
- the locale to use- Returns:
- The literal matching the external input value.
-
-