Search code examples
listgraphqlprismamutationresolver

Mutation with list of strings as argument in Prisma + GraphQL


I am trying to make a mutation but I am getting an error with the arguments.

In schema.graphql I have the following:

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
}

In 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[]
}

Then my mutation resolver is the following:

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,
  }
}

But when I try the function on GraphQL Playground for example with:

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

I get the following error:

{
  "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: '$2a$10$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"
      ]
    }
  ]
}

I don't know what I am doing wrong and I cannot find examples of Mutations with lists of strings.


Solution

  • It turns out I needed to change the mutation to the following:

    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,
      }
    }
    

    Basically use a set operation. I have no idea why, the documentation of Prisma is terrible. Maybe someone can clear things a bit?