Home

Awesome

graphql-java-datetime

License Latest Release PRs Welcome

GraphQL ISO Date is a set of RFC 3339 compliant date/time scalar types to be used with graphql-java.

Summary

A set of ISO 33601, RFC 3339 compatible date time scalars for GraphQL Java implementation (graphql-java), as well as starters for

Serialization

java.util.Date, java.time.LocalDate, java.time.LocalDateTime

FormatJSON String
yyyy-MM-dd'T'HH:MM:ss.SSS'Z'2017-07-09T13:14:45.947Z
yyyy-MM-dd'T'HH:MM:ss'Z'2017-07-09T11:54:42Z
yyyy-MM-dd'T'HH:MM:ss2017-07-09T11:54:42
yyyy-MM-dd2017-07-09

java.time.LocalTime

FormatJSON String
HH:MM:ss.SSS17:59:59.129
HH:MM:ss17:59:59
HH:MM17:59

java.time.Duration

JSON String
PT1H30M
P1DT3H30M
P3Y6M4DT12H30M5S

Usage

Spring Boot

This library supports the following popular graphql-java framework:

Add one of the following starters according to your project.

Installation

Maven

Add the following to your pom.xml:

for GraphQL Java Kickstart:

<dependency>
    <groupId>com.tailrocks.graphql</groupId>
    <artifactId>graphql-datetime-kickstart-spring-boot-starter</artifactId>
    <version>6.0.0</version>
</dependency>

for Netflix DGS:

<dependency>
    <groupId>com.tailrocks.graphql</groupId>
    <artifactId>graphql-datetime-dgs-starter</artifactId>
    <version>6.0.0</version>
</dependency>

for Spring GraphQL:

<dependency>
    <groupId>com.tailrocks.graphql</groupId>
    <artifactId>graphql-datetime-spring-boot-starter</artifactId>
    <version>6.0.0</version>
</dependency>

Gradle

Add the following to your build.gradle:

for GraphQL Java Kickstart (Spring Boot):

implementation("com.tailrocks.graphql:graphql-datetime-kickstart-spring-boot-starter:6.0.0")

for DGS:

implementation("com.tailrocks.graphql:graphql-datetime-dgs-starter:6.0.0")

for Spring GraphQL:

implementation("com.tailrocks.graphql:graphql-datetime-spring-boot-starter:6.0.0")

Scalars

Add these scalars to your .graphqls schema file:

# java.util.Date implementation
scalar Date

# java.time.LocalDate implementation
scalar LocalDate

# java.time.LocalDateTime implementation
scalar LocalDateTime

# java.time.LocalTime implementation
scalar LocalTime

# java.time.OffsetDateTime implementation
scalar OffsetDateTime

# java.time.YearMonth implementation
scalar YearMonth

# java.time.Duration implementation
scalar Duration

You can rename the scalar however you want by simply adding the following properties to your application.yaml:

graphql:
  datetime:
    scalars:
      date:
        scalar-name: MyDate
      local-date:
        scalar-name: MyLocalDate
      local-date-time:
        scalar-name: MyLocalDateTime
      local-time:
        scalar-name: MyLocalTime
      offset-date-time:
        scalar-name: MyOffsetDateTime
      year-month:
        scalar-name: MyYearMonth
      duration:
        scalar-name: MyDuration

A custom format can be set for LocalDate and LocalDateTime only using the following properties in application.yaml

graphql:
  datetime:
    scalars:
      local-date:
        format: MM/dd/yyyy
      local-date-time:
        format: yyyy-MM-dd'T'HH:mm:ss

You can enable automatic zone conversion by adding the following property to your application.yaml. This will automatically convert between UTC and the default TimeZone for LocalDateTime:

graphql:
  datetime:
    scalars:
      zone-conversion-enabled: true

If using OffsetDateTime in order to present the offset and disable the automatic convertion to UTC from Jackson you should set to your application.yml the following:

spring:
  jackson:
    deserialization:
      adjust-dates-to-context-time-zone: false

Sample

Now you can use these scalars in your application. Here are graphql-datetime spring boot sample applications:

Bugs

To report any bug, please use the project Issues section on GitHub.

Contributing

Please contribute using Github Flow. Create a branch, add commits, and open a pull request.

License

Copyright © 2017-2023 Alexey Zhokhov. All rights reserved.

This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.