Skip to content

Commit 490fd97

Browse files
Eugen Stanbbakerman
Eugen Stan
authored andcommitted
Add Locale scalar - partial fix for graphql-java#4
1 parent 3decba9 commit 490fd97

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.idea
22
.gradle
33
out
4-
*.iml
4+
*.iml
5+
6+
build

src/main/java/graphql/scalars/ExtendedScalars.java

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import graphql.scalars.object.ObjectScalar;
1818
import graphql.scalars.regex.RegexScalar;
1919
import graphql.scalars.url.UrlScalar;
20+
import graphql.scalars.locale.LocaleScalar;
2021
import graphql.schema.GraphQLScalarType;
2122

2223
/**
@@ -111,6 +112,12 @@ public class ExtendedScalars {
111112
*/
112113
public static GraphQLScalarType Url = new UrlScalar();
113114

115+
/**
116+
* A Locale scalar that accepts a IETF BCP 47 language tag string and produces {@link
117+
* java.util.Locale} objects at runtime.
118+
*/
119+
public static GraphQLScalarType Locale = new LocaleScalar();
120+
114121
/**
115122
* An `Int` scalar that MUST be greater than zero
116123
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package graphql.scalars.locale;
2+
3+
import static graphql.scalars.util.Kit.typeName;
4+
5+
import graphql.Internal;
6+
import graphql.language.StringValue;
7+
import graphql.schema.Coercing;
8+
import graphql.schema.CoercingParseLiteralException;
9+
import graphql.schema.CoercingParseValueException;
10+
import graphql.schema.CoercingSerializeException;
11+
import graphql.schema.GraphQLScalarType;
12+
import java.util.Locale;
13+
14+
/**
15+
* Access this via {@link graphql.scalars.ExtendedScalars#Locale}
16+
*/
17+
@Internal
18+
public class LocaleScalar extends GraphQLScalarType {
19+
20+
public LocaleScalar() {
21+
super("Locale", "A IETF BCP 47 language tag", new Coercing<Locale, String>() {
22+
23+
@Override
24+
public String serialize(Object input) throws CoercingSerializeException {
25+
if (input instanceof String) {
26+
try {
27+
return Locale.forLanguageTag((String) input).toLanguageTag();
28+
} catch (Exception e) {
29+
throw new CoercingSerializeException(
30+
"Expected a valid language tag string but was but was " + typeName(input));
31+
}
32+
}
33+
if (input instanceof Locale) {
34+
return ((Locale) input).toLanguageTag();
35+
} else {
36+
throw new CoercingSerializeException(
37+
"Expected a 'java.util.Locale' object but was " + typeName(input));
38+
}
39+
}
40+
41+
@Override
42+
public Locale parseValue(Object input) throws CoercingParseValueException {
43+
if (input instanceof String) {
44+
try {
45+
return Locale.forLanguageTag(input.toString());
46+
} catch (Exception e) {
47+
throw new CoercingParseValueException(
48+
"Unable to parse value to 'java.util.Locale' because of: " + e.getMessage());
49+
}
50+
} else {
51+
throw new CoercingParseValueException(
52+
"Expected a 'java.lang.String' object but was " + typeName(input));
53+
}
54+
}
55+
56+
@Override
57+
public Locale parseLiteral(Object input) throws CoercingParseLiteralException {
58+
if (input instanceof StringValue) {
59+
return Locale.forLanguageTag(((StringValue) input).getValue());
60+
} else {
61+
throw new CoercingParseLiteralException(
62+
"Expected a 'java.lang.String' object but was " + typeName(input));
63+
}
64+
}
65+
});
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package graphql.scalars.locale
2+
3+
import graphql.language.StringValue
4+
import spock.lang.Specification
5+
import spock.lang.Unroll
6+
7+
import static graphql.scalars.util.TestKit.mkLocalDate
8+
import static graphql.scalars.util.TestKit.mkLocalDate
9+
import static graphql.scalars.util.TestKit.mkLocalDate
10+
import static graphql.scalars.util.TestKit.mkLocale
11+
import static graphql.scalars.util.TestKit.mkOffsetDT
12+
import static graphql.scalars.util.TestKit.mkZonedDT
13+
14+
class LocaleScalarTest extends Specification {
15+
16+
def coercing = new LocaleScalar().getCoercing()
17+
18+
@Unroll
19+
def "full locale parseValue"() {
20+
21+
when:
22+
def result = coercing.parseValue(input)
23+
then:
24+
result == expectedValue
25+
where:
26+
input | expectedValue
27+
"en" | mkLocale("en")
28+
"ro-RO" | mkLocale("ro-RO")
29+
"zh-hakka" | mkLocale("zh-hakka")
30+
}
31+
32+
@Unroll
33+
def "full Locale parseLiteral"() {
34+
when:
35+
def result = coercing.parseLiteral(input)
36+
then:
37+
result == expectedValue
38+
where:
39+
input | expectedValue
40+
new StringValue("ro-RO") | mkLocale("ro-RO")
41+
}
42+
43+
@Unroll
44+
def "full Locale serialization"() {
45+
when:
46+
def result = coercing.serialize(input)
47+
then:
48+
result == expectedValue
49+
where:
50+
input | expectedValue
51+
"ro-RO" | "ro-RO"
52+
mkLocale("ro-RO") | "ro-RO"
53+
mkLocale("en") | "en"
54+
}
55+
56+
}

src/test/groovy/graphql/scalars/util/TestKit.groovy

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import java.time.ZoneOffset
1212
import java.time.ZonedDateTime
1313

1414
class TestKit {
15+
16+
static Locale mkLocale(String s) {
17+
Locale.forLanguageTag(s)
18+
}
19+
1520
static LocalDate mkLocalDate(String s) {
1621
LocalDate.parse(s)
1722
}

0 commit comments

Comments
 (0)