带参数的 graphql 查询不适用于用户 ID

Ani*_*i T 3 graphql prisma prisma-graphql prisma2

我被迷惑了。我最初创建了用户查询,它给了我错误,我认为是语法错误。但后来我创建了一个相同的车辆查询,效果非常好。我怀疑这和ID有关!类型,但我已经没有线索了。任何帮助,将不胜感激!

这是我的类型定义和解析器。

//类型定义//

   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
    }
Run Code Online (Sandbox Code Playgroud)

//解析器//

   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,
          }
        })
      }
   
Run Code Online (Sandbox Code Playgroud)

//询问//

**这个是损坏的,并且有错误:在 prisma.findOneUser 上得到无效值“1”。提供了字符串,预期为 Int **我尝试过这样做id: "1"并且user(where: {id: 1})

query {
  user(id:1){
    id
    fname
  }
}
Run Code Online (Sandbox Code Playgroud)

**这个按预期工作

query {
  vehicle(vin:"123123123"){
    vin
    make
  }
}
Run Code Online (Sandbox Code Playgroud)

//完全错误*//

{
  "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
  }
}
Run Code Online (Sandbox Code Playgroud)

pza*_*ger 8

Prisma 期望Int

“参数 id:在 prisma.findOneUser 上得到无效值“1”。提供的字符串,预期为 Int。”,

因此你需要转换id为一个数字。也许是这样的:

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