|
20 | 20 | @Internal
|
21 | 21 | public class DateTimeScalar extends GraphQLScalarType {
|
22 | 22 |
|
23 |
| - |
24 |
| - static Coercing<OffsetDateTime, String> RFC3339_DATETIME_COERCING = new Coercing<OffsetDateTime, String>() { |
25 |
| - @Override |
26 |
| - public String serialize(Object input) throws CoercingSerializeException { |
27 |
| - OffsetDateTime offsetDateTime; |
28 |
| - if (input instanceof OffsetDateTime) { |
29 |
| - offsetDateTime = (OffsetDateTime) input; |
30 |
| - } else if (input instanceof ZonedDateTime) { |
31 |
| - offsetDateTime = ((ZonedDateTime) input).toOffsetDateTime(); |
32 |
| - } else { |
33 |
| - throw new CoercingParseValueException( |
34 |
| - "Expected something we can convert to 'java.time.OffsetDateTime' but was '" + typeName(input) + "'." |
35 |
| - ); |
36 |
| - } |
37 |
| - try { |
38 |
| - return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime); |
39 |
| - } catch (DateTimeException e) { |
40 |
| - throw new CoercingSerializeException( |
41 |
| - "Unable to turn TemporalAccessor into OffsetDateTime because of : '" + e.getMessage() + "'." |
42 |
| - ); |
| 23 | + public DateTimeScalar() { |
| 24 | + super("DateTime", "An RFC-3339 compliant DateTime Scalar", new Coercing<OffsetDateTime, String>() { |
| 25 | + @Override |
| 26 | + public String serialize(Object input) throws CoercingSerializeException { |
| 27 | + OffsetDateTime offsetDateTime; |
| 28 | + if (input instanceof OffsetDateTime) { |
| 29 | + offsetDateTime = (OffsetDateTime) input; |
| 30 | + } else if (input instanceof ZonedDateTime) { |
| 31 | + offsetDateTime = ((ZonedDateTime) input).toOffsetDateTime(); |
| 32 | + } else if (input instanceof String) { |
| 33 | + offsetDateTime = parseOffsetDateTime(input.toString(), CoercingSerializeException::new); |
| 34 | + } else { |
| 35 | + throw new CoercingSerializeException( |
| 36 | + "Expected something we can convert to 'java.time.OffsetDateTime' but was '" + typeName(input) + "'." |
| 37 | + ); |
| 38 | + } |
| 39 | + try { |
| 40 | + return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime); |
| 41 | + } catch (DateTimeException e) { |
| 42 | + throw new CoercingSerializeException( |
| 43 | + "Unable to turn TemporalAccessor into OffsetDateTime because of : '" + e.getMessage() + "'." |
| 44 | + ); |
| 45 | + } |
43 | 46 | }
|
44 |
| - } |
45 | 47 |
|
46 |
| - @Override |
47 |
| - public OffsetDateTime parseValue(Object input) throws CoercingParseValueException { |
48 |
| - if (!(input instanceof String)) { |
49 |
| - throw new CoercingParseValueException( |
50 |
| - "Expected a 'String' but was '" + typeName(input) + "'." |
51 |
| - ); |
| 48 | + @Override |
| 49 | + public OffsetDateTime parseValue(Object input) throws CoercingParseValueException { |
| 50 | + OffsetDateTime offsetDateTime; |
| 51 | + if (input instanceof OffsetDateTime) { |
| 52 | + offsetDateTime = (OffsetDateTime) input; |
| 53 | + } else if (input instanceof ZonedDateTime) { |
| 54 | + offsetDateTime = ((ZonedDateTime) input).toOffsetDateTime(); |
| 55 | + } else if (input instanceof String) { |
| 56 | + offsetDateTime = parseOffsetDateTime(input.toString(), CoercingParseValueException::new); |
| 57 | + } else { |
| 58 | + throw new CoercingParseValueException( |
| 59 | + "Expected a 'String' but was '" + typeName(input) + "'." |
| 60 | + ); |
| 61 | + } |
| 62 | + return offsetDateTime; |
52 | 63 | }
|
53 |
| - return parseStr(input.toString(), CoercingParseValueException::new); |
54 |
| - } |
55 | 64 |
|
56 |
| - @Override |
57 |
| - public OffsetDateTime parseLiteral(Object input) throws CoercingParseLiteralException { |
58 |
| - if (!(input instanceof StringValue)) { |
59 |
| - throw new CoercingParseLiteralException( |
60 |
| - "Expected AST type 'StringValue' but was '" + typeName(input) + "'." |
61 |
| - ); |
| 65 | + @Override |
| 66 | + public OffsetDateTime parseLiteral(Object input) throws CoercingParseLiteralException { |
| 67 | + if (!(input instanceof StringValue)) { |
| 68 | + throw new CoercingParseLiteralException( |
| 69 | + "Expected AST type 'StringValue' but was '" + typeName(input) + "'." |
| 70 | + ); |
| 71 | + } |
| 72 | + return parseOffsetDateTime(((StringValue) input).getValue(), CoercingParseLiteralException::new); |
62 | 73 | }
|
63 |
| - return parseStr(((StringValue) input).getValue(), CoercingParseLiteralException::new); |
64 |
| - } |
65 | 74 |
|
66 |
| - private OffsetDateTime parseStr(String s, Function<String, RuntimeException> exceptionMaker) { |
67 |
| - try { |
68 |
| - return OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME); |
69 |
| - } catch (DateTimeParseException e) { |
70 |
| - throw exceptionMaker.apply("Invalid RFC3339 value : '" + s + "'. because of : '" + e.getMessage() + "'"); |
| 75 | + private OffsetDateTime parseOffsetDateTime(String s, Function<String, RuntimeException> exceptionMaker) { |
| 76 | + try { |
| 77 | + return OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME); |
| 78 | + } catch (DateTimeParseException e) { |
| 79 | + throw exceptionMaker.apply("Invalid RFC3339 value : '" + s + "'. because of : '" + e.getMessage() + "'"); |
| 80 | + } |
71 | 81 | }
|
72 |
| - } |
73 |
| - }; |
74 |
| - |
75 |
| - public DateTimeScalar() { |
76 |
| - super("DateTime", "An RFC-3339 compliant DateTime Scalar", RFC3339_DATETIME_COERCING); |
| 82 | + }); |
77 | 83 | }
|
| 84 | + |
78 | 85 | }
|
0 commit comments