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

graphql query with args not working for user id

我被蒙蔽了。我最初创建了用户查询,它给我的错误我认为是语法错误。但后来我为车辆创建了一个相同的查询,它运行得很好。我怀疑是跟身份证有关!类型,但我有 运行 条线索。如有任何帮助,我们将不胜感激!

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

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

//解析器//

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

//查询//

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

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

**这个按预期工作

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

//完全错误*//

{
  "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
  }
}

Prisma 需要 Int:

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

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

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