带有args的graphql查询不适用于用户ID

Posted

技术标签:

【中文标题】带有args的graphql查询不适用于用户ID【英文标题】:graphql query with args not working for user id 【发布时间】:2021-04-08 13:35:09 【问题描述】:

我被骗了。我最初创建了用户查询,它给了我我认为是语法错误的错误。但后来我为完美运行的车辆创建了一个相同的查询。我怀疑它与ID有关!类型,但我已经用完了线索。任何帮助将不胜感激!

这是我的 typedef 和解析器。

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

【问题讨论】:

如果您在帖子中提及错误,请同时分享该错误以帮助社区准确了解问题所在:) @JosephHall 哦,好吧!我会在那里添加完整的错误,谢谢 【参考方案1】:

Prisma 期望 Int:

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

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

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

【讨论】:

我不敢相信这是问题所在。我现在觉得很傻。所有的 Prisma 教程都使用这个 get Object by id 示例,所以我认为它比这更深。感谢您的帮助!

以上是关于带有args的graphql查询不适用于用户ID的主要内容,如果未能解决你的问题,请参考以下文章

Graphql 突变查询不适用于 express-graphql

graphQl突变查询不适用于变量

GraphQL 查询适用于 Gatsby 页面,但不适用于类组件

用户注册的 GraphQL 突变不适用于 jwt

Rails 低级缓存不适用于活动记录

GraphQL:每种类型的子字段与可以使用 args 过滤的根查询字段?