yireo / magento2-sync-graph-ql-session-with-frontend
Magento 2 module to sync a GraphQL session with the regular session
Package info
github.com/yireo/Yireo_SyncGraphQlSessionWithFrontend
Language:TypeScript
Type:magento2-module
pkg:composer/yireo/magento2-sync-graph-ql-session-with-frontend
Requires
- php: >=8.2 <8.6
- magento/framework: ^100.1|^101.0|^102.0|^103.0
- magento/module-checkout: ^100.1
- magento/module-quote: ^100.1|^101.0
Requires (Dev)
- phpunit/phpunit: ^10.0 || ^11.0 || ^12.0
- yireo/magento2-integration-test-helper: ~0.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-30 19:06:16 UTC
README
Magento 2 module to share the guest cart of the regular frontend (Luma or Hyvä) with GraphQL.
Summary
- The GraphQL cart token (aka
masked_id) of a guest cart is added to the CustomerData sectioncart(customerData.get('cart')in Luma, theprivate-content-loadedevent in Hyvä), which is automatically synced to local storage; - Within local storage, you can fetch the GraphQL token again from
mage-cache-storage.cart.masked_id, for instance if you are switching from a regular frontend to a React-based checkout; - This works in one direction only: a cart that is created through GraphQL (
createGuestCartorcreateEmptyCart) is not attached to the session of the regular frontend;
Kudos
Thanks to Willem Wigman for coming up with the idea for putting the cart token in the section data.
Proof-of-Concept for cart
Add a product to the cart in the regular frontend as a guest. Next, inspect the local storage entry mage-cache-storage.cart.masked_id (if it is missing, wipe out local storage or modify the cart, so that the section data is reloaded). The result might be something like the following:
vGS4ZLj6LkFVrH5CkAPEapLhgfbsoQKF
Use this token as cartId within a GraphQL client (for instance within React). The product added in the frontend should be there in the GraphQL cart as well:
query cart($cartId: String!) { cart(cart_id: $cartId){ id items { product { sku } } } }
Now, add a product to the cart through GraphQL (in this case with a product SKU 24-MB04):
mutation addToCart($cartId: String!) { addProductsToCart(cartId: $cartId, cartItems: [{quantity: 1, sku: "24-MB04"}]) { cart { id total_quantity items { id prices { row_total { currency value } } product { url_key } } } user_errors { code message } } }
Next, head over to the regular frontend, wipe out local storage mage-cache-storage.cart (or do this in Luma via customerData.reload(['cart'], true)) and inspect the cart again: It should contain both products.
Note that a token from createGuestCart (or createEmptyCart) creates a new, separate cart that the regular frontend does not know about. Always start with the token from mage-cache-storage.cart.masked_id instead.
Security
- The
masked_idis only added for guest carts. For a guest cart, the masked ID is the only credential that GraphQL needs to read and modify that cart (email, addresses, placing the order). Because it is stored in local storage, any XSS or compromised third-party script on your site can read it. Keep a strict Content Security Policy and be careful with third-party scripts. - For customer carts, no
masked_idis added: GraphQL requires a customer token for those carts anyway. - When using a headless frontend on another origin, keep the CORS allow-list strict and never combine
Access-Control-Allow-Credentials: truewith a wildcard or reflected origin, becausecustomer/section/loadwould then be readable cross-origin. - Older versions of this module also added the GraphQL customer token to the
customersection (mage-cache-storage.customer.customer_token). This was removed, because it exposed a bearer token to JavaScript (also to visitors with only a persistent "Remember Me" session) and returned tokens that were revoked or issued to other devices. To get a customer token in a headless frontend, use thegenerateCustomerTokenmutation instead.