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/regionsanonymously (or cached on the server) so they hit Redis (X-Cache: HIT), and fetchactiveCustomerCart/myCompanyseparately with the customer bearer token. - Avoid: Combining
productsandcartin one query—doing so forces the entire response toCache-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
myCompanyisnull, render the standard retail checkout flow. - If
myCompanyis present, show the customer's company name, employee spending limit, Request a Quote (requestQuote), and Submit for Approval (createCartApprovals) actions whenapproval_settingsrequires 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 (
10on/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-KeyandX-Xclade-Request: trueon every storefront request.