-
|
I am using a few simple custom scalars in an application. As documented, this is done by providing a class that implements the Coercing interface. For reference, this is one of them, defining a scalar representing a public final class InstantScalar implements Coercing<Instant, String> {
@Override
public String serialize(Object dataFetcherResult) {
if (dataFetcherResult instanceof Instant) {
return dataFetcherResult.toString();
} else {
throw new CoercingSerializeException(dataFetcherResult + " is not a valid Instant");
}
}
@Override
public Instant parseValue(Object input) {
try {
return Instant.parse(input.toString());
} catch (RuntimeException exception) {
throw new CoercingParseValueException(
input + " is not a valid ISO date time value.", exception);
}
}
@Override
public Instant parseLiteral(Object input) {
if (input instanceof StringValue stringValue) {
try {
return parseValue(stringValue.getValue());
} catch (RuntimeException exception) {
throw new CoercingParseLiteralException(
input + " is not a valid ISO date time literal.", exception);
}
}
throw new CoercingParseLiteralException(
input + " must be an instance of graphql.language.StringValue.");
}
}In the graphql-java 20.0 release notes, it is described that there are breaking changes to coercion, but I am unsure how to adapt to those changes. The example from the documentation and my class there above now both How do I provide custom scalar coercion in graphql-java 20.0 without depending on the deprecated APIs? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Hi @Ernir, There were two recent changes to scalars:
The Coercing interface was changed to support i18n for coercing error messages, new methods take The breaking change highlighted at the top of the v20.0 release notes relates to the built-in scalars only, not for custom scalars. As more info, we made this change to align the built-in scalar You rightly point out our documentation is out of date! For anyone reading this, documentation PRs are always welcome on the I hope that helps! |
Beta Was this translation helpful? Give feedback.
Hi @Ernir,
There were two recent changes to scalars:
parseValuecoercion highlighted in the v20.0 release notesThe Coercing interface was changed to support i18n for coercing error messages, new methods take
LocaleandGraphQLContextas arguments. Have a look at built-in scalar Coercing implementations for ideas, for example Boolean: https://github.com/graphql-java/graphql-java/blob/v20.0/src/main/java/graphql/scalar/GraphqlBooleanCoercing.javaThe breaking change highlighted at the top of the v20.0 release notes relates to the built-in scalars …