Skip to content

CustomScalarType JSON example using AnyHashable fails due to Sendable requirement in v2 #3596

@AlexPan1992

Description

@AlexPan1992

Question

extension MySchema {
    public enum CustomJSON: CustomScalarType, Hashable {
        case dictionary([String: AnyHashable])
        case array([AnyHashable])

        public init(_jsonValue value: JSONValue) throws {
            if let dict = value as? [String: AnyHashable] {
                self = .dictionary(dict)
            } else if let array = value as? [AnyHashable] {
                self = .array(array)
            } else {
                throw JSONDecodingError.couldNotConvert(value: value, to: CustomJSON.self)
            }
        }

        public var _jsonValue: JSONValue {
            switch self {
            case let .dictionary(json as AnyHashable),
                 let .array(json as AnyHashable):
                return json
            }
        }

        public static func == (lhs: CustomJSON, rhs: CustomJSON) -> Bool {
            lhs._jsonValue == rhs._jsonValue
        }

        public func hash(into hasher: inout Hasher) {
            hasher.combine(_jsonValue)
        }
    }
}

https://www.apollographql.com/docs/ios/custom-scalars#json-and-other-custom-scalars-with-multiple-return-types

CustomScalarType & JSONValue now requires conforming types to be Sendable, and AnyHashable cannot conform to Sendable.
This makes the documented example unusable in practice. A note or an updated example using a Sendable-compatible type would be very helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that have a question which should be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions