xcladeDocs
Storefront GraphQL

Best Practices

Production patterns for fast, cache-friendly, resilient xclade storefronts and B2B portals

Best Practices

1. Separate Public Catalog Reads from Private Session Reads

Because XcladeResponseCache only caches queries whose every root field is in the public allowlist and whose request is anonymous, avoid mixing products and activeCustomerCart or authStatus into the same GraphQL document.

  • Good: Issue products / siteSettings / regions anonymously (or cached on the server) so they hit Redis (X-Cache: HIT), and fetch activeCustomerCart / myCompany separately with the customer bearer token.
  • Avoid: Combining products and cart in one query—doing so forces the entire response to Cache-Control: private, no-store.

2. Always Supply pricing_context When Selecting calculated_price

Pass at least region_id (recommended, since it resolves both currency and tax-inclusivity rules) or currency_code inside pricing_context:

query PricedCatalog($regionId: String!) {
  products(limit: 24, pricing_context: { region_id: $regionId }) {
    products {
      id
      title
      variants {
        id
        sku
        calculated_price {
          calculated_amount
          original_amount
          currency_code
        }
      }
    }
  }
}

3. Use addCartLineItemsBulk for Multi-SKU Additions

For B2B quick-order matrices, CSV paste forms, and bundle add-to-cart flows, use addCartLineItemsBulk instead of looping addCartLineItem. It executes atomically in one request, avoids hitting the 150 req/min rate limiter, and recalculates taxes/promotions once.


4. Handle Both B2C and B2B Cleanly

Check myCompany after customer sign-in:

  • If myCompany is null, render the standard retail checkout flow.
  • If myCompany is present, show the customer's company name, employee spending limit, Request a Quote (requestQuote), and Submit for Approval (createCartApprovals) actions when approval_settings requires sign-off.

5. Refresh Payment Collections After Cart Edits

Whenever a buyer edits line-item quantities, applies a promo code, or switches shipping methods after creating a payment collection, call refreshPaymentCollection(cart_id) before finalizing payment so the authorized amount always matches cart.total.


6. Keep Queries Lean and Shallow

  • Stay well below the depth cap (10 on /graphql) and avoid circular selections (e.g. product → variants → product → variants).
  • Always paginate list queries (limit + offset) rather than requesting unbounded pages.
  • Include X-Xclade-Publishable-Key and X-Xclade-Request: true on every storefront request.

On this page