首页 > 解决方案 > 无法推断 Vapor GraphQL 架构字段通用参数“ChildType”

问题描述

我已经开始使用 Vapor 作为 GraphQLKit 和 Graphiti 的 BE 框架,并且我正在尝试定义我的模型对象和架构,但是Generic parameter 'ChildType' could not be inferred在尝试将可选子关系添加为架构字段时出现错误。

用户模型:

public final class User: Model {
    public static let schema = "users"
    
    @ID(key: .id)
    public var id: UUID?
    
    @OptionalField(key: "name")
    public var name: String?

    //MARK: Relations
    @OptionalChild(for: \.$user)
    public var record: Record?

    public init() { }

    public init(id: UUID? = nil,
                about: String? = nil) {
        self.id = id
        self.name = name
    }
}

记录模型:

public final class Record: Model {
    public static let schema = "records"
    
    @ID(key: .id)
    public var id: UUID?
    
    @Field(key: "type")
    public var type: Int
    
    //MARK: Relations
    @OptionalParent(key:"user_id")
    public var user: User?

    public init() { }

    public init(id: UUID? = nil,
                type: Int,
                userId: UUID? = nil) {
        self.id = id
        self.type = type
        self.$user.id = userId
    }
}

方案:

let schema = try! Schema<Resolver, Request> {
    Scalar(UUID.self).description("Unique ID Type")
    
    Type(User.self) {
        Field("id", at: \.id)
        Field("name", at: \.name)
        
        Field("record", with: \.$record) ***Generic parameter 'ChildType' could not be inferred***
    }
    
    Type(Record.self) {
        Field("id", at: \.id)
        Field("type", at: \.type)
        
        Field("user", with: \.$user)
    }
    
    Query {
        Field("user", at: Resolver.getUser) {
            Argument("id", at: \.id)
        }
    }
}

我究竟做错了什么?

标签: swiftgenericsgraphqlfluentvapor

解决方案


似乎在模式上设置此字段的正确方法是:

Type(User.self) {
    Field("id", at: \.id)
    Field("name", at: \.name)
        
    Field("record", at: \.$record, as: TypeReference<Record>?.self)
} 

推荐阅读