|
| 1 | +# Deprecate and replace `Mutation.createEmptyCart` |
| 2 | + |
| 3 | +## What |
| 4 | + |
| 5 | +- Mark `Mutation.createEmptyCart` as deprecated |
| 6 | +- Add `Mutation.createGuestCart` as a replacement that includes `Cart` in its return type |
| 7 | + |
| 8 | +## Why |
| 9 | + |
| 10 | +This PR started from a Slack discussion about mutations and client-side caching. At one point [@sirugh](https://github.com/sirugh) mentioned: |
| 11 | + |
| 12 | +> This makes me wonder if the createCart mutation should return a cart type instead of just the id |
| 13 | +
|
| 14 | +There are 2 problems that I'm aware of with the current schema returning just an `ID` instead of the `Cart` type: |
| 15 | + |
| 16 | +1. Requires manual cache handling with libraries like Apollo, to link the `ID` back to a `Cart` object |
| 17 | +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 |
| 18 | + |
| 19 | +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. |
| 20 | + |
| 21 | +## Intended Usage |
| 22 | +Because the UI knows if it has a token for a customer, `Query.cart` should be used by the UI to get the cart for a logged-in user. If there is _not_ a customer token present, the UI should use `createGuestCart`, which will return a `Cart` object, the same return type as `Query.cart`. |
| 23 | + |
| 24 | + |
| 25 | +## Proposed Changes |
| 26 | + |
| 27 | +```diff |
| 28 | +type Mutation { |
| 29 | ++ createGuestCart( |
| 30 | ++ input: CreateGuestCartInput |
| 31 | ++ ): CreateGuestCartOutput @doc(description: "Create a new shopping cart") |
| 32 | + createEmptyCart( |
| 33 | + input: createEmptyCartInput |
| 34 | ++ ): String @deprecated(reason: "Use `Mutation.createGuestCart`, or `Query.cart` for logged-in shoppers") |
| 35 | +- ): String @doc(description:"Creates an empty shopping cart for a guest or logged in user") |
| 36 | +} |
| 37 | + |
| 38 | ++input CreateGuestCartInput { |
| 39 | ++ cart_uid: ID @doc(description: "Optional client-generated ID") |
| 40 | ++} |
| 41 | ++ |
| 42 | ++type CreateGuestCartOutput { |
| 43 | ++ cart: Cart |
| 44 | ++} |
| 45 | + |
| 46 | +input createEmptyCartInput { |
| 47 | + cart_id: String |
| 48 | +} |
| 49 | +``` |
0 commit comments