Skip to content

Commit

Permalink
Merge pull request #2089 from graphcommerce-org/fix/GCOM-1241
Browse files Browse the repository at this point in the history
Use 'cache-first' fetch mode for getting 'total_quantity' data (GCOM-1241)
  • Loading branch information
carlocarels90 authored Oct 24, 2023
2 parents 92f1024 + d4aee37 commit fd94498
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-seahorses-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphcommerce/magento-cart': patch
---

The CartFab wouldn't reflect that there are items in the cart when a customer refreshes the page after adding a product to the cart, without viewing the cart.
1 change: 1 addition & 0 deletions packages/magento-cart/components/CartFab/CartFab.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
query CartFab($cartId: String!) {
cart(cart_id: $cartId) {
__typename
id
...CartTotalQuantity
}
Expand Down
15 changes: 1 addition & 14 deletions packages/magento-cart/components/CartFab/CartFab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,8 @@ function CartFabContent(props: CartFabContentProps) {
)
}

/**
* We give CartFab a bit of special handling. We don't want to make requests for this component
* whilly nilly. We've imposed some limitations:
*
* We use useCartQuery that means that this will only execute when there is a cartId.
*
* We use fetchPolicy 'cache-only' so that when the cart comes into existence it will not
* immediately start fetching. Why? There is a time between creating a cart and adding the first
* product to the cart. This would mean that it would immediately start executing this query.
*/
export function CartFab(props: CartFabProps) {
const cartQuery = useCartQuery(CartFabDocument, {
fetchPolicy: 'cache-only',
nextFetchPolicy: 'cache-first',
})
const cartQuery = useCartQuery(CartFabDocument)

return (
<WaitForQueries waitFor={cartQuery} fallback={<CartFabContent {...props} total_quantity={0} />}>
Expand Down
15 changes: 15 additions & 0 deletions packages/magento-cart/typePolicies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ export const cartTypePolicies: StrictTypedTypePolicies = {
toReference({ __typename: 'Cart', id: (args as QuerycartArgs)?.cart_id }),
},
},

Mutation: {
fields: {
createEmptyCart: {
merge: (_, incoming: string, options) => {
options.cache.writeQuery({
query: CartFabDocument,
variables: { cartId: incoming },
data: { cart: { __typename: 'Cart', id: incoming, total_quantity: 0 } },
})
return incoming
},
},
},
},
}

export const migrateCart = (
Expand Down

0 comments on commit fd94498

Please sign in to comment.