|
| 1 | +package graphql.scalars.datetime; |
| 2 | + |
| 3 | +import graphql.Internal; |
| 4 | +import graphql.language.StringValue; |
| 5 | +import graphql.language.Value; |
| 6 | +import graphql.schema.Coercing; |
| 7 | +import graphql.schema.CoercingParseLiteralException; |
| 8 | +import graphql.schema.CoercingParseValueException; |
| 9 | +import graphql.schema.CoercingSerializeException; |
| 10 | +import graphql.schema.GraphQLScalarType; |
| 11 | + |
| 12 | +import java.time.DateTimeException; |
| 13 | +import java.time.YearMonth; |
| 14 | +import java.time.format.DateTimeFormatter; |
| 15 | +import java.time.format.DateTimeParseException; |
| 16 | +import java.time.temporal.TemporalAccessor; |
| 17 | +import java.util.function.Function; |
| 18 | + |
| 19 | +import static graphql.scalars.util.Kit.typeName; |
| 20 | + |
| 21 | +/** |
| 22 | + * Access this via {@link graphql.scalars.ExtendedScalars#YearMonth} |
| 23 | + */ |
| 24 | +@Internal |
| 25 | +public final class YearMonthScalar { |
| 26 | + |
| 27 | + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM"); |
| 28 | + |
| 29 | + public static final GraphQLScalarType INSTANCE; |
| 30 | + |
| 31 | + private YearMonthScalar() {} |
| 32 | + |
| 33 | + static { |
| 34 | + Coercing<YearMonth, String> coercing = new Coercing<YearMonth, String>() { |
| 35 | + @Override |
| 36 | + public String serialize(Object input) throws CoercingSerializeException { |
| 37 | + TemporalAccessor temporalAccessor; |
| 38 | + if (input instanceof TemporalAccessor) { |
| 39 | + temporalAccessor = (TemporalAccessor) input; |
| 40 | + } else if (input instanceof String) { |
| 41 | + temporalAccessor = parseYearMonth(input.toString(), CoercingSerializeException::new); |
| 42 | + } else { |
| 43 | + throw new CoercingSerializeException( |
| 44 | + "Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'." |
| 45 | + ); |
| 46 | + } |
| 47 | + try { |
| 48 | + return DATE_FORMATTER.format(temporalAccessor); |
| 49 | + } catch (DateTimeException e) { |
| 50 | + throw new CoercingSerializeException( |
| 51 | + "Unable to turn TemporalAccessor into full yearMonth because of : '" + e.getMessage() + "'." |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public YearMonth parseValue(Object input) throws CoercingParseValueException { |
| 58 | + TemporalAccessor temporalAccessor; |
| 59 | + if (input instanceof TemporalAccessor) { |
| 60 | + temporalAccessor = (TemporalAccessor) input; |
| 61 | + } else if (input instanceof String) { |
| 62 | + temporalAccessor = parseYearMonth(input.toString(), CoercingParseValueException::new); |
| 63 | + } else { |
| 64 | + throw new CoercingParseValueException( |
| 65 | + "Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'." |
| 66 | + ); |
| 67 | + } |
| 68 | + try { |
| 69 | + return YearMonth.from(temporalAccessor); |
| 70 | + } catch (DateTimeException e) { |
| 71 | + throw new CoercingParseValueException( |
| 72 | + "Unable to turn TemporalAccessor into full yearMonth because of : '" + e.getMessage() + "'." |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public YearMonth parseLiteral(Object input) throws CoercingParseLiteralException { |
| 79 | + if (!(input instanceof StringValue)) { |
| 80 | + throw new CoercingParseLiteralException( |
| 81 | + "Expected AST type 'StringValue' but was '" + typeName(input) + "'." |
| 82 | + ); |
| 83 | + } |
| 84 | + return parseYearMonth(((StringValue) input).getValue(), CoercingParseLiteralException::new); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Value<?> valueToLiteral(Object input) { |
| 89 | + String s = serialize(input); |
| 90 | + return StringValue.newStringValue(s).build(); |
| 91 | + } |
| 92 | + |
| 93 | + private YearMonth parseYearMonth(String s, Function<String, RuntimeException> exceptionMaker) { |
| 94 | + try { |
| 95 | + TemporalAccessor temporalAccessor = DATE_FORMATTER.parse(s); |
| 96 | + return YearMonth.from(temporalAccessor); |
| 97 | + } catch (DateTimeParseException e) { |
| 98 | + throw exceptionMaker.apply("Invalid RFC3339 full yearMonth value : '" + s + "'. because of : '" + e.getMessage() + "'"); |
| 99 | + } |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + INSTANCE = GraphQLScalarType.newScalar() |
| 104 | + .name("YearMonth") |
| 105 | + .description("An RFC-3339 compliant Full YearMonth Scalar") |
| 106 | + .coercing(coercing) |
| 107 | + .build(); |
| 108 | + } |
| 109 | +} |
0 commit comments