-
Notifications
You must be signed in to change notification settings - Fork 60
/
DateScalar.java
93 lines (83 loc) · 4 KB
/
DateScalar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package graphql.scalars.datetime;
import graphql.Internal;
import graphql.language.StringValue;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLScalarType;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.util.function.Function;
import static graphql.scalars.util.Kit.typeName;
/**
* Access this via {@link graphql.scalars.ExtendedScalars#Date}
*/
@Internal
public class DateScalar extends GraphQLScalarType {
private final static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public DateScalar() {
super("Date", "An RFC-3339 compliant Full Date Scalar", new Coercing<LocalDate, String>() {
@Override
public String serialize(Object input) throws CoercingSerializeException {
TemporalAccessor temporalAccessor;
if (input instanceof TemporalAccessor) {
temporalAccessor = (TemporalAccessor) input;
} else if (input instanceof String) {
temporalAccessor = parseLocalDate(input.toString(), CoercingSerializeException::new);
} else {
throw new CoercingSerializeException(
"Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'."
);
}
try {
return dateFormatter.format(temporalAccessor);
} catch (DateTimeException e) {
throw new CoercingSerializeException(
"Unable to turn TemporalAccessor into full date because of : '" + e.getMessage() + "'."
);
}
}
@Override
public LocalDate parseValue(Object input) throws CoercingParseValueException {
TemporalAccessor temporalAccessor;
if (input instanceof TemporalAccessor) {
temporalAccessor = (TemporalAccessor) input;
} else if (input instanceof String) {
temporalAccessor = parseLocalDate(input.toString(), CoercingParseValueException::new);
} else {
throw new CoercingParseValueException(
"Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'."
);
}
try {
return LocalDate.from(temporalAccessor);
} catch (DateTimeException e) {
throw new CoercingParseValueException(
"Unable to turn TemporalAccessor into full date because of : '" + e.getMessage() + "'."
);
}
}
@Override
public LocalDate parseLiteral(Object input) throws CoercingParseLiteralException {
if (!(input instanceof StringValue)) {
throw new CoercingParseLiteralException(
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
);
}
return parseLocalDate(((StringValue) input).getValue(), CoercingParseLiteralException::new);
}
private LocalDate parseLocalDate(String s, Function<String, RuntimeException> exceptionMaker) {
try {
TemporalAccessor temporalAccessor = dateFormatter.parse(s);
return LocalDate.from(temporalAccessor);
} catch (DateTimeParseException e) {
throw exceptionMaker.apply("Invalid RFC3339 full date value : '" + s + "'. because of : '" + e.getMessage() + "'");
}
}
});
}
}