-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathCurrencyScalar.java
More file actions
80 lines (67 loc) · 3.24 KB
/
CurrencyScalar.java
File metadata and controls
80 lines (67 loc) · 3.24 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
package graphql.scalars.currency;
import graphql.GraphQLContext;
import graphql.Internal;
import graphql.execution.CoercedVariables;
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.util.Currency;
import java.util.Locale;
import java.util.function.Function;
import static graphql.scalars.util.Kit.typeName;
/**
* Access this via {@link graphql.scalars.ExtendedScalars#Currency}
*/
@Internal
public class CurrencyScalar {
public static final GraphQLScalarType INSTANCE;
static {
Coercing<Currency, String> coercing = new Coercing<>() {
@Override
public String serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
Currency currency = parseCurrency(input, CoercingSerializeException::new);
return currency.getCurrencyCode();
}
@Override
public Currency parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
return parseCurrency(input, CoercingParseValueException::new);
}
@Override
public Currency parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
if (!(input instanceof StringValue)) {
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + typeName(input) + "'.");
}
String stringValue = ((StringValue) input).getValue();
return parseCurrency(stringValue, CoercingParseLiteralException::new);
}
@Override
public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) {
String serializedInput = serialize(input, graphQLContext, locale);
return StringValue.newStringValue(serializedInput).build();
}
private Currency parseCurrency(Object input, Function<String, RuntimeException> exceptionMaker) {
final Currency result;
if (input instanceof Currency) {
result = (Currency) input;
} else if (input instanceof String) {
try {
result = Currency.getInstance((String) input);
} catch (NullPointerException | IllegalArgumentException ex) {
throw exceptionMaker.apply("Invalid ISO 4217 value : '" + input + "'. because of : '" + ex.getMessage() + "'");
}
} else {
throw exceptionMaker.apply("Expected a 'String' or 'Currency' but was '" + typeName(input) + "'.");
}
return result;
}
};
INSTANCE = GraphQLScalarType.newScalar()
.name("Currency")
.description("An ISO-4217 compliant Currency Scalar")
.coercing(coercing).build();
}
}