-
Notifications
You must be signed in to change notification settings - Fork 133
Allow context to be a function that is called per message #12
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
Conversation
@NeoPhi Do you have a specific use-case that this is necessary for? |
Our standard HTTP GraphQL flow uses per request context creation to handle authorization and caching. The current onSubscribe semantics creates the context once and it is shared for all messages that are later processed. Given that messages may occur at various times in the future we would want to create the context for each message to avoid any long lived state in the context. For example before processing a messages we would want to check the user's session to see what their current authorization is. For a standard GraphQL request we do this as part of the per request context creation and would like to use similar semantics when handling subscription message processing. |
@NeoPhi I'm not sure how that per-message context would help in your case, because the client only sends a TLDR: if it's just for auth, I don't think it makes sense to have a per-event context. Are there other things that you put on the context that actually have to change? |
I would store a connector/dataloader instance for every subscription execution. |
The issue is that to store a dataloader on the originally returned context then I need to worry about invalidating that cache between message executions. We also store a normalized time on the context so that all resolvers have the same value of now for calculating data. |
Yep - I'm agreeing with you @NeoPhi, I think generating a new context per execution is a good idea. |
Ok, that makes sense. I'd rather have everything in one function though, can you modify the PR to just do all the work in the onMessage function? I'd also rather not have too many options, so we we could just overload the context argument, and make it either an object, a function, or a function that returns a promise. That would be similar to how express-graphql does it, so I think it would be fine. Thoughts? |
Yeah, |
Just to confirm, do you mean combining the |
Yeah, I think if you want the context in the filter, then I'd move that into onMessage. Do you want the context in the filter so you can check permissions before running the query? |
…ction instead of separate option
Looks good! I'm still not 100% sure we need the context in the filter function, but it probably doesn't hurt, so let's go with it. |
The idea behind this change is to support a distinct context for each processed message. The idea is to mirror the per HTTP request GraphQL context creation flow. Currently the context is fixed on the SubscriptionManager when the initial publish is created and is also used to create the triggerMap. The API change adds
createContext: Function
to SubscriptionOptions. If specified the new context is passed tofilter
andonMessage
.