首页 > 解决方案 > GraphQL 突变抛出错误

问题描述

更新

我已经缩小到引发错误的位置,这似乎是一个Prisma问题。

错误发生在投票功能中:

const linkExists = await context.db.exists.Vote({
    user: { id: userId },
    link: { id: args.linkId }
})

原来的

我正在学习本教程

我正在尝试为帖子“投票”,但会引发以下错误:

{
  "data": {
    "vote": null
  },
  "errors": [
    {
      "message": "Variable '$_v0_where' cannot be non input type 'VoteWhereInput'. 
                  (line 1, column 20):\nquery ($_v0_where: VoteWhereInput) 
                  {\n                   ^",
      "locations": [],
      "path": [
        "votes"
      ]
    }
  ]
}

我发送的突变是:

mutation{
  vote(linkId:"cjit726cxfkpj0a21z74nuncu"){
    id
  }
}

不完全确定此错误的含义。我一直在将我的代码与Github 上的官方 repo进行比较,但我找不到区别。

这是我的投票功能:

async function vote(parent, args, context, info) {
  const userId = getUserId(context)
  const linkExists = await context.db.exists.Vote({
    user: { id: userId },
    link: { id: args.linkId },
  })
  if (linkExists) {
    throw new Error(`Already voted for link: ${args.linkId}`)
  }

  return context.db.mutation.createVote(
    {
      data: {
        user: { connect: { id: userId } },
        link: { connect: { id: args.linkId } },
      },
    },
    info,
  )
}

当我注销时,args它显示:

query:
query ($_v0_where: VoteWhereInput) {
  votes(where: $_v0_where) {
    id
  }
}
operationName: null
variables:
{
  "_v0_where": {
    "link": {
      "id": "cjit5v46i2r3y0a21a7ez0t9p"
    },
    "user": {
      "id": "cjis44x27gbn60a219abasvjz"
    }
  }
}

我创建了一个新用户,与该用户创建了一个帖子(两者都工作正常),但是当我尝试以该用户的身份投票时,我被困在这里。

datamodel.schema如果你愿意,我的:

type Link {
  id: ID! @unique
  description: String!
  url: String!
  postedBy: User
  votes: [Vote!]!
}

type User {
  id: ID! @unique
  name: String!
  email: String! @unique
  password: String!
  links: [Link!]!
  votes: [Vote!]!
}

type Vote {
  id: ID! @unique
  link: Link!  
  user: User!
}

标签: javascriptgraphqlprismaprisma-graphql

解决方案


这是因为 的结构db.exists 需要发送包装在where字段中的参数。

在此处输入图像描述 看起来文档已经过时,也会更新它们


推荐阅读