GraphQLSwift / GraphQL

The Swift GraphQL implementation for macOS and Linux
MIT License
938 stars 72 forks source link

0 default value for argument is interpreted as false and can not be used #70

Closed Igor-Palaguta closed 4 years ago

Igor-Palaguta commented 4 years ago

We have in application argument with default value 0 "offset": GraphQLArgument(type: GraphQLInt, defaultValue: 0),

If this argument is not passed, then instead of integer 0 boolean false is used. So arguments["offset"].int returns nil

In the screenshot you can see that Any 0 is casted to Bool and is interpreted as false

It happens as try JSONSerialization.jsonObject(with: data, options: .allowFragments) returns NSNumber, and 0 is interpreted as false (https://developer.apple.com/documentation/foundation/nsnumber/1410865-boolvalue)

Screenshot 2020-10-09 at 01 04 55

GraphQL 1.1.7 Xcode Version 12.0 (12A7209)

Igor-Palaguta commented 4 years ago

For proper detection what value is hidden under NSNumber can be used code from https://stackoverflow.com/a/30223989/2035054

func isBoolNumber(num: NSNumber) -> Bool
{
    let boolID = CFBooleanGetTypeID() // the type ID of CFBoolean
    let numID = CFGetTypeID(num) // the type ID of num
    return numID == boolID
}

CFBooleanGetTypeID is already used in several places in GraphQL

Igor-Palaguta commented 4 years ago

Fixed here https://github.com/GraphQLSwift/GraphQL/pull/71