I've got some code that's generated (by SwiftGraphQL). I'd like to use it, but I'm bumping into an error message. I've tried to minimize the code; the following can be pasted into a playground:
public enum Objects {
public struct Query {}
public struct Starship {}
}
public final class Fields<TypeLock> {
public private(set) var data: Codable
public init(data: Codable) {
self.data = data
}
}
public struct Selection<T, TypeLock> {
private var decoder: (Fields<TypeLock>) throws -> T
public init(decoder: @escaping (Fields<TypeLock>) throws -> T) {
self.decoder = decoder
}
}
extension Selection where T == Never, TypeLock == Never {
public typealias Root<T> = Selection<T, Objects.Query>
}
extension Selection where T == Never, TypeLock == Never {
public typealias Starship<T> = Selection<T, Objects.Starship>
}
extension Fields where TypeLock == Objects.Starship {
public func name() throws -> String? {
return "X-Wing"
}
}
extension Fields where TypeLock == Objects.Query {
public func starship<T>(
id: String,
selection: Selection<T, Objects.Starship?>
) throws -> T {
return Objects.Starship() as! T
}
}
And this is my own code:
struct MyStarshipModel {
var name: String
init?(name: String?) {
guard let name else {
return nil
}
self.name = name
}
}
let starshipSelection = Selection.Starship(decoder: {
MyStarshipModel(name: try $0.name())
})
let query = Selection.Root {
$0.starship(id: "abcxyz", selection: starshipSelection)
}
The last stanza gives me the following error:
Cannot convert value of type 'Selection<MyStarshipModel?, Objects.Starship>' to expected argument type 'Selection<MyStarshipModel?, Objects.Starship?>'
I can't for the life of me, cast the starshipSelection such that it compiles. The difference is just one optional so it shouldn't be so hard, I'm hoping I'm just tired and overlook the obvious. What's the solution here?