Skip to content

Commit 3062e2b

Browse files
committed
change: This is maybe a breaking change: GraphQLFloat now returns double instead of float.
The naming is unfortunately based on float = double precision according to the spec. This clashes with the Java world where float != double.
1 parent 3414572 commit 3062e2b

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/main/java/graphql/Scalars.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ public Object parseLiteral(Object input) {
6868

6969
public static GraphQLScalarType GraphQLFloat = new GraphQLScalarType("Float", "Built-in Float", new Coercing() {
7070
@Override
71-
public Float serialize(Object input) {
71+
public Double serialize(Object input) {
7272
if (input instanceof String) {
73-
return Float.parseFloat((String) input);
73+
return Double.parseDouble((String) input);
74+
} else if (input instanceof Double) {
75+
return (Double) input;
7476
} else if (input instanceof Float) {
75-
return (Float) input;
77+
return (double) (Float) input;
7678
} else if (input instanceof Integer) {
77-
return (float) (Integer) input;
79+
return (double) (Integer) input;
7880
} else {
7981
return null;
8082
}

src/test/groovy/graphql/ScalarsTest.groovy

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import graphql.language.FloatValue
55
import graphql.language.IntValue
66
import graphql.language.StringValue
77
import spock.lang.Specification
8-
8+
import spock.lang.Unroll
99

1010
class ScalarsTest extends Specification {
1111

@@ -107,18 +107,20 @@ class ScalarsTest extends Specification {
107107
new FloatValue(42.3) | 42.3f
108108
}
109109

110-
def "Float serialize/parseValue object"() {
110+
@Unroll
111+
def "Float serialize/parseValue #value into #result"() {
111112
expect:
112113
Scalars.GraphQLFloat.getCoercing().serialize(value) == result
113114
Scalars.GraphQLFloat.getCoercing().parseValue(value) == result
114115

115116
where:
116-
value | result
117-
"42.3" | 42.3f
118-
"42.0" | 42.0f
119-
new Float(42.3) | 42.3f
120-
10 | 10.0f
121-
null | null
117+
value | result
118+
"11.3" | 11.3d
119+
"24.0" | 24.0d
120+
42.3f | 42.3f
121+
10 | 10.0d
122+
90.000004d | 90.000004d
123+
null | null
122124
}
123125

124126
def "Boolean parse literal"() {

src/test/groovy/graphql/execution/ValuesResolverTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ValuesResolverTest extends Specification {
3232
GraphQLInt | new TypeName("Int") | 100 || 100
3333
GraphQLString | new TypeName("String") | 'someString' || 'someString'
3434
GraphQLBoolean | new TypeName("Boolean") | 'true' || true
35-
GraphQLFloat | new TypeName("Float") | '42.43' || 42.43f
35+
GraphQLFloat | new TypeName("Float") | '42.43' || 42.43d
3636

3737
}
3838

0 commit comments

Comments
 (0)