swift - 无法推断 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)
}
}
}
我究竟做错了什么?
解决方案
似乎在模式上设置此字段的正确方法是:
Type(User.self) {
Field("id", at: \.id)
Field("name", at: \.name)
Field("record", at: \.$record, as: TypeReference<Record>?.self)
}
推荐阅读
- laravel - Eloquent 模型 getRawOriginal() 在“更新”观察者中返回新值
- javascript - 'vue-stars' 星星没有加载到视图中
- sql - 如何在 Postgres 中使用 ON CONFLICT UPDATE 子句插入多条记录?
- html - 电子邮件设计:背景图片上的文字
- javascript - 在 MongoDB 中保持文本格式
- .net - SQL Server Mgmt Studio - 无法加载文件或程序集“System.ValueTuple”
- php - 正确重新生成会话 ID
- java - 我无法从一位客户那里获得消息以显示给另一位客户
- excel - 知道如何在 Excel 中重新排列此数据集
- time-series - 使用 SVR 预测股票价格:基于时间序列的问题