Skip to content

Commit eee1ee8

Browse files
fix: Allows numeric values to be parsed as Bools
This is because MapCoder round-trips of Bools result in Number types. This is hard to avoid since it gets put into an NSObject container and there is no NSBool type.
1 parent 0dfa0d9 commit eee1ee8

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

Sources/GraphQL/Type/Scalars.swift

+5
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ public let GraphQLBoolean = try! GraphQLScalarType(
201201
if case let .bool(value) = inputValue {
202202
return inputValue
203203
}
204+
// NOTE: We deviate from graphql-js and allow numeric conversions here because
205+
// the MapCoder's round-trip conversion to NSObject for Bool converts to 0/1 numbers.
206+
if case let .number(value) = inputValue {
207+
return .bool(value.intValue != 0)
208+
}
204209
throw GraphQLError(
205210
message: "Boolean cannot represent a non boolean value: \(inputValue)"
206211
)

Tests/GraphQLTests/TypeTests/ScalarTests.swift

+6-12
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,12 @@ class ScalarTests: XCTestCase {
300300
GraphQLBoolean.parseValue(.null),
301301
"Boolean cannot represent a non boolean value: null"
302302
)
303-
try XCTAssertThrowsError(
304-
GraphQLBoolean.parseValue(0),
305-
"Boolean cannot represent a non boolean value: 0"
306-
)
307-
try XCTAssertThrowsError(
308-
GraphQLBoolean.parseValue(1),
309-
"Boolean cannot represent a non boolean value: 1"
310-
)
311-
try XCTAssertThrowsError(
312-
GraphQLBoolean.parseValue(.double(Double.nan)),
313-
"Boolean cannot represent a non boolean value: NaN"
314-
)
303+
// NOTE: We deviate from graphql-js and allow numeric conversions here because
304+
// the MapCoder's round-trip conversion to NSObject for Bool converts to 0/1 numbers.
305+
try XCTAssertNoThrow(GraphQLBoolean.parseValue(0))
306+
try XCTAssertNoThrow(GraphQLBoolean.parseValue(1))
307+
try XCTAssertNoThrow(GraphQLBoolean.parseValue(.double(Double.nan)))
308+
315309
try XCTAssertThrowsError(
316310
GraphQLBoolean.parseValue(""),
317311
#"Boolean cannot represent a non boolean value: """#

0 commit comments

Comments
 (0)