In our GraphQL SpringBoot application we are successfully using custom scalar implementation for LocalDateTime via graphql-java-datetime
<dependency>
<groupId>com.zhokhov.graphql</groupId>
<artifactId>graphql-datetime-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>4.3.0</version>
</dependency>
It is all working fine as long as we are only using it as String in schema
scalar LocalDateTime
input AttendanceFilter {
# date format yyyy-MM-ddThh:mm:ss
fromDateTime: String
toDateTime: String
}
type Query {
attendanceList(filter: AttendanceFilter): [Attendance]
}
type Attendance {
date: LocalDateTime
}
we can also use Java type of LocalDateTime in Java resolvers implementation
public List<AttendancePOJO> attendanceList(AttendanceFilter filter) {
LocalDateTime fromDateTime = filter.getFromDateTime;
LocalDateTime toDateTime = filter.getToDateTime;
return jpaAttendanceRepository.findByFromTimeAfterAndToTimeBefore(fromDateTime, toDateTime) }
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
public class AttendanceFilter {
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(pattern = "yyyy[-]MM[-]dd'T'HH[:]mm[:]ss")
private LocalDateTime fromDateTime;
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(pattern = "yyyy[-]MM[-]dd'T'HH[:]mm[:]ss")
private LocalDateTime toDateTime;
}
And GraphQL query such as
query: {
attendanceList(filter: {fromDateTime: "2018-01-01T00:00:00",
toDateTime: "2018-06-01T23:59:59"}) { date }
}
works just fine.
But when we wanted to make it more readable, and instead of String parameter with fixed format to use LocalDateTime even in schema query.graphqls
input AttendanceFilter {
# date format yyyy-MM-ddThh:mm:ss
fromDateTime: LocalDateTime
toDateTime: LocalDateTime
}
the deserialization does not seem to work properly and returns an error
Exception while fetching data (/attendanceList) : Unexpected token (START_OBJECT), expected VALUE_STRING: Expected array or string. at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: ***.AttendanceFilter["fromDateTime"])
java.lang.IllegalArgumentException: Unexpected token (START_OBJECT), expected VALUE_STRING: Expected array or string. at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: ***.AttendanceFilter["fromDateTime"])
Can anybody please suggest how to convert the values of JSON input in AttendanceFilter object into Java? Thank you very much in advance