如何解决在 Prisma Playground 中 React + Apollo“入门”错误
我在 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
}
请帮我找出问题?
解决方法
那是因为在编写服务器时考虑到了一个 Post 永远属于一个用户的业务规则。数据库在 Link 表上有一个 NOT NULL PostedById 字段,即一个帖子将始终附加一个用户 ID。您需要在 Prisma ORM 模式中的 Link 模型中使 PostedById 字段可以为空。要解决此问题,请对服务器端代码进行以下更改并重新启动服务器
-
在服务器文件夹中,转到 schema.prisma 文件,并通过添加 ?
postedBy? User @relation(fields: [postedById],references:[id]) postedById? Int
-
然后运行以下命令以使用更改重新创建数据库
npx prisma migrate reset
-
更新 Mutation.js file 中的 post 解析器函数以替换该行
postedBy: { connect: { id: userId } }
与
postedById: userId,
因为prisma connect api 不接受空值
这是旧的,但以防其他人遇到这个问题并且像我一样在谷歌上搜索它:
不幸的是,这里的答案对我不起作用,但解决方案是 Node+GraphQL 教程中涵盖的内容,您可能无法自行弄清楚:
为了发帖,您必须先创建一个用户,然后被授权为该用户。页面上列举了创建用户的mutation作为示例,可惜作者在尝试创建帖子之前忘记指示读者运行它。
- 将其粘贴到 Playground 中以创建用户
mutation {
signup(name: "Sarah",email: "sarah@prisma.io",password: "graphql") {
token
user {
id
}
}
}
-
复制返回的
token
-
打开左下角的“HTTP Headers”面板
-
用您的令牌输入:
{
"Authorization": "Bearer TOKEN_HERE"
}
- 现在您可以运行更改来创建帖子
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。