I have a basic pubsub working here using the boilerplate and graphql-yoga: https://github.com/ryanking1809/prisma2_subscriptions https://codesandbox.io/s/github/ryanking1809/prisma2_subscriptions/tree/sql-lite
With a publish mutation:
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
},
})
},
})
And a subscription - I'm just returning true
to make sure withFilter
(from graphql-yoga
) is working.
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
)
});
}
});
Returning the following on publish
(you can copy and paste these into codesandbox - which is neat!)
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
}
For some reason, it's returning data: null
. When I log payload.publishedPosts
in the filter function it seems as though everything is there.
{ 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' } }
Is there something I'm missing?
Finally got it figured out! 🙌
The subscription function needs to be named after the key in the pubsub. So if you have a publish function like the following:
ctx.pubsub.publish("PUBLISHED_POST", {
publishedPost: post
});
then you have to name your subscription 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
)
});
if you name your subscription publishedPostWithEmail
then no data is returned
t.field("publishedPostWithEmail", {
//...
});
Interestingly, if you have 2 keys
ctx.pubsub.publish("PUBLISHED_POST", {
publishedPost2: post,
publishedPost3: post
});
Then if you name your subscription publishedPost2
then publishedPost3
is ommited from the results.
Oddly if you subscribe to 2 messages you get back all the data
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
So you can get around the above issue by subscribing to an array with a single item and the name of the subscription becomes irrelevant.
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;
}
)
});
So seems like this might be a bug 🤔