-
Notifications
You must be signed in to change notification settings - Fork 150
Proposal: Deprecate Mutation.createEmptyCart, add Mutation.createGuestCart
#385
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
49d8186 to
7363074
Compare
| type Mutation { | ||
| + createCart( | ||
| + input: CreateCartInput | ||
| + ): CreateCartOutput @doc(description: "Create a new shopping cart") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current createEmptyCart mutation has some magic to it. If the request is authenticated with a bearer token the mutation returns the user's existing cart id otherwise it returns a new cart's id.
I see in the new response type the cart is returned, which is ideal, but would it also still return a user's existing cart?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the request is authenticated with a bearer token the mutation returns the user's existing cart id otherwise it returns a new cart's id.
Does it return the existing cart ID even in the case where that cart is not empty? If so that makes the createEmptyCart name a bit counter-intuitive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it return the existing cart ID even in the case where that cart is not empty
Checked, it does.
createCartForCustomer->executein resolvercreateCustomerCartincreateEmptyCartForCustomerquoteRepository->getActiveForCustomerincreateCustomerCart
That's confusing! I think the name is more confusing than the API, because you wouldn't want to flush a customer's existing cart, but a customer can only have a single cart.
I wonder what a better name is here? A name that makes it clear that this returns an existing thing, or falls back to creating a new thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it return the existing cart ID even in the case where that cart is not empty?
Yes, it returns the user's existing cart id in both cases.
If so that makes the createEmptyCart name a bit counter-intuitive.
And yes, I agree. That's why we alias over it in our code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what a better name is here? A name that makes it clear that this returns an existing thing, or falls back to creating a new thing.
In PWA Studio a user can only have one cart visible and active. We have no concept of fetching data for a "stashed" cart. So I can't see why you ever need the concept of new cart. I would think that simply querying for cart would suffice regardless. You're either getting a new cart or an active one if authed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry - shouldn't have even mentioned "stashed cart". I don't even know if Magento has the idea of a customer having multiple carts at once.
getCart seems reasonable, but the name sounds more like a query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went and looked at venia a bit more, and now I'm questioning if createEmptyCart handling logged-in users makes sense at all.
@sirugh what's the situation where we would want to use createEmptyCart for a logged-in user? It seems like, at the time venia sends this mutation, we already should know if the user is a guest or not, right?
I think these are the cases we need to cover, but please correct me if I'm wrong:
When rendering the cart
| Use Case | Covered by |
|---|---|
| User logged-in? | Query.customerCart |
| User not logged in, but guest cart token in local storage? | Query.cart(id: ID) |
| User not logged in, and no guest cart token in local storage? | Mutation.createEmptyCart, which would return the full Cart type |
If the above is correct, is seems like createEmptyCart should be something like createGuestCart. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getCart seems reasonable, but the name sounds more like a query.
Yeah, I keep wanting to say it, but then stop myself each time because it doesn't make it clear that it causes side-effects.
Hopefully my proposal above this comment improves things?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sirugh what's the situation where we would want to use createEmptyCart for a logged-in user? It seems like, at the time venia sends this mutation, we already should know if the user is a guest or not, right?
I don't remember why we used the single mutation but it seemed to make the logic simpler.
If the above is correct, is seems like createEmptyCart should be something like createGuestCart. Thoughts?
Sure that sounds fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember why we used the single mutation but it seemed to make the logic simpler.
Makes sense. I guess we ended up just moving the logic into an odd place: a createEmptyCart query that can return a non-empty cart.
I pushed up some changes to the proposal based on this thread. Can you give it another read through?
299edb9 to
c734a22
Compare
Mutation.createEmptyCart, add Mutation.createGuestCart
ff9e700 to
e75d77f
Compare
e75d77f to
7d6e681
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DrewML Looks good to me. Can you confirm this is not a breaking change?
|
@nrkapoor Can confirm - not a breaking change. Only change to an existing API is marking for deprecation, but not strict requirement to remove until down the line when we're sure usage has mostly dropped off. |
@DrewML Thanks. Please let me know when we are ready to merge this change so I can create a ticket on the graphql board for implementation. |
paliarush
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Until some point createEmptyCart was used by both, guests and registered customers. After creating separate query for registered customers, it was not renamed because of backward compatibility.
| 1. Requires manual cache handling with libraries like Apollo, to link the `ID` back to a `Cart` object | ||
| 2. Requires 2 round trips for the client to get cart data for a guest. Even though the cart is empty, it still has defaults the pwa-studio components will use to render an empty cart | ||
|
|
||
| During discussions, it was also noted that `createEmptyCart` has some behavior that's not clearly described by our schema. That is, If a customer is logged-in and has items in the cart, `createEmptyCart` returns an ID referencing a cart that is _not_ empty, which is extremely unintuitive. It was mentioned that the `pwa-studio` code [aliases this query](https://github.com/magento/pwa-studio/blob/38d652a4fbc797a4ac8ac0c3efa611003152c090/packages/venia-ui/lib/queries/createCart.graphql#L4) because of that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds like a bug. Registered customers are supposed to use customerCart query to get cart object (either new or existing)
design-documents/graph-ql/coverage/deprecate-createEmptyCart-add-createGuestCart.md
Outdated
Show resolved
Hide resolved
design-documents/graph-ql/coverage/deprecate-createEmptyCart-add-createGuestCart.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Alex Paliarush <[email protected]>
Rendered Proposal