【问题标题】:React + Apollo "Getting Started" Error in Prisma PlaygroundPrisma Playground 中的 React + Apollo“入门”错误
【发布时间】:2021-06-17 21:04:36
【问题描述】:

我在 React + Apollo 入门章节:https://www.howtographql.com/react-apollo/1-getting-started/

当我在 Prisma Playground 中输入以下查询时(如教程告诉我的那样):

mutation CreatePrismaLink {
  post(
    description: "Prisma gives you a powerful database toolkit ????"
    url: "https://prisma.io"
  ) {
    id
  }
}

mutation CreateApolloLink {
  post(
    description: "The best GraphQL client for React"
    url: "https://www.apollographql.com/docs/react/"
  ) {
    id
  }
} 

我收到了我不理解的错误消息。好像是服务器的问题

  "errors": [
    {
      "message": "Argument id for data.postedBy.connect.id must not be null. Please use undefined instead.\n",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "post"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "clientVersion": "2.12.1",
          "stacktrace": [
            "Error: Argument id for data.postedBy.connect.id must not be null. Please use undefined instead.",
            "",
            "    at Document.validate (C:\\Users\\shanm\\hackernews-react-apollo\\server\\node_modules\\@prisma\\client\\runtime\\index.js:77413:19)",
            "    at NewPrismaClient._executeRequest (C:\\Users\\shanm\\hackernews-react-apollo\\server\\node_modules\\@prisma\\client\\runtime\\index.js:79065:17)",
            "    at C:\\Users\\shanm\\hackernews-react-apollo\\server\\node_modules\\@prisma\\client\\runtime\\index.js:79002:52",
            "    at AsyncResource.runInAsyncScope (async_hooks.js:197:9)",
            "    at NewPrismaClient._request (C:\\Users\\shanm\\hackernews-react-apollo\\server\\node_modules\\@prisma\\client\\runtime\\index.js:79002:25)",
            "    at Object.then (C:\\Users\\shanm\\hackernews-react-apollo\\server\\node_modules\\@prisma\\client\\runtime\\index.js:79119:39)",
            "    at processTicksAndRejections (internal/process/task_queues.js:93:5)"
          ]
        }
      }
    }
  ],
  "data": null
}

请帮我找出问题所在?

【问题讨论】:

    标签: reactjs apollo react-apollo prisma


    【解决方案1】:

    这是旧的,但以防其他人遇到这个问题并像我一样用谷歌搜索:

    不幸的是,这里的答案对我不起作用,但解决方案是 Node+GraphQL 教程中介绍的内容,您可能无法自己弄清楚:

    为了发帖,您必须先创建一个用户,然后获得该用户的授权。页面上列出了创建用户的突变作为示例,但不幸的是,作者在尝试创建帖子之前忘记指示读者运行它。

    1. 将其粘贴到 Playground 中以创建用户
    mutation {
      signup(name: "Sarah", email: "sarah@prisma.io", password: "graphql") {
        token
        user {
          id
        }
      }
    }
    
    1. 复制返回的token

    2. 打开左下方的“HTTP 标头”窗格

    3. 用你的令牌输入这个:

    {
      "Authorization": "Bearer TOKEN_HERE"
    }
    
    1. 现在您可以运行突变来创建帖子

    【讨论】:

      【解决方案2】:

      这是因为服务器在编写时考虑到了 Post 将始终属于 User 的业务规则。数据库在 Link 表上有一个 NOT NULL postedById 字段,即帖子将始终附加一个用户 ID。您需要在 Prisma ORM 模式的 Link 模型中使 postedById 字段为空。要解决此问题,请对服务器端代码进行以下更改并重新启动服务器

      1. 在服务器文件夹中转到schema.prisma 文件,并通过使用后缀将postedBy 和postedById 两个字段设为可选/可为空?

        postedBy?   User     @relation(fields: [postedById], references:[id])
        postedById? Int
        
      2. 然后运行以下命令以重新创建具有更改的数据库

        npx prisma migrate reset
        
      3. 更新Mutation.js file中的post解析器函数以替换该行

        postedBy: { connect: { id: userId } }
        

        postedById: userId,
        

        因为 prisma connect api 不接受空值

      【讨论】:

        猜你喜欢
        • 2018-10-20
        • 2017-10-19
        • 1970-01-01
        • 2022-01-27
        • 2018-12-27
        • 2021-07-24
        • 1970-01-01
        • 2017-03-25
        • 1970-01-01
        相关资源
        最近更新 更多