forked from graphql-java/graphql-java-extended-scalars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccurateDurationScalar.java
More file actions
93 lines (80 loc) · 3.6 KB
/
AccurateDurationScalar.java
File metadata and controls
93 lines (80 loc) · 3.6 KB
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.language.Value;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLScalarType;
import java.time.Duration;
import java.time.format.DateTimeParseException;
import java.util.function.Function;
import static graphql.scalars.util.Kit.typeName;
/**
* Access this via {@link graphql.scalars.ExtendedScalars#AccurateDuration}
*/
@Internal
public class AccurateDurationScalar {
public static final GraphQLScalarType INSTANCE;
private AccurateDurationScalar() {}
static {
Coercing<Duration, String> coercing = new Coercing<Duration, String>() {
@Override
public String serialize(Object input) throws CoercingSerializeException {
Duration duration;
if (input instanceof Duration) {
duration = (Duration) input;
} else if (input instanceof String) {
duration = parseDuration(input.toString(), CoercingSerializeException::new);
} else {
throw new CoercingSerializeException(
"Expected something we can convert to 'java.time.Duration' but was '" + typeName(input) + "'."
);
}
return duration.toString();
}
@Override
public Duration parseValue(Object input) throws CoercingParseValueException {
Duration duration;
if (input instanceof Duration) {
duration = (Duration) input;
} else if (input instanceof String) {
duration = parseDuration(input.toString(), CoercingParseValueException::new);
} else {
throw new CoercingParseValueException(
"Expected a 'String' but was '" + typeName(input) + "'."
);
}
return duration;
}
@Override
public Duration parseLiteral(Object input) throws CoercingParseLiteralException {
if (!(input instanceof StringValue)) {
throw new CoercingParseLiteralException(
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
);
}
return parseDuration(((StringValue) input).getValue(), CoercingParseLiteralException::new);
}
@Override
public Value<?> valueToLiteral(Object input) {
String s = serialize(input);
return StringValue.newStringValue(s).build();
}
private Duration parseDuration(String s, Function<String, RuntimeException> exceptionMaker) {
try {
return Duration.parse(s);
} catch (DateTimeParseException e) {
throw exceptionMaker.apply("Invalid ISO 8601 value : '" + s + "'. because of : '" + e.getMessage() + "'");
}
}
};
INSTANCE = GraphQLScalarType.newScalar()
.name("AccurateDuration")
.description("A ISO 8601 duration scalar with only day, hour, minute, second components.")
.specifiedByUrl("https://scalars.graphql.org/AlexandreCarlton/accurate-duration") // TODO: Change to .specifiedByURL when builder added to graphql-java
.coercing(coercing)
.build();
}
}