2

我正在尝试使用在 GraphQL-yoga 服务器上运行的 Prisma 在 graphql 中设置 updateNode 突变。这是我尝试运行突变时收到的错误:

"变量\"$_v0_data\" 的值无效{ 数据:{ 名称:\"Test\" },其中:{ id: \"cjqulnr0yftuh0a71sdkek697\" } };字段 \"data\" 未由 CocktailUpdateInput 类型定义。 \n变量 \"$_v0_data\" 获得无效值 { data: { name: \"Test\" },其中:{ id: \"cjqulnr0yftuh0a71sdkek697\" } };字段 \"where\" 未由 CocktailUpdateInput 类型定义。 "

这是我的突变解析器:

const Mutation = {
  async updateCocktail(parent, args, ctx, info) {
    const data = { ...args };
    delete data.id;
    const where = {id: args.id};
    return await ctx.db.mutation.updateCocktail({ data, where }, info);
  },
}

数据模型.棱镜:

type Cocktail {
  id: ID! @unique
  name: String!
  info: String
  glass: Glass
  ingredients: [Ingredient]
  steps: [Step]
}

架构.graphql

type Mutation {
  updateCocktail(data: CocktailUpdateInput!, where: CocktailWhereUniqueInput!): Cocktail
}

最后这是我要在操场上执行的操作:

mutation{
  updateCocktail(
    where: {id: "cjqulnr0y0tuh0a71sdkek697"},
    data: {
      name: "Test"
    }
  ){
    id
    name
  }
}
4

2 回答 2

1

如果我正确阅读了您的解析器,您的解析器会执行以下操作:

  • 获取 args 并将它们放入数据中(没有 id)
  • 把 args 中的 id 放到 where

但是,在操场上,您提供以下参数:

args = {
  where: {id: "cjqulnr0y0tuh0a71sdkek697"},
  data: {
    name: "Test"
  }
}

他们已经成型了!这意味着您的解析器将执行以下步骤并构建以下变量:

data = {
  where: {id: "cjqulnr0y0tuh0a71sdkek697"},
  data: {
    name: "Test"
  }
}

where = { id: null }

您可以通过两种方式解决此问题:

1/不要重建数据和解析器的位置,只需将参数传递给棱镜

2/当调用你的突变时,给它如下参数:

updateCocktail(id: "abc", name: "Test") {...}
于 2019-01-25T13:13:28.740 回答
0

According to your error, the problem should lie in your playground execution. It is taking your "where" and "data" as data types.

You could try doing something more like this:

 mutation UpdateCocktailMutation($data: CocktailUpdateInput!, $where: CocktailWhereUniqueInput!) {
    updateCocktail(data: $data, where: $where) {
      id
      name
    }
  }

and in your bottom of the playground they have a query variable field. Fill it will your variable data. Do correct my case sensitivity and naming conventions as I may have missed out on parts of it.

于 2019-01-25T10:59:06.250 回答