-
Notifications
You must be signed in to change notification settings - Fork 574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[integrations/fastify] Add documentation to enable SSE subscriptions #3276
Comments
I ended up building this plugin to handle subscriptions in my Fastify setup import { EOL } from 'os'
export const useSubscription = () => {
return {
async onSubscribe () {
return {
async onSubscribeResult ({
result,
args: {
contextValue: { req, reply }
}
}) {
req.socket.on('end', () => {
result.return()
reply.hijack() // tell Fastify to skip the automatic invocation of reply.send()
})
req.socket.on('close', () => {
reply.log.info(
{
res: reply,
responseTime: reply.elapsedTime
},
'request completed'
)
reply.raw.end() // tell the server that all of the response headers and body have been sent
})
for await (const data of result) {
reply.raw.write(
`event: next${EOL}data: ${JSON.stringify(data)}${EOL}${EOL}`
)
}
}
}
}
}
} Hopefully this is useful to other devs as well. |
GraphQL Yoga doesn't need an extra plugin for SSE except single connection mode. You can see Fastify tests below that use the same path for subscriptions. Maybe you can help us reproducing your issue here; I'd not use that kind of plugin which bypasses entire Yoga plugin hooks, and it might cause an unexpected behavior. |
You are absolutely right. The issue I had initially was related to how I was setting an initial value for my subscription. After migrating to Yoga the Now I spent more time into this and figured all this out. Repeater.merge([
initialData,
pubSub.subscribe(topic)
]) I think I would probably add something to the documentation that documents simple subscriptions without any need for the SSE plugin. Maybe make it more explicit. |
Thank you for your feedback! It seems our documentation still need some update on this ? If it's the case, I will let this issue open to not forget about it :-) |
Discussed in #3273
Originally posted by santino May 15, 2024
Hello folks, as mentioned by the title, I am looking to setup subscriptions in my Yoga implementation.
I am not using Yoga as a full server, but instead I am integrating it with my
fastify
instance.The
useGraphQLSSE
plugin only seems to work if Yoga is executed as a server.Do you have any example for writing up a plugin that only handles subscription requests received on my single
/graphql
endpoint?The text was updated successfully, but these errors were encountered: