Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.donbeave:graphql-java-datetime:4.0.0'
}
dependencies {
implementation("com.github.donbeave:graphql-java-datetime:4.0.0")
}
<dependency>
<groupId>com.github.donbeave</groupId>
<artifactId>graphql-java-datetime</artifactId>
<version>4.0.0</version>
</dependency>
libraryDependencies += "com.github.donbeave" % "graphql-java-datetime" % "4.0.0"
:dependencies [[com.github.donbeave/graphql-java-datetime "4.0.0"]]
GraphQL ISO Date is a set of RFC 3339 compliant date/time scalar types to be used with graphql-java.
A set of ISO 33601, RFC 3339 compatible date time scalars for GraphQL Java implementation (graphql-java), as well as starters for
java.util.Date, java.time.LocalDate, java.time.LocalDateTime
| Format | JSON 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:ss | 2017-07-09T11:54:42 | | yyyy-MM-dd | 2017-07-09 |
java.time.LocalTime
| Format | JSON String | |:-------------|:-------------| | HH:MM:ss.SSS | 17:59:59.129 | | HH:MM:ss | 17:59:59 | | HH:MM | 17:59 |
java.time.Duration
| JSON String | |:-----------------| | PT1H30M | | P1DT3H30M | | P3Y6M4DT12H30M5S |
This library supports the following popular graphql-java framework:
Add one of the following starters according to your project.
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>
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")
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
Now you can use these scalars in your application. Here are graphql-datetime spring boot sample applications:
To report any bug, please use the project Issues section on GitHub.
Please contribute using Github Flow. Create a branch, add commits, and open a pull request.
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.