Skip to content

Commit 0964fe1

Browse files
committed
When resolving a Double value, checking for NaN and returning null
* When resolving a Double value, checking for NaN and returning null * When resolving a Double value, checking for NaN and returning null, also for BatchedStrategy
1 parent 45c4755 commit 0964fe1

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

src/main/java/graphql/execution/ExecutionStrategy.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ protected ExecutionResult completeValueForEnum(GraphQLEnumType enumType, Object
118118
}
119119

120120
protected ExecutionResult completeValueForScalar(GraphQLScalarType scalarType, Object result) {
121-
return new ExecutionResultImpl(scalarType.getCoercing().serialize(result), null);
121+
Object serialized = scalarType.getCoercing().serialize(result);
122+
//6.6.1 http://facebook.github.io/graphql/#sec-Field-entries
123+
if (serialized instanceof Double && ((Double) serialized).isNaN()) {
124+
serialized = null;
125+
}
126+
return new ExecutionResultImpl(serialized, null);
122127
}
123128

124129
protected ExecutionResult completeValueForList(ExecutionContext executionContext, GraphQLList fieldType, List<Field> fields, List<Object> result) {

src/main/java/graphql/execution/ValuesResolver.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ private Object coerceValueAst(GraphQLType type, Value inputValue, Map<String, Ob
122122
return variables.get(((VariableReference) inputValue).getName());
123123
}
124124
if (type instanceof GraphQLScalarType) {
125-
//6.6.1 http://facebook.github.io/graphql/#sec-Field-entries
126-
Object value = ((GraphQLScalarType) type).getCoercing().parseLiteral(inputValue);
127-
if (value instanceof Double && ((Double) value).isNaN()) return null;
128-
return value;
125+
return ((GraphQLScalarType) type).getCoercing().parseLiteral(inputValue);
129126
}
130127
if (type instanceof GraphQLNonNull) {
131128
return coerceValueAst(((GraphQLNonNull) type).getWrappedType(), inputValue, variables);

src/main/java/graphql/execution/batched/BatchedExecutionStrategy.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ private void handlePrimitives(List<GraphQLExecutionNodeValue> values, String fie
210210
GraphQLType type) {
211211
for (GraphQLExecutionNodeValue value : values) {
212212
Object coercedValue = coerce(type, value.getValue());
213+
//6.6.1 http://facebook.github.io/graphql/#sec-Field-entries
214+
if (coercedValue instanceof Double && ((Double) coercedValue).isNaN()) {
215+
coercedValue = null;
216+
}
213217
value.getResultContainer().putResult(fieldName, coercedValue);
214218
}
215219
}

src/test/groovy/graphql/ScalarsQuerySchema.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ public class ScalarsQuerySchema {
2424
.type(Scalars.GraphQLBigDecimal)
2525
.staticValue(BigDecimal.valueOf(1234.0))
2626
.build())
27+
.field(newFieldDefinition()
28+
.name("doubleNaN")
29+
.type(Scalars.GraphQLFloat)
30+
.staticValue(Double.NaN)
31+
.build())
32+
.field(newFieldDefinition()
33+
.name("doubleNaNInput")
34+
.type(Scalars.GraphQLFloat)
35+
.argument(newArgument()
36+
.name("input")
37+
.type(new GraphQLNonNull(Scalars.GraphQLFloat))
38+
.build())
39+
.dataFetcher(new DataFetcher() {
40+
@Override
41+
public Object get(DataFetchingEnvironment environment) {
42+
return environment.getArgument("input");
43+
}
44+
})
45+
.build())
2746
.field(newFieldDefinition()
2847
.name("bigIntegerInput")
2948
.type(Scalars.GraphQLBigInteger)

src/test/groovy/graphql/ScalarsQueryTest.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql
22

3+
import graphql.execution.batched.BatchedExecutionStrategy
34
import spock.lang.Specification
45

56
class ScalarsQueryTest extends Specification {
@@ -47,4 +48,24 @@ class ScalarsQueryTest extends Specification {
4748
then:
4849
result == expected
4950
}
51+
52+
def 'Double NaN Not a Number '() {
53+
given:
54+
def query = """
55+
query DoubleNaN {
56+
doubleNaN
57+
}
58+
"""
59+
def expected = [
60+
doubleNaN: null
61+
]
62+
63+
when:
64+
def result = new GraphQL(ScalarsQuerySchema.scalarsQuerySchema).execute(query).data
65+
def resultBatched = new GraphQL(ScalarsQuerySchema.scalarsQuerySchema, new BatchedExecutionStrategy()).execute(query).data
66+
67+
then:
68+
result == expected
69+
resultBatched == expected
70+
}
5071
}

0 commit comments

Comments
 (0)