Skip to content

Conversation

@DrewML
Copy link
Contributor

@DrewML DrewML commented Jun 16, 2020

@DrewML DrewML force-pushed the deprecate-createEmptyCart-add-createCart branch from 49d8186 to 7363074 Compare June 16, 2020 19:46
type Mutation {
+ createCart(
+ input: CreateCartInput
+ ): CreateCartOutput @doc(description: "Create a new shopping cart")
Copy link

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

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.

Copy link

@sirugh sirugh Jun 16, 2020

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.

Copy link

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.

Copy link

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.

Copy link
Contributor Author

@DrewML DrewML Jun 16, 2020

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?

Copy link
Contributor Author

@DrewML DrewML Jun 16, 2020

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?

Copy link

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.

Copy link
Contributor Author

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?

@DrewML DrewML force-pushed the deprecate-createEmptyCart-add-createCart branch 6 times, most recently from 299edb9 to c734a22 Compare June 16, 2020 22:00
@DrewML DrewML changed the title Proposal: Deprecate createEmptyCart, add createCart Proposal: Deprecate Mutation.createEmptyCart, add Mutation.createGuestCart Jun 16, 2020
@DrewML DrewML force-pushed the deprecate-createEmptyCart-add-createCart branch 4 times, most recently from ff9e700 to e75d77f Compare June 16, 2020 22:05
@DrewML DrewML force-pushed the deprecate-createEmptyCart-add-createCart branch from e75d77f to 7d6e681 Compare June 16, 2020 22:06
Copy link
Contributor

@nrkapoor nrkapoor left a 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?

@DrewML
Copy link
Contributor Author

DrewML commented Jun 17, 2020

@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 DrewML added the open discussion Proposal or design document is open for public discussion label Jun 17, 2020
@nrkapoor
Copy link
Contributor

@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.

Copy link
Contributor

@paliarush paliarush left a 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.
Copy link
Contributor

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)

@melnikovi melnikovi merged commit 62b50fc into master Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

GraphQL open discussion Proposal or design document is open for public discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants