ZoneId in Java as scalar
See original GitHub issueHello,
In Java we have the java.time.ZoneId
object and I want to have it as scalar in GraphQL.
So I have this GraphQLScararType
implementation:
public class CustomZoneIdType {
public static GraphQLScalarType getScalarType() {
final Class<ZoneId> type = ZoneId.class;
return new GraphQLScalarType(type.getSimpleName(), type.getSimpleName() + " for datetime",
new Coercing<ZoneId, String>() {
@Override
public String serialize(final Object input) {
if (input == null) {
return null;
}
return input.toString();
}
@Override
public ZoneId parseValue(final Object input) {
if (input == null) {
return null;
}
if (input instanceof String) {
return ZoneId.of((String) input);
}
throw new CoercingParseValueException("Invalid input for parsing ZoneId from object");
}
@Override
public ZoneId parseLiteral(final Object input) {
if (input == null) {
return null;
}
if (input instanceof StringValue) {
return ZoneId.of(((StringValue) input).getValue());
}
throw new CoercingParseValueException("Invalid input for parsing ZoneId from literal");
}
});
}
}
Also in the GraphQLSchemaGenerator
I’ve registered it like this:
.withAdditionalTypes(Arrays.asList(
CustomZoneIdType.getScalarType())
)
.generate();
The problem is that when having the ZoneId
as a field on an output it works well and the serialize
method is called well.
But when having it on an input object it is generated as ZoneIdInput
and I cannot pass a string to it because it requires a map from me:
"errorDescription": "Could not resolve type id 'ZoneId' as a subtype of [simple type, class java.time.ZoneId]: known type ids = [] (for POJO property 'zoneId')\n at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: org.mypackage.MyClass[\"zoneId\"])"
I have io.leangen.graphql:spqr version 0.9.6
.
Please help me discovering what I’m doing wrong.
Regards, Attila
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
java.time.ZoneId.toString java code examples - Tabnine
ScalarTypeZoneId.formatValue(...) @Override public String formatValue(ZoneId v) { return v.toString(); }. origin: ebean-orm/ebean ...
Read more >ZoneId (Java Platform SE 8 ) - Oracle Help Center
A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime . There are two distinct types...
Read more >Java Date Time - Period multipliedBy(int scalar) example
Period multipliedBy(int scalar) returns a new instance with each element in this period multiplied by the specified scalar.
Read more >Java 8 Features - Scaler Topics
ZonedDateTime takes date and time and ZoneId as arguments. Default and Static Methods. From Java 8 onwards, interfaces can have declared methods ...
Read more >org.cactoos.Scalar Java Examples - ProgramCreek.com
This page shows Java code examples of org.cactoos.Scalar.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve adapted my code to your suggestion and it works well now. I don’t need the
SCALAR_SUFFIX
.I’ll put here the final code parts, maybe if others will have the same problem this will help out them.
How to register a custom scalar The version used is 0.9.9. I have my scalar implementation:
Utility methods:
Then I’ve create static instance for the scalar:
I have the type mapper like this:
And finally in the
GraphQLSchemaGenerator
I just added this row:It seems that I don’t need to register my scalar in the
withAdditionalTypes
because it is working also only with calling thewithTypeMappers
method and registering the type mapper. Is there a reason @kaqqao to add the mapper and the GraphQLZoneId in the additional types also?So yes, this is it with custom scalars 😄
Thank you very much @kaqqao for helping me!
Good evening Guys,
I know that this ticket is close, so can I put my comments on this ticket. Please confirm and if this is a violation then I will open a new ticket.
In my case I have applied the above logic and my ZoneId is loaded successfully. But in my case my ZoneId output should contains the rules (ZoneRules) as well which is part of ZoneOffset (extending abstract class ZoneId). But while implementing the logic it is returning id only as the outcome. Can someone please help in extracting the ZoneOffset variables i.e. Rules.
https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html https://docs.oracle.com/javase/8/docs/api/java/time/zone/ZoneRules.html
Please suggest.