Skip to content

Commit a3f7877

Browse files
SinaiNINigdianov
authored andcommitted
Add LocalDate/LocalDateTime/OffsetDateTime/ZonedDateTime/Instant support for query and filter (#158)
* Local Date fix * fix format * fix format * fix instant parse form string only * fix format * add ZonedDateTime * Add Unit Test on JavaScalars * Fix format * Add tests LocalDate/Time * Format code * Fix error time zone * Fix Test Error * Fix by remove unnecessary code * Add more tests * Fix duplicate testing * Add more test * Fix for with where condition * Fix test error with zonedDateTime * Extract Localdatetime to own package * Fix error of testing * add more data for test * fix expect value * Fix expect value * fix expect value of Zoned date
1 parent 257a92d commit a3f7877

File tree

9 files changed

+1739
-17
lines changed

9 files changed

+1739
-17
lines changed

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/JavaScalars.java

+132-6
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@
2121
import java.text.DateFormat;
2222
import java.text.ParseException;
2323
import java.text.SimpleDateFormat;
24-
import java.time.Instant;
25-
import java.time.LocalDate;
26-
import java.time.LocalDateTime;
27-
import java.time.LocalTime;
28-
import java.time.ZoneOffset;
24+
import java.time.*;
2925
import java.time.format.DateTimeFormatter;
3026
import java.time.format.DateTimeParseException;
3127
import java.util.Collections;
@@ -111,7 +107,10 @@ public class JavaScalars {
111107
scalarsRegistry.put(Object.class, new GraphQLScalarType("Object", "Object type", new GraphQLObjectCoercing()));
112108
scalarsRegistry.put(java.sql.Date.class, new GraphQLScalarType("SqlDate", "SQL Date type", new GraphQLSqlDateCoercing()));
113109
scalarsRegistry.put(java.sql.Timestamp.class, new GraphQLScalarType("SqlTimestamp", "SQL Timestamp type", new GraphQLSqlTimestampCoercing()));
114-
scalarsRegistry.put(Byte[].class, new GraphQLScalarType("ByteArray", "ByteArray type", new GraphQLLOBCoercing()));
110+
scalarsRegistry.put(Byte[].class, new GraphQLScalarType("ByteArray", "ByteArray type", new GraphQLLOBCoercing()));
111+
scalarsRegistry.put(Instant.class, new GraphQLScalarType("Instant", "Instant type", new GraphQLInstantCoercing()));
112+
scalarsRegistry.put(ZonedDateTime.class, new GraphQLScalarType("ZonedDateTime", "ZonedDateTime type", new GraphQLZonedDateTimeCoercing()));
113+
scalarsRegistry.put(OffsetDateTime.class, new GraphQLScalarType("OffsetDateTime", "OffsetDateTime type", new GraphQLOffsetDateTimeCoercing()));
115114
}
116115

117116
public static GraphQLScalarType of(Class<?> key) {
@@ -142,6 +141,8 @@ public Object serialize(Object input) {
142141
return parseStringToLocalDateTime((String) input);
143142
} else if (input instanceof LocalDateTime) {
144143
return input;
144+
}else if (input instanceof LocalDate) {
145+
return input;
145146
} else if (input instanceof Long) {
146147
return parseLongToLocalDateTime((Long) input);
147148
} else if (input instanceof Integer) {
@@ -331,6 +332,131 @@ private Date parseStringToDate(String input) {
331332
}
332333
};
333334

335+
public static class GraphQLInstantCoercing implements Coercing<Object, Object> {
336+
337+
@Override
338+
public Object serialize(Object input) {
339+
if (input instanceof String) {
340+
return parseStringToInstant((String) input);
341+
} else if (input instanceof Instant) {
342+
return input;
343+
}
344+
return null;
345+
}
346+
347+
@Override
348+
public Object parseValue(Object input) {
349+
return serialize(input);
350+
}
351+
352+
@Override
353+
public Object parseLiteral(Object input) {
354+
if (input instanceof StringValue) {
355+
return parseStringToInstant(((StringValue) input).getValue());
356+
}
357+
return null;
358+
}
359+
360+
private Instant parseStringToInstant(String input) {
361+
try {
362+
return Instant.parse(input);
363+
} catch (DateTimeParseException e) {
364+
log.warn("Failed to parse Date from input: " + input, e);
365+
return null;
366+
}
367+
}
368+
};
369+
370+
public static class GraphQLZonedDateTimeCoercing implements Coercing<Object, Object> {
371+
372+
@Override
373+
public Object serialize(Object input) {
374+
if (input instanceof String) {
375+
return parseStringToZonedDateTime((String) input);
376+
} else if (input instanceof ZonedDateTime) {
377+
return input;
378+
} else if (input instanceof LocalDate) {
379+
return input;
380+
} else if (input instanceof Long) {
381+
return parseLongToZonedDateTime((Long) input);
382+
} else if (input instanceof Integer) {
383+
return parseLongToZonedDateTime((Integer) input);
384+
}
385+
return null;
386+
}
387+
388+
@Override
389+
public Object parseValue(Object input) {
390+
return serialize(input);
391+
}
392+
393+
@Override
394+
public Object parseLiteral(Object input) {
395+
if (input instanceof StringValue) {
396+
return parseStringToZonedDateTime(((StringValue) input).getValue());
397+
}
398+
return null;
399+
}
400+
401+
private ZonedDateTime parseLongToZonedDateTime(long input) {
402+
return ZonedDateTime.ofInstant(Instant.ofEpochSecond(input), TimeZone.getDefault().toZoneId());
403+
}
404+
405+
private ZonedDateTime parseStringToZonedDateTime(String input) {
406+
try {
407+
return ZonedDateTime.parse(input);
408+
} catch (DateTimeParseException e) {
409+
log.warn("Failed to parse Date from input: " + input, e);
410+
return null;
411+
}
412+
}
413+
};
414+
415+
public static class GraphQLOffsetDateTimeCoercing implements Coercing<Object, Object> {
416+
417+
@Override
418+
public Object serialize(Object input) {
419+
if (input instanceof String) {
420+
return parseStringToOffsetDateTime((String) input);
421+
} else if (input instanceof OffsetDateTime) {
422+
return input;
423+
} else if (input instanceof LocalDate) {
424+
return input;
425+
} else if (input instanceof Long) {
426+
return parseLongToOffsetDateTime((Long) input);
427+
} else if (input instanceof Integer) {
428+
return parseLongToOffsetDateTime((Integer) input);
429+
}
430+
return null;
431+
}
432+
433+
@Override
434+
public Object parseValue(Object input) {
435+
return serialize(input);
436+
}
437+
438+
@Override
439+
public Object parseLiteral(Object input) {
440+
if (input instanceof StringValue) {
441+
return parseStringToOffsetDateTime(((StringValue) input).getValue());
442+
}
443+
return null;
444+
}
445+
446+
private OffsetDateTime parseLongToOffsetDateTime(long input) {
447+
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(input), TimeZone.getDefault().toZoneId());
448+
}
449+
450+
private OffsetDateTime parseStringToOffsetDateTime(String input) {
451+
try {
452+
return OffsetDateTime.parse(input);
453+
} catch (DateTimeParseException e) {
454+
log.warn("Failed to parse Date from input: " + input, e);
455+
return null;
456+
}
457+
}
458+
};
459+
334460
public static class GraphQLUUIDCoercing implements Coercing<Object, Object> {
335461

336462
@Override

0 commit comments

Comments
 (0)