Skip to content

Conversation

@vanpho93
Copy link
Member

Resolves #6437
Impact: major
Type: feature

Issue

Unable to notify client when cart changed.

Solution

Add GraphQL Subscription to cart

@changeset-bot
Copy link

changeset-bot bot commented Nov 28, 2022

⚠️ No Changeset found

Latest commit: 1ac6ba9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vanpho93 vanpho93 changed the base branch from trunk to feat/upgrade-to-apollo-server-v3 November 28, 2022 02:58
@brent-hoover brent-hoover requested review from aldeed and removed request for brent-hoover November 28, 2022 02:58
@vanpho93 vanpho93 force-pushed the feat/add-cart-updated-subscription branch from 09c0e07 to 96da22e Compare November 28, 2022 04:50
@brent-hoover brent-hoover self-requested a review December 7, 2022 09:21
Copy link
Collaborator

@brent-hoover brent-hoover left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok to me except a bunch of spelling errors in test descriptions. Would want @aldeed to look at it as well

Copy link
Contributor

@aldeed aldeed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be nice to have available! I left some suggestions, mostly about patterns and naming.

if (cart.accountId) {
context.pubSub.publish("ACCOUNT_CART_UPDATED", { accountCartUpdate: cart });
} else {
context.pubSub.publish("ANONYMOUS_CART_UPDATED", { anonymousCartUpdate: cart });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems fine for both of these to pass { cart } rather than separate names accountCartUpdate and anonymousCartUpdate. The subscriptions could also then share the same filter function if you want.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might even consider simplifying this to emit CART_UPDATED always, and then filter on cart.accountId in each subscription resolver instead. There are probably pros and cons to each approach, performance vs. consumer complexity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vanpho93 I still think this should be simplified further to:

context.pubSub.publish("CART_UPDATED", { cart });

Otherwise it is unnecessary complexity for the event consumer to check for both anonymousCartUpdate and accountCartUpdate when they often will not care what type of cart it is. If they do care, they can check it with if (cart.accountId) in the consumer function.

For example, if you make this change, then your account cart subscription filter can just be:

export function filter(payload, variables) {
  const { cart } = payload;
  const { accountId, shopId } = variables;
  return cart.accountId === accountId && cart.shopId === shopId;
}

@vanpho93 vanpho93 force-pushed the feat/upgrade-to-apollo-server-v3 branch 3 times, most recently from 15a0f76 to f9509eb Compare December 14, 2022 03:32
@vanpho93 vanpho93 force-pushed the feat/add-cart-updated-subscription branch from be6ad3f to 5554ec6 Compare December 14, 2022 09:36
@vanpho93 vanpho93 force-pushed the feat/upgrade-to-apollo-server-v3 branch from f9509eb to 8dcc827 Compare January 5, 2023 13:23
@vanpho93 vanpho93 force-pushed the feat/add-cart-updated-subscription branch from 5554ec6 to 09ae49a Compare January 5, 2023 13:32
@vanpho93 vanpho93 requested a review from aldeed January 6, 2023 01:26
Copy link
Contributor

@aldeed aldeed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment response about using cart as the event payload property. Otherwise looking good.

@vanpho93 vanpho93 force-pushed the feat/add-cart-updated-subscription branch from 09ae49a to 1ac6ba9 Compare January 9, 2023 07:19
@vanpho93
Copy link
Member Author

vanpho93 commented Jan 9, 2023

@aldeed Updated, can you take another look?

@vanpho93 vanpho93 requested a review from aldeed January 9, 2023 07:24
@brent-hoover brent-hoover merged commit 02e8d8e into feat/upgrade-to-apollo-server-v3 Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

As a developer, I want to enable cart subscription so I know when my cart has been modified

4 participants