Skip to content

Commit 3e968ed

Browse files
committed
add ID type
1 parent 948231f commit 3e968ed

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

src/main/java/graphql/Scalars.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,21 @@ public Object coerceLiteral(Object input) {
106106
});
107107

108108

109-
// public static GraphQLScalarType GraphQLID = new GraphQLScalarType("ID", "Built-in ID", new Coercing() {
110-
// @Override
111-
// public Object coerce(Object input) {
112-
// return input;
113-
// }
114-
//
115-
// @Override
116-
// public Object coerceLiteral(Object input) {
117-
// return input;
118-
// }
119-
// });
109+
public static GraphQLScalarType GraphQLID = new GraphQLScalarType("ID", "Built-in ID", new Coercing() {
110+
@Override
111+
public Object coerce(Object input) {
112+
if(input instanceof String){
113+
return input;
114+
}
115+
116+
throw new GraphQLException();
117+
}
118+
119+
@Override
120+
public Object coerceLiteral(Object input) {
121+
if (!(input instanceof StringValue)) return null;
122+
return ((StringValue) input).getValue();
123+
}
124+
});
120125

121126
}

src/test/groovy/graphql/ScalarsTest.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ class ScalarsTest extends Specification {
2828
"test" | "test"
2929
}
3030

31+
def "ID coerce literal"() {
32+
expect:
33+
Scalars.GraphQLID.getCoercing().coerceLiteral(literal) == result
34+
35+
where:
36+
literal | result
37+
new StringValue("5457486ABSBHS4w646") | "5457486ABSBHS4w646"
38+
}
39+
40+
def "ID coerce object"() {
41+
expect:
42+
Scalars.GraphQLID.getCoercing().coerce(value) == result
43+
44+
where:
45+
value | result
46+
"5457486ABSBHS4w646" | "5457486ABSBHS4w646"
47+
}
48+
3149
def "Int coerce literal"() {
3250
expect:
3351
Scalars.GraphQLInt.getCoercing().coerceLiteral(literal) == result

0 commit comments

Comments
 (0)