Skip to content

Commit 948231f

Browse files
committed
add GraphQLLong type
1 parent a8ca9e0 commit 948231f

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/main/java/graphql/Scalars.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ public Object coerceLiteral(Object input) {
3030
}
3131
});
3232

33+
34+
public static GraphQLScalarType GraphQLLong = new GraphQLScalarType("Long", "Long type", new Coercing() {
35+
@Override
36+
public Object coerce(Object input) {
37+
if (input instanceof String) {
38+
return Long.parseLong((String) input);
39+
} else if (input instanceof Long) {
40+
return input;
41+
} else if (input instanceof Integer) {
42+
return ((Integer) input).longValue();
43+
} else {
44+
throw new GraphQLException("");
45+
}
46+
}
47+
48+
@Override
49+
public Object coerceLiteral(Object input) {
50+
if (!(input instanceof StringValue)) return null;
51+
return Long.parseLong(((StringValue) input).getValue());
52+
}
53+
});
54+
3355
public static GraphQLScalarType GraphQLFloat = new GraphQLScalarType("Float", "Built-in Float", new Coercing() {
3456
@Override
3557
public Object coerce(Object input) {

src/test/groovy/graphql/ScalarsTest.groovy

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ class ScalarsTest extends Specification {
3737
new IntValue(42) | 42
3838
}
3939

40+
def "Long coerce object"() {
41+
expect:
42+
Scalars.GraphQLLong.getCoercing().coerce(value) == result
43+
44+
where:
45+
value | result
46+
"42" | 42
47+
new Long(42345784398534785l) | 42345784398534785l
48+
new Integer(42) | 42
49+
}
50+
51+
def "Long coerce literal"() {
52+
expect:
53+
Scalars.GraphQLLong.getCoercing().coerceLiteral(literal) == result
54+
55+
where:
56+
literal | result
57+
new StringValue("42") | 42
58+
}
59+
4060
def "Int coerce object"() {
4161
expect:
4262
Scalars.GraphQLInt.getCoercing().coerce(value) == result
@@ -90,5 +110,4 @@ class ScalarsTest extends Specification {
90110
}
91111

92112

93-
94113
}

0 commit comments

Comments
 (0)