|
21 | 21 | import java.text.DateFormat;
|
22 | 22 | import java.text.ParseException;
|
23 | 23 | 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.*; |
29 | 25 | import java.time.format.DateTimeFormatter;
|
30 | 26 | import java.time.format.DateTimeParseException;
|
31 | 27 | import java.util.Collections;
|
@@ -111,7 +107,10 @@ public class JavaScalars {
|
111 | 107 | scalarsRegistry.put(Object.class, new GraphQLScalarType("Object", "Object type", new GraphQLObjectCoercing()));
|
112 | 108 | scalarsRegistry.put(java.sql.Date.class, new GraphQLScalarType("SqlDate", "SQL Date type", new GraphQLSqlDateCoercing()));
|
113 | 109 | 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())); |
115 | 114 | }
|
116 | 115 |
|
117 | 116 | public static GraphQLScalarType of(Class<?> key) {
|
@@ -142,6 +141,8 @@ public Object serialize(Object input) {
|
142 | 141 | return parseStringToLocalDateTime((String) input);
|
143 | 142 | } else if (input instanceof LocalDateTime) {
|
144 | 143 | return input;
|
| 144 | + }else if (input instanceof LocalDate) { |
| 145 | + return input; |
145 | 146 | } else if (input instanceof Long) {
|
146 | 147 | return parseLongToLocalDateTime((Long) input);
|
147 | 148 | } else if (input instanceof Integer) {
|
@@ -331,6 +332,131 @@ private Date parseStringToDate(String input) {
|
331 | 332 | }
|
332 | 333 | };
|
333 | 334 |
|
| 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 | + |
334 | 460 | public static class GraphQLUUIDCoercing implements Coercing<Object, Object> {
|
335 | 461 |
|
336 | 462 | @Override
|
|
0 commit comments