mercure icon indicating copy to clipboard operation
mercure copied to clipboard

Create a Mercure adapter for GraphQL subscriptions (Apollo)

Open mathieutu opened this issue 4 years ago • 8 comments

Hi Kévin.

I would like to reopen #162. I would like to use mercure instead of websocket for my graphql subscriptions, but I'm actually not understanding how to properly do that. I didn't find any resource about it, neither someone who have already done it.

Disclaimer : I'm not a graphql expert, and especially totally noob in real-time stuff.

What I've understood is there is a several part in subscription: the pubsub implementation, and the transport implementation.

The documentation tells us about pubsub, but it forgets totally the transport here: https://www.apollographql.com/docs/apollo-server/data/subscriptions

I'm reading about sse transport in graphql, but I don't fully understand how to adapt it to mercure and how to implement it.

It seems really simple when reading you:

In response to the subscription query, the GraphQL server may return a corresponding topic URL. The client can then subscribe to the Mercure's event stream corresponding to this subscription by creating a new EventSource with an URL like https://example.com/.well-known/mercure?topic=https://example.com/subscriptions/ as parameter.

Updates for the given subscription can then be sent from the GraphQL server to the clients through the Mercure hub (in the data property of the server-sent event). Also, Mercure can easily be integrated with Apollo GraphQL by creating a dedicated transport.

Could you give us some advice on how to handle that? Maybe an example? Or would you mind giving an hand on creating a transport for mercure? It can helps adoption of the protocol in graphql community.

Thanks. Mathieu.

mathieutu avatar Jan 16 '21 18:01 mathieutu

Hi Mathieu,

Using Mercure for GraphQL subscriptions has been implemented by @alanpoulain in API Platform: https://api-platform.com/docs/master/core/graphql/#subscriptions The related code: https://github.com/api-platform/core/tree/master/src/GraphQl/Subscription

I hope this helps!

dunglas avatar Jan 16 '21 20:01 dunglas

Hi, @mathieutu , did you succeeded setting up an apollo transport for mercure subscriptions with help of the documentation ? On my part, i don't have sufficient apollo knowledge to got it working. I tried to use a SplitLink (https://www.apollographql.com/docs/link/composition/) to dedicate a transport to mercure, but link initialization fail with a 401.

If anyone can provide a working apollo code to subscribe to mercure ?

xleliberty avatar Feb 22 '21 10:02 xleliberty

Hi @xleliberty. Unfortunately no, I didn't succeed to have anything working with mercure, so I moved on to a standard websocket implementation which is ok for me. I may take a further look one day when I will have the time to do it.

mathieutu avatar Feb 22 '21 10:02 mathieutu

ok thanks

xleliberty avatar Feb 22 '21 13:02 xleliberty

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 23 '21 19:04 stale[bot]

Apparently, it will not be possible to completely get rid of the websocket yet. In addition, the current implementation in the api platform only supports update operations.

https://github.com/api-platform/core/pull/3321 https://github.com/api-platform/core/issues/4302

lermontex avatar Jun 09 '21 01:06 lermontex

Hello there. @mathieutu still using websocket? I would like to use mercure for my subscription as well.

tabha avatar Apr 02 '22 14:04 tabha

Hi @tabha. Sorry but yep the project was done with WS, and I've switched project months ago so not a proper owner for this anymore.

Good luck!

mathieutu avatar Apr 03 '22 20:04 mathieutu