-
Notifications
You must be signed in to change notification settings - Fork 60
/
UUIDScalar.java
94 lines (80 loc) · 3.16 KB
/
UUIDScalar.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
94
package graphql.scalars.id;
import graphql.Internal;
import graphql.language.StringValue;
import graphql.language.Value;
import graphql.scalars.util.Kit;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLScalarType;
import java.util.UUID;
import static graphql.scalars.util.Kit.typeName;
/**
* Access this via {@link graphql.scalars.ExtendedScalars#UUID}
*/
@Internal
public class UUIDScalar {
public static GraphQLScalarType INSTANCE;
static {
Coercing<UUID, String> coercing = new Coercing<UUID, String>() {
private UUID convertImpl(Object input) {
if (input instanceof String) {
try {
return (UUID.fromString((String) input));
} catch (IllegalArgumentException ex) {
return null;
}
} else if (input instanceof UUID) {
return (UUID) input;
}
return null;
}
@Override
public String serialize(Object input) throws CoercingSerializeException {
UUID result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'UUID' but was '" + Kit.typeName(input) + "'."
);
}
return result.toString();
}
@Override
public UUID parseValue(Object input) throws CoercingParseValueException {
UUID result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Expected type 'UUID' but was '" + Kit.typeName(input) + "'."
);
}
return result;
}
@Override
public UUID parseLiteral(Object input) throws CoercingParseLiteralException {
if (!(input instanceof StringValue)) {
throw new CoercingParseLiteralException(
"Expected a 'java.util.UUID' AST type object but was '" + typeName(input) + "'."
);
}
try {
return UUID.fromString(((StringValue) input).getValue());
} catch (IllegalArgumentException ex) {
throw new CoercingParseLiteralException(
"Expected something that we can convert to a UUID but was invalid"
);
}
}
@Override
public Value<?> valueToLiteral(Object input) {
String s = serialize(input);
return StringValue.newStringValue(s).build();
}
};
INSTANCE = GraphQLScalarType.newScalar()
.name("UUID")
.description("A universally unique identifier compliant UUID Scalar")
.coercing(coercing)
.build();
}
}