首页 > 解决方案 > 无法用字符串数组编写突变

问题描述

不能在带有列表标量类型的 graphql 游乐场中使用 set。

我已经创建了我的 Prisma datamodel.graphql 并部署了它。我的运动类型中的一个字段是运动列表。我使用列表标量类型来定义这个字段,并在我的 mutation.js 文件中编写了随附的突变。当我尝试在我的 graphql 操场上添加一个新练习时,我收到了这个错误。

{
  "data": null,
  "errors": [
    {
      "message": "Variable \"$_v0_data\" got invalid value {\"name\":\"split squat 3\",\"movement\":[\"push\",\"pull\"],\"liked\":false}; Field \"0\" is not defined by type ExerciseCreatemovementInput at value.movement.\nVariable \"$_v0_data\" got invalid value {\"name\":\"split squat 3\",\"movement\":[\"push\",\"pull\"],\"liked\":false}; Field \"1\" is not defined by type ExerciseCreatemovementInput at value.movement.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createExercise"
      ]
    }
  ]
}

这是我在 graphql 操场上写的突变。

mutation {
  createExercise(
    name: "split squat 3"
    movement: ["push", "pull"]
    liked: false
  ) {
    id
    name
  }
}

这是 datamodel.graphql 文件。

type User {
  id: ID! @unique
  name: String!
}

# model for exercises
type Exercise {
  id: ID! @unique
  name: String!
  movement: [String!]!
  liked: Boolean
  video: String
}

当我编写没有任何字符串的突变时,只有一个像这样的空数组,

mutation {
  createExercise(
    name: "split squat 3"
    movement: []
    liked: false
  ) {
    id
    name
  }
}

一切正常,练习被添加为一个新节点。

当我尝试用这样的集合编写突变时,

mutation {
  createExercise(
    name: "split squat 3"
    movement: {set: ["push","pull"]}
    liked: false
  ) {
    id
    name
  }
}

我也得到一个错误。

我可以登录 prisma.io 并通过手动创建新节点将字符串添加到数组中。

我不确定为什么我不能在我的突变中添加字符串列表。=

标签: javascriptgraphqlprisma-graphql

解决方案


您必须像在 prisma 模式中一样创建单独的输入。

input ExcerciseCreatemovementInput{
  set: [String!]
}

推荐阅读