prisma - prisma2 订阅返回数据:null
问题描述
我有一个基本的 pubsub 使用样板和 graphql-yoga 在这里工作: https://github.com/ryanking1809/prisma2_subscriptions https://codesandbox.io/s/github/ryanking1809/prisma2_subscriptions/tree/sql-lite
使用发布突变:
const Mutation = objectType({
name: 'Mutation',
definition(t) {
//...
t.field('publish', {
type: 'Post',
nullable: true,
args: {
id: idArg(),
},
resolve: async (parent, { id }, ctx) => {
const post = await ctx.photon.posts.update({
where: { id },
data: { published: true },
include: { author: true }
});
ctx.pubsub.publish("PUBLISHED_POST", {
publishedPost: post
});
return post
},
})
},
})
和订阅 - 我只是回来true
确保withFilter
(来自graphql-yoga
)工作。
const Subscription = objectType({
name: "Subscription",
definition(t) {
t.field("publishedPostWithEmail", {
type: "Post",
args: {
authorEmail: stringArg({ required: false })
},
subscribe: withFilter(
(parent, { authorEmail }, ctx) => ctx.pubsub.asyncIterator("PUBLISHED_POST"),
(payload, { authorEmail }) => true
)
});
}
});
返回以下内容publish
(您可以将这些复制并粘贴到代码和框中 - 这很整洁!)
mutation {
publish(
id: "cjzwz39og0000nss9b3gbzb7v"
) {
id,
title,
author {
email
}
}
}
subscription {
publishedPostWithEmail(authorEmail:"prisma@subscriptions.com") {
title,
content,
published
}
}
{
"errors": [
{
"message": "Cannot return null for non-nullable field Subscription.publishedPostWithEmail.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"publishedPostWithEmail"
]
}
],
"data": null
}
由于某种原因,它正在返回data: null
。当我登录payload.publishedPosts
过滤器功能时,似乎一切都在那里。
{ id: 'cjzwqcf2x0001q6s97m4yzqpi',
createdAt: '2019-08-29T13:34:26.648Z',
updatedAt: '2019-08-29T13:54:19.479Z',
published: true,
title: 'Check Author',
content: 'Do you save the author?',
author:
{ id: 'sdfsdfsdfsdf',
email: 'prisma@subscriptions.com',
name: 'Prisma Sub' } }
有什么我想念的吗?
解决方案
终于想通了!
订阅函数需要以 pubsub 中的 key 命名。因此,如果您有如下发布功能:
ctx.pubsub.publish("PUBLISHED_POST", {
publishedPost: post
});
那么你必须命名你的订阅publishedPost
t.field("publishedPost", {
type: "Post",
args: {
authorEmail: stringArg({ required: false })
},
subscribe: withFilter(
(parent, { authorEmail }, ctx) =>
ctx.pubsub.asyncIterator("PUBLISHED_POST"),
(payload, { authorEmail }) => payload.publishedPost.author.email === authorEmail
)
});
如果您为订阅命名,publishedPostWithEmail
则不会返回任何数据
t.field("publishedPostWithEmail", {
//...
});
有趣的是,如果你有 2 把钥匙
ctx.pubsub.publish("PUBLISHED_POST", {
publishedPost2: post,
publishedPost3: post
});
然后,如果您命名您的订阅,publishedPost2
则publishedPost3
结果中会省略。
奇怪的是,如果您订阅 2 条消息,您将获得所有数据
ctx.pubsub.publish("PUBLISHED_POST", {
publishedPost: post,
publishedPost2: post
});
ctx.pubsub.publish("PUBLISHED_POST_X", {
publishedPostX: post,
publishedPostY: post
});
ctx.pubsub.asyncIterator([
"PUBLISHED_POST",
"PUBLISHED_POST_X"
]),
返回publishedPost
, publishedPost2
, publishedPostX
,publishedPostY
因此,您可以通过使用单个项目订阅数组来解决上述问题,并且订阅的名称变得无关紧要。
t.field("publishedPostXYZ", {
type: "Post",
args: {
authorEmail: stringArg({ required: false })
},
subscribe: withFilter(
(parent, { authorEmail }, ctx) =>
ctx.pubsub.asyncIterator([
"PUBLISHED_POST"
]),
(payload, { authorEmail }) => {
return payload.publishedPost.author.email === authorEmail;
}
)
});
所以看起来这可能是一个错误
推荐阅读
- c# - 如何将此 json 转换为字符串并获取“id”属性的值
- mysql - Spring Boot 应用程序与 Docker mysql 服务器用户连接
- php - PHP无法从数据库中获取更大的数据
- visual-studio-code - VS代码在鼠标不在时模糊
- c# - 启用 VPN 后,外部 IP 地址不会改变
- sql - 从 SQL 表中选择标记为 X 或 Y 的记录,但不选择同时具有 X 和 Y 的记录
- mysql - 如何在 SQL 条件下包含一些数据和其他数据?
- nativescript - Nativescript-Vue ScrollView 可滚动高度始终为 0。我的目标是将其滚动到底部,因为它是一个聊天应用程序
- javascript - 将应用程序推送到实时服务器时 Heroku Fatal
- amazon-web-services - SageMaker - 为 ECR 指定什么 IAM 权限?