mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify
https://mercurius.dev/
MIT License
2.34k stars 234 forks source link

[question] How to handle the personalized subscriptions? #294

Open katsanva opened 3 years ago

katsanva commented 3 years ago

Hello. I've been using the mercurius for some time and got a question on how to resolve the personalized subscriptions?

The part that handles this is located here.

Use case 1:

Users should obtain personalized content by making the subscription query.

This is not working as far operationId is resolved by the query & params only.

Is it a good way to add the connectionInit when resolving the operationId?

Use case 2:

Non-logged in user creates a subscription and get's non-personalized content. Then the user performs a query to log in, while the subscription is still active. Since that user should get the personalized content from the existing subscription.

Is this intended to be resolved on the mercurius side or is that application-specific?

mcollina commented 3 years ago

I don't really understand the question. What do you mean by personalized content, and how you model it?

katsanva commented 3 years ago

@mcollina lets say its something like news feed. the subscription query will look like (pseudocode)

subscription news {
   title
   img
   preview
}

with no variables.

When the anonymous user makes that query he will get some default stream of events But when the logged-in user subscribes he need to get not the default stream, but more relevant news, that he is interested in.

Does that sound better?

But currently, they will obtain the same default stream due to the implementation.

PacoDu commented 3 years ago

Hi @katsanva, regarding the user authentication, mercurius exposes two options: subscription.onConnect or subscription.verifyClient, with these options you can authenticate/identify the request and extend the subscription context with any useful information related to the current user (like a decoded JWT token). Your resolver implementation can then change its behavior depending on the context.

katsanva commented 3 years ago

@PacoDu thank you. I know how to auth the user. The issue is that subscription-client ignores on the second call is the user authenticated or not.

mcollina commented 3 years ago

Can you please make an example to reproduce your code with a failing test?