Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/GraphQL/Type/Scalars.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public let GraphQLBoolean = try! GraphQLScalarType(
if case let .bool(value) = inputValue {
return inputValue
}
// NOTE: We deviate from graphql-js and allow numeric conversions here because
// the MapCoder's round-trip conversion to NSObject for Bool converts to 0/1 numbers.
if case let .number(value) = inputValue {
return .bool(value.intValue != 0)
}
throw GraphQLError(
message: "Boolean cannot represent a non boolean value: \(inputValue)"
)
Expand Down
18 changes: 6 additions & 12 deletions Tests/GraphQLTests/TypeTests/ScalarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,18 +300,12 @@ class ScalarTests: XCTestCase {
GraphQLBoolean.parseValue(.null),
"Boolean cannot represent a non boolean value: null"
)
try XCTAssertThrowsError(
GraphQLBoolean.parseValue(0),
"Boolean cannot represent a non boolean value: 0"
)
try XCTAssertThrowsError(
GraphQLBoolean.parseValue(1),
"Boolean cannot represent a non boolean value: 1"
)
try XCTAssertThrowsError(
GraphQLBoolean.parseValue(.double(Double.nan)),
"Boolean cannot represent a non boolean value: NaN"
)
// NOTE: We deviate from graphql-js and allow numeric conversions here because
// the MapCoder's round-trip conversion to NSObject for Bool converts to 0/1 numbers.
try XCTAssertNoThrow(GraphQLBoolean.parseValue(0))
try XCTAssertNoThrow(GraphQLBoolean.parseValue(1))
try XCTAssertNoThrow(GraphQLBoolean.parseValue(.double(Double.nan)))

try XCTAssertThrowsError(
GraphQLBoolean.parseValue(""),
#"Boolean cannot represent a non boolean value: """#
Expand Down
Loading