我一直试图在 Prisma 网站上找到一些有关此问题的文档,但说实话,在那里找到非常详细的用例有点困难,特别是当问题像这个问题一样难以描述时。
我遇到的情况是,我的前端向createPosting我的 GraphQL-Yoga 服务器发送带有字段的突变请求positionTitle, employmentType, description, requirements, customId, expiresAt(我已经彻底测试过这是否按预期工作)。createdAt我想在 Prisma 服务上创建节点之前添加一个字段。
在我的 GraphQL-Yoga 服务器中,我有一个 datamodel.graphql,其中包含以下内容:
type Posting {
id: ID! @unique
customId: String! @unique
offeredBy: Employer!
postingTitle: String!
positionTitle: String!
employmentType: EmploymentType!
status: PostingStatus!
description: String
requirements: String
applications: [Application!]!
createdAt: DateTime!
expiresAt: DateTime!
}
Run Code Online (Sandbox Code Playgroud)
我的 schema.graphql 在突变下有这个:
createPosting(postingTitle: String!,
positionTitle: String!,
employmentType: String!,
description: String!,
requirements: String!,
customId: String!,
expiresAt: DateTime!,
status: PostingStatus): Posting!
Run Code Online (Sandbox Code Playgroud)
最后,在我的 createPosting 解析器中,我尝试像这样改变 Prisma 后端:
const result = await context.prisma.mutation.createPosting({
data: {
offeredBy: { connect: { name: context.req.name} },
postingTitle: args.postingTitle,
positionTitle: args.positionTitle,
employmentType: args.employmentType,
description: args.description,
requirements: args.requirements,
customId: args.customId,
createdAt: new Date().toISOString(),
expiresAt: expiresAt,
status: args.status || 'UPCOMING'
}
})
Run Code Online (Sandbox Code Playgroud)
当我尝试从前端运行此程序时,我在服务器上收到以下错误:
Error: Variable '$_v0_data' expected value of type 'PostingCreateInput!' but got: {"customId":"dwa","postingTitle":"da","positionTitle":"da","employmentType":"PART_TIME","status":"UPCOMING","description":"dada","requirements":"dadada","expiresAt":"2018-09-27T00:00:00.000Z","createdAt":"2018-09-04T20:29:10.745Z","offeredBy":{"connect":{"name":"NSB"}}}. Reason: 'createdAt' Field 'createdAt' is not defined in the input type 'PostingCreateInput'.
从这个错误消息中,我假设我的 Prisma 服务由于某种原因不知道createdAt,因为我最近添加了这个字段,但是当我检查 Prisma 主机上 GraphQL 游乐场中的 Posting 类型和 PostingCreateInput 时,我发现了该字段createdAt !在这两个地方。
我尝试删除生成的 prisma.graphql 并再次部署新文件,但这不起作用。当我检查 prisma.graphql 时,PostingCreateInput 确实错过了 createdAt 字段,尽管 Prisma 服务器似乎有它。
如果有人能给我指出错误的正确方向,或者让我更好地了解如何设置应该存储在数据库中但在我的瑜伽服务器中而不是在前端中创建的变量,我会非常感激:)
虽然这个问题可能看起来有点具体,但我相信在创建节点之前在服务器上为字段创建数据的想法应该是可能的,但目前我正在努力思考如何做到这一点。
太长了;在向我的 Prisma 服务发送创建请求之前,想要createdAt:DateTime在解析器上的 GraphQL-Yoga 服务器上创建一个字段。
好吧,经过大量尝试不同策略的工作后,我终于尝试将字段名称从 更改为createdAt,createdDate现在它可以工作了。
当我浏览 Playground 时,我发现这createdAt是 Prisma 本身在请求对查询进行排序时使用的半隐藏受保护字段。orderBy它可以在单个数据条目的参数列表中的选择下找到。
不过,错误消息Reason: 'createdAt' Field 'createdAt' is not defined in the input type 'PostingCreateInput'.肯定没有以任何方式为我指明正确的方向。
太长了;问题是我正在命名我的字段,createdAt这是一个受保护的字段名称。