question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for OffsetDateTime

See original GitHub issue

Needed this so wrote my own (pasted below). I also was unable to use this library because graphql-java doesn’t allow clients to change the scalar name (I wanted a Java LocalDate to be called just Date as a scalar in graphql).

This more closely matches the behavior I expect https://www.npmjs.com/package/graphql-iso-date to have wrt its Date and DateTime scalars.

    // example: 2007-12-03T10:15:30+01:00
    // example: 2007-12-03T10:15:30Z
    public static final GraphQLScalarType OFFSET_DATE_TIME_TYPE = new GraphQLScalarType("Timestamp",
            "A Java OffsetDateTime",
            new Coercing<OffsetDateTime, String>() {
                private OffsetDateTime convertImpl(Object input) {
                    if (input instanceof String) {
                        try {
                            return OffsetDateTime.parse((String) input, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
                        } catch (DateTimeParseException ignored) {
                            return null;
                        }
                    }
                    return null;
                }

                @Override
                public String serialize(Object input) {
                    if (input instanceof OffsetDateTime) {
                        return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format((OffsetDateTime) input);
                    } else {
                        OffsetDateTime result = convertImpl(input);
                        if (result == null) {
                            throw new CoercingSerializeException(
                                    "Invalid value '" + input + "' for Timestamp (a Java OffsetDateTime)");
                        }
                        return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(result);
                    }
                }

                @Override
                public OffsetDateTime parseValue(Object input) {
                    OffsetDateTime result = convertImpl(input);
                    if (result == null) {
                        throw new CoercingParseValueException(
                                "Invalid value '" + input + "' for Timestamp (a Java OffsetDateTime)");
                    }
                    return result;
                }

                @Override
                public OffsetDateTime parseLiteral(Object input) {
                    if (!(input instanceof StringValue)) {
                        return null;
                    }
                    String value = ((StringValue) input).getValue();
                    OffsetDateTime result = convertImpl(value);
                    return result;
                }
            });

    // example: 2012-01-05
    public static final GraphQLScalarType LOCAL_DATE_TYPE = new GraphQLScalarType("Date", "A Java LocalDate",
            new Coercing<LocalDate, String>() {
                private LocalDate convertImpl(Object input) {
                    if (input instanceof String) {
                        try {
                            return LocalDate.parse((String) input, DateTimeFormatter.ISO_LOCAL_DATE);
                        } catch (DateTimeParseException ignored) {
                            return null;
                        }
                    }
                    return null;
                }

                @Override
                public String serialize(Object input) {
                    if (input instanceof LocalDate) {
                        return DateTimeFormatter.ISO_LOCAL_DATE.format((LocalDate) input);
                    } else {
                        LocalDate result = convertImpl(input);
                        if (result == null) {
                            throw new CoercingSerializeException(
                                    "Invalid value '" + input + "' for Date (a Java LocalDate)");
                        }
                        return DateTimeFormatter.ISO_LOCAL_DATE.format(result);
                    }
                }

                @Override
                public LocalDate parseValue(Object input) {
                    LocalDate result = convertImpl(input);
                    if (result == null) {
                        throw new CoercingParseValueException(
                                "Invalid value '" + input + "' for Date (a Java LocalDate)");
                    }
                    return result;
                }

                @Override
                public LocalDate parseLiteral(Object input) {
                    if (!(input instanceof StringValue)) {
                        return null;
                    }
                    String value = ((StringValue) input).getValue();
                    LocalDate result = convertImpl(value);
                    return result;
                }
            });

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

2reactions
donbeavecommented, May 20, 2018

(I wanted a Java LocalDate to be called just Date as a scalar in graphql).

Can not, because some people want to use as Date and some want to use as LocalDate.

I’ll add OffsetDateTime as a new scalar also.

Read more comments on GitHub >

github_iconTop Results From Across the Web

OffsetDateTime (Java Platform SE 8 ) - Oracle Help Center
OffsetDateTime is an immutable representation of a date-time with an offset. This class stores all date and time fields, to a precision of...
Read more >
How to persist OffsetTime and OffsetDateTime with JPA and ...
Since version 2.2, JPA offers support for mapping Java 8 Date/Time API, like LocalDateTime , LocalTime , LocalDateTimeTime , OffsetDateTime or OffsetTime ....
Read more >
Improved OffsetDateTime and ZonedDateTime Mapping in ...
As I explained in a previous article, Hibernate 5 supports ZonedDateTime and OffsetDateTime as basic types. It normalizes the timestamp and ...
Read more >
OffsetDateTime - Android Developers
OffsetDateTime adds to the instant the offset from UTC/Greenwich, which allows ... The minimum supported OffsetDateTime , '-999999999-01-01T00:00:00+18:00'.
Read more >
Add support for persisting OffsetDateTime/ZonedDateTime ...
I don't want to add logic for Zoned* variants of date and time types. In almost all cases we have seen, the timezone...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found