Vendure: How to Properly Subscribe to activeOrder

Created on 19 Aug 2020  ·  2Comments  ·  Source: vendure-ecommerce/vendure

Describe the bug
After attempting to use Apollo's Subscription method (WebSockets), I get the following error, Error during WebSocket handshake: Unexpected response code: 400.

So more of a general question: I do not see a subscription method in the Shop API Guide. Does Vendure allow the ws:// protocol?

Use case:
I have at cart icon in the header and upon using addToCart, I expect to be able to listen to changes in the cart to update the user in real-time without having to pass props.

Edit:
Just randomly tried to use activeOrder as a subscription and got this error: Schema is not configured for subscriptions. It makes sense but it further supports my question.

question ❓

Most helpful comment

Thanks for the response. For anyone tuning in, for React I ended up using the Context API, so creating a global state for the cart and pushing to it every time I use addItemToOrder.

All 2 comments

Hi,
Vendure currently does not define any Subscriptions, only Queries and Mutations.

However, you shouldn't need subscriptions to solve the problem you mention. The cart contents only change in response to user actions, e.g addItemToOrder. So as long as you are making use of the Apollo cache, then the active order should get updated in the cache in all places after executing that mutation. At least, that's how it works in Apollo Client with Angular. I'm sure there is similar functionality when using React but I cannot advise on specifics.

Thanks for the response. For anyone tuning in, for React I ended up using the Context API, so creating a global state for the cart and pushing to it every time I use addItemToOrder.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

michaelbromley picture michaelbromley  ·  5Comments

michaelbromley picture michaelbromley  ·  3Comments

mattgills picture mattgills  ·  4Comments

chladog picture chladog  ·  7Comments

stefanvanherwijnen picture stefanvanherwijnen  ·  5Comments