Skip to content

Commit 612c029

Browse files
committed
Now with toLiteral and tests
1 parent ea81b1f commit 612c029

File tree

2 files changed

+55
-41
lines changed

2 files changed

+55
-41
lines changed

src/main/java/graphql/scalars/id/UUIDScalar.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import graphql.Internal;
44
import graphql.language.StringValue;
55
import graphql.language.Value;
6+
import graphql.scalars.util.Kit;
67
import graphql.schema.Coercing;
78
import graphql.schema.CoercingParseLiteralException;
89
import graphql.schema.CoercingParseValueException;
@@ -23,42 +24,39 @@ public class UUIDScalar {
2324

2425
static {
2526
Coercing<UUID, String> coercing = new Coercing<UUID, String>() {
26-
@Override
27-
public String serialize(Object input) throws CoercingSerializeException {
27+
private UUID convertImpl(Object input) {
2828
if (input instanceof String) {
2929
try {
30-
return (UUID.fromString((String) input)).toString();
30+
return (UUID.fromString((String) input));
3131
} catch (IllegalArgumentException ex) {
32-
throw new CoercingSerializeException(
33-
"Expected a UUID value that can be converted : '" + ex.getMessage() + "'."
34-
);
32+
return null;
3533
}
3634
} else if (input instanceof UUID) {
37-
return input.toString();
38-
} else {
35+
return (UUID) input;
36+
}
37+
return null;
38+
}
39+
40+
@Override
41+
public String serialize(Object input) throws CoercingSerializeException {
42+
UUID result = convertImpl(input);
43+
if (result == null) {
3944
throw new CoercingSerializeException(
40-
"Expected something we can convert to 'java.util.UUID' but was '" + typeName(input) + "'."
45+
"Expected type 'UUID' but was '" + Kit.typeName(input) + "'."
4146
);
4247
}
48+
return result.toString();
4349
}
4450

4551
@Override
4652
public UUID parseValue(Object input) throws CoercingParseValueException {
47-
if (input instanceof String) {
48-
try {
49-
return UUID.fromString((String) input);
50-
} catch (IllegalArgumentException ex) {
51-
throw new CoercingParseValueException(
52-
"Expected a 'String' of UUID type but was '" + typeName(input) + "'."
53-
);
54-
}
55-
} else if (input instanceof UUID) {
56-
return (UUID) input;
57-
} else {
53+
UUID result = convertImpl(input);
54+
if (result == null) {
5855
throw new CoercingParseValueException(
59-
"Expected a 'String' or 'UUID' type but was '" + typeName(input) + "'."
56+
"Expected type 'UUID' but was '" + Kit.typeName(input) + "'."
6057
);
6158
}
59+
return result;
6260
}
6361

6462
@Override
@@ -78,8 +76,9 @@ public UUID parseLiteral(Object input) throws CoercingParseLiteralException {
7876
}
7977

8078
@Override
81-
public Value valueToLiteral(Object input) {
82-
return Coercing.super.valueToLiteral(input);
79+
public Value<?> valueToLiteral(Object input) {
80+
String s = serialize(input);
81+
return StringValue.newStringValue(s).build();
8382
}
8483
};
8584

src/test/groovy/graphql/scalars/id/UUIDScalarTest.groovy

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package graphql.scalars.id
22

33
import graphql.language.StringValue
4+
import graphql.scalars.ExtendedScalars
45
import graphql.schema.CoercingParseLiteralException
56
import graphql.schema.CoercingParseValueException
67
import graphql.schema.CoercingSerializeException
78
import spock.lang.Specification
89
import spock.lang.Unroll
910

10-
import static graphql.scalars.util.TestKit.*
11+
import static graphql.scalars.util.TestKit.mkStringValue
12+
import static graphql.scalars.util.TestKit.mkUUIDValue
1113

1214
class UUIDScalarTest extends Specification {
1315

14-
def coercing = new UUIDScalar().getCoercing()
16+
def coercing = ExtendedScalars.UUID.getCoercing()
1517

1618
@Unroll
1719
def "UUID parseValue"() {
@@ -21,9 +23,9 @@ class UUIDScalarTest extends Specification {
2123
then:
2224
result == expectedValue
2325
where:
24-
input | expectedValue
25-
"43f20307-603c-4ad1-83c6-6010d224fabf" | mkUUIDValue("43f20307-603c-4ad1-83c6-6010d224fabf")
26-
"787dbc2b-3ddb-4098-ad1d-63d026bac111" | mkUUIDValue("787dbc2b-3ddb-4098-ad1d-63d026bac111")
26+
input | expectedValue
27+
"43f20307-603c-4ad1-83c6-6010d224fabf" | mkUUIDValue("43f20307-603c-4ad1-83c6-6010d224fabf")
28+
"787dbc2b-3ddb-4098-ad1d-63d026bac111" | mkUUIDValue("787dbc2b-3ddb-4098-ad1d-63d026bac111")
2729
}
2830

2931
@Unroll
@@ -34,10 +36,10 @@ class UUIDScalarTest extends Specification {
3436
then:
3537
thrown(expectedValue)
3638
where:
37-
input | expectedValue
38-
"a-string-that-is-not-uuid" | CoercingParseValueException
39-
100 | CoercingParseValueException
40-
"1985-04-12" | CoercingParseValueException
39+
input | expectedValue
40+
"a-string-that-is-not-uuid" | CoercingParseValueException
41+
100 | CoercingParseValueException
42+
"1985-04-12" | CoercingParseValueException
4143
}
4244

4345
def "UUID AST literal"() {
@@ -58,8 +60,8 @@ class UUIDScalarTest extends Specification {
5860
then:
5961
thrown(expectedValue)
6062
where:
61-
input | expectedValue
62-
new StringValue("a-string-that-us-not-uuid") | CoercingParseLiteralException
63+
input | expectedValue
64+
new StringValue("a-string-that-us-not-uuid") | CoercingParseLiteralException
6365
}
6466

6567
def "UUID serialization"() {
@@ -69,10 +71,10 @@ class UUIDScalarTest extends Specification {
6971
then:
7072
result == expectedValue
7173
where:
72-
input | expectedValue
73-
"42287d47-c5bd-45e4-b470-53e426d3d503" | "42287d47-c5bd-45e4-b470-53e426d3d503"
74-
"423df0f3-cf05-4eb5-b708-ae2f4b4a052d" | "423df0f3-cf05-4eb5-b708-ae2f4b4a052d"
75-
mkUUIDValue("6a90b1e6-20f3-43e5-a7ba-34db8010c071") | "6a90b1e6-20f3-43e5-a7ba-34db8010c071"
74+
input | expectedValue
75+
"42287d47-c5bd-45e4-b470-53e426d3d503" | "42287d47-c5bd-45e4-b470-53e426d3d503"
76+
"423df0f3-cf05-4eb5-b708-ae2f4b4a052d" | "423df0f3-cf05-4eb5-b708-ae2f4b4a052d"
77+
mkUUIDValue("6a90b1e6-20f3-43e5-a7ba-34db8010c071") | "6a90b1e6-20f3-43e5-a7ba-34db8010c071"
7678
}
7779

7880
def "UUID serialization bad inputs"() {
@@ -82,9 +84,22 @@ class UUIDScalarTest extends Specification {
8284
then:
8385
thrown(expectedValue)
8486
where:
85-
input | expectedValue
86-
"1985-04-12" | CoercingSerializeException
87-
100 | CoercingSerializeException
87+
input | expectedValue
88+
"1985-04-12" | CoercingSerializeException
89+
100 | CoercingSerializeException
8890
}
8991

92+
@Unroll
93+
def "UUID valueToLiteral"() {
94+
95+
when:
96+
def result = coercing.valueToLiteral(input)
97+
then:
98+
result.isEqualTo(expectedValue)
99+
where:
100+
input | expectedValue
101+
"42287d47-c5bd-45e4-b470-53e426d3d503" | mkStringValue("42287d47-c5bd-45e4-b470-53e426d3d503")
102+
"423df0f3-cf05-4eb5-b708-ae2f4b4a052d" | mkStringValue("423df0f3-cf05-4eb5-b708-ae2f4b4a052d")
103+
mkUUIDValue("6a90b1e6-20f3-43e5-a7ba-34db8010c071") | mkStringValue("6a90b1e6-20f3-43e5-a7ba-34db8010c071")
104+
}
90105
}

0 commit comments

Comments
 (0)