在 Prisma + GraphQL 中以字符串列表作为参数的变异

Mutation with list of strings as argument in Prisma + GraphQL

我正在尝试进行更改,但我收到参数错误。

schema.graphql中我有以下内容:

type User {
  id: ID!
  name: String!
  email: String!
  age: Int!
  height: Float!
  weight: Float!
  goals: [String!]!
  history: [Routine!]!
}

type Mutation {
  signup(email: String!, password: String!, name: String!, age: Int!, height: Float!, weight: Float!, goals: [String!]!): AuthPayload
}

schema.prisma:

model User {
  id        Int      @id @default(autoincrement())
  name      String
  email     String   @unique
  password  String
  age       Int
  weight    Float
  height    Float
  goals     String[]
  history   Routine[]
}

然后我的突变解析器如下:

async function signup(parent, args, context, info) {
  // 1
  const password = await bcrypt.hash(args.password, 10)

  // 2
  const user = await context.prisma.user.create({ data: { ...args, password } })

  // 3
  const token = jwt.sign({ userId: user.id }, APP_SECRET)

  // 4
  return {
    token,
    user,
  }
}

但是当我在 GraphQL Playground 上尝试该功能时,例如:

mutation {
  signup(
    name: "Esteban"
    email: "esteban@gmail.com"
    password: "Password"
    age: 23
    height: 1.84
    weight: 70.0
    goals: ["WEIGHT_LOSS"]
  ) {
    token
    user {
      id
    }
  }
}

我收到以下错误:

{
  "data": {
    "signup": null
  },
  "errors": [
    {
      "message": "\nInvalid `context.prisma.user.create()` invocation in\n/Users/estebankramer1/Documents/ITBA/pf-proyf/server/src/resolvers/Mutation.js:10:42\n\n   6 // 1\n   7 const password = await bcrypt.hash(args.password, 10)\n   8 \n   9 // 2\n→ 10 const user = await context.prisma.user.create({\n       data: {\n         email: 'esteban@gmail.com',\n         password: 'a$hL1sMErBEcg98hpk5kRNpef2isI5d/O66zfRom8sQyThIHue7ku2O',\n         name: 'Esteban',\n         age: 23,\n         height: 1.84,\n         weight: 70,\n         goals: [\n           'WEIGHT_LOSS'\n         ]\n       }\n     })\n\nUnknown arg `0` in data.goals.0 for type UserCreategoalsInput. Available args:\n\ntype UserCreategoalsInput {\n  set?: List<String>\n}\n\n",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "signup"
      ]
    }
  ]
}

我不知道我做错了什么,我找不到带有字符串列表的突变示例。

事实证明我需要将突变更改为以下内容:

async function signup(parent, args, context, info) {
  // 1
  const password = await bcrypt.hash(args.password, 10)

  // 2
  const user = await context.prisma.user.create({ data: {
    ...args,
      goals: {
        set: args.goals
      },
      password
  } })

  // 3
  const token = jwt.sign({ userId: user.id }, APP_SECRET)

  // 4
  return {
    token,
    user,
  }
}

基本上使用集合操作。 我不知道为什么,Prisma 的文档很糟糕。也许有人可以把事情弄清楚一点?