Ani T
Ani T

Reputation: 65

graphql query with args not working for user id

I am bamboozled. I initially created the user query and it was giving me errors that I assumed were syntax errors. But then I created an identical query for vehicles which works perfectly. I have a suspicion that it's related to the ID! type but I have run out of leads. Any help would be appreciated!

Here are my typedefs and resolvers.

//TYPEDEFS//

   type User {
      id: ID!
      fname: String
      lname: String
      email: String
      password: String
      vehicles: [Vehicle]
    }

    type Vehicle {
      id: ID!
      vin: String
      model: String
      make: String
      drivers: [User]
    }

    type Query {
      users: [User]
      user(id: ID!): User
      vehicles: [Vehicle]
      vehicle(vin: String): Vehicle
    }

//RESOLVERS//

   user: async (parent, args, context) => {
        const { id } = args
        return context.prisma.user.findUnique({
          where: {
            id,
          },
        })
      },
   vehicle: async (parent, args, context) => {
        const { vin } = args
        return context.prisma.vehicle.findUnique({
          where: {
            vin,
          }
        })
      }
   

//QUERY//

**This one is the broken one and has the error: Got invalid value '1' on prisma.findOneUser. Provided String, expected Int **I've tried doing id: "1" and user(where: {id: 1})

query {
  user(id:1){
    id
    fname
  }
}

**This one works as intended

query {
  vehicle(vin:"123123123"){
    vin
    make
  }
}

//FULL ERROR*//

{
  "errors": [
    {
      "message": "\nInvalid `prisma.user.findUnique()` invocation:\n\n{\n  where: {\n    id: '1'\n        ~~~\n  }\n}\n\nArgument id: Got invalid value '1' on prisma.findOneUser. Provided String, expected Int.\n\n",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "user"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "clientVersion": "2.13.1",
          "stacktrace": [
            "Error: ",
            "Invalid `prisma.user.findUnique()` invocation:",
            "",
            "{",
            "  where: {",
            "    id: '1'",
            "        ~~~",
            "  }",
            "}",
            "",
            "Argument id: Got invalid value '1' on prisma.findOneUser. Provided String, expected Int.",
            "",
            "",
            "    at Document.validate (/home/atran/workspace/m4m/m4m_server/node_modules/@prisma/client/runtime/index.js:76090:19)",
            "    at NewPrismaClient._executeRequest (/home/atran/workspace/m4m/m4m_server/node_modules/@prisma/client/runtime/index.js:77796:17)",
            "    at resource.runInAsyncScope (/home/atran/workspace/m4m/m4m_server/node_modules/@prisma/client/runtime/index.js:77733:52)",
            "    at AsyncResource.runInAsyncScope (async_hooks.js:188:21)",
            "    at NewPrismaClient._request (/home/atran/workspace/m4m/m4m_server/node_modules/@prisma/client/runtime/index.js:77733:25)",
            "    at Object.then (/home/atran/workspace/m4m/m4m_server/node_modules/@prisma/client/runtime/index.js:77850:39)",
            "    at process._tickCallback (internal/process/next_tick.js:68:7)"
          ]
        }
      }
    }
  ],
  "data": {
    "user": null
  }
}

Upvotes: 3

Views: 6045

Answers (1)

pzaenger
pzaenger

Reputation: 11973

Prisma expects an Int:

"Argument id: Got invalid value '1' on prisma.findOneUser. Provided String, expected Int.",

Therefore you need to cast id to a number. Maybe something like this:

user: async (parent, args, context) => {
  const id = +args.id;
  return context.prisma.user.findUnique({
    where: { id }
  });
}

Upvotes: 8

Related Questions