graphcommerce-org / graphcommerce

GraphCommerce® is a headless storefront replacement for Magento 2 (PWA), that delivers a faster, better user experience. Fully customizable (React, Next.js) and open-source.
https://www.graphcommerce.org
Other
307 stars 72 forks source link

Coupon Query does not need to be a Cart Fragment #2381

Open JesseMaxwell opened 1 month ago

JesseMaxwell commented 1 month ago

Describe the Bug

The CouponAccordion uses a query: GetCouponDocument. This imports (node_modules/@graphcommerce/magento-cart-coupon/CouponAccordion/GetCoupon.graphql):

query GetCoupon($cartId: String!) {
  cart(cart_id: $cartId) {
    ...Coupon
  }
}

Which imports (node_modules/@graphcommerce/magento-cart-coupon/Api/Coupon.graphql):

fragment Coupon on Cart @injectable {
  id
  __typename
  applied_coupons {
    code
  }
}

Problem

Because Coupon is a cart fragment, it imports more than needed. It also causes the Coupon component to update anytime the cart updates.

Recommendation

Add the Coupon's fragment directly to the GetCoupon for a leaner request and less frequent Coupon component updates.

Expected Behavior

Coupon query does not get shipping addresses and cart prices.

To Reproduce

  1. Navigation to the cart
  2. Make a change to the cart
  3. Notice that the Coupon query is called.
paales commented 1 month ago

Because Coupon is a cart fragment, it imports more than needed. It also causes the Coupon component to update anytime the cart updates.

I wouldn't expect that to happen specifically, but it does seem to happen because of: https://github.com/graphcommerce-org/graphcommerce/blob/canary/packages/magento-cart-checkout/fragments/InjectTotalsIntoCoupon.graphql

I'm not exactly sure why it is there, but then again, the totals do need to be refetched when the coupon changes, right? but it seems that CartTotals fetches a lot which isn't necessary? Coupons can affect shipping method pricing, I think?