prisma2 订阅 returns 数据:空
prisma2 subscriptions returns data: 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
上返回以下内容(您可以将它们复制并粘贴到 codesandbox - 这很简洁!)
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"
]),
returns 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;
}
)
});
看来这可能是一个错误
我有一个基本的 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
上返回以下内容(您可以将它们复制并粘贴到 codesandbox - 这很简洁!)
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"
]),
returns 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;
}
)
});
看来这可能是一个错误