为 GraphQL Yoga 提供但在解析器中不需要的 Prisma 声明字段的正确方法

Correct way to declare fields for Prisma provided by GraphQL Yoga but not required in resolver

我一直试图在 Prisma 网站上找到一些关于此的文档,但老实说,在那里找到非常详细的用例有点困难,尤其是当问题像这个问题一样难以描述时。

我遇到这样一种情况,我的前端向我的 GraphQL-Yoga 服务器上的 createPosting 发送了一个带有字段 positionTitle, employmentType, description, requirements, customId, expiresAt 的突变请求(我已经彻底测试过它按预期工作)。我想在 Prisma 服务上创建节点之前添加一个 createdAt 字段。

在我的 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!
}

我的 schema.graphql 在 Mutations 下有这个:

createPosting(postingTitle: String!,
    positionTitle: String!,
    employmentType: String!,
    description: String!,
    requirements: String!,
    customId: String!,
    expiresAt: DateTime!,
    status: PostingStatus): Posting!

最后,在我的 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'
    }
  })

当我尝试从我的前端运行这个时,我在服务器上收到以下错误:

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 playground 中检查类型 Posting 和 PostingCreateInput 时我找到了 createdAt 字段!在这两个地方。

我尝试删除生成的 prisma.graphql 并再次部署新文件,但这没有用。当我检查 prisma.graphql 时,PostingCreateInput 确实错过了 createdAt 字段,即使 Prisma 服务器似乎有它。

如果有人能指出我正确的方向,或者让我更好地了解如何设置应该存储在数据库中但在我的 Yoga-server 中创建的变量,而不是在前端我会非常感激:)

虽然这个问题可能看起来有点具体,但我相信在创建节点之前在服务器上为字段创建数据的想法应该是可行的,但目前我正在努力思考如何做它。

TLDR;在向我的 Prisma 服务发送创建请求之前,想在我的 GraphQL-Yoga 服务器上的解析器上创建一个 createdAt:DateTime 字段。

好的,在尝试了很多不同的策略之后,我终于尝试将字段的名称从 createdAt 更改为 createdDate,现在可以使用了。

当我浏览 Playground 时,我发现 createdAt 是 Prisma 本身在请求对查询进行排序时使用的半隐藏受保护字段。它可以在单个数据条目的参数列表中的 orderBy 选择下找到。

错误消息 Reason: 'createdAt' Field 'createdAt' is not defined in the input type 'PostingCreateInput'. 当然没有以任何方式为我指明正确的方向。

TLDR;问题是我正在命名我的字段 createdAt 这是一个受保护的字段名称。