apollographql / react-apollo

:recycle: React integration for Apollo Client
https://www.apollographql.com/docs/react/
MIT License
6.85k stars 790 forks source link

Expose context in useSubscription options #3654

Open xEcEz opened 4 years ago

xEcEz commented 4 years ago

Would it be possible to expose context in useSubscription options?

I have a use case where I need to set the URI dynamically for a given subscription. Currently, I am stuck with having to declare another client that uses it by default, where in a similar scenario but when using useQuery, I can simply set the context's URI on the fly.

prince3339 commented 4 years ago

@xEcEz I'm having the same issue. Stuck with this. Did you get any workaround for this?

xEcEz commented 4 years ago

No, I am still using another ApolloClient on the side that declares a WebSocketLink which uses the target URI.

prince3339 commented 4 years ago

@xEcEz I don't understand why the team still hasn't given any attention to this issue :(

ReshaSD commented 4 years ago

We can use a very unreliable crutch for now: add a comment to the query string. For instance:

#QUERY_TYPE=${type}#
  subscription f( ...

And check in the split method:

split(
  (op) => {
    const sourceQuery = op.query.loc.source.body;
    return sourceQuery.search(new RegExp('#QUERY_TYPE=${type}#')) !== -1;
  },
  link1,
  link2
)