Skip to content

Commit

Permalink
Merge pull request #156 from NeedleInAJayStack/fix/bool-parseValue
Browse files Browse the repository at this point in the history
Allows numeric values to be parsed as GraphQLBools
  • Loading branch information
NeedleInAJayStack authored Oct 24, 2024
2 parents 0dfa0d9 + eee1ee8 commit ec809df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
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

0 comments on commit ec809df

Please sign in to comment.