Search by

yireo / magento2-sync-graph-ql-session-with-frontend

yireo

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

Fund package maintenance!

yireo

www.paypal.me/yireo

Statistics

Installs: 33

Dependents: 0

Suggesters: 0

Stars: 11

Open Issues: 0

1.0.0 2026-11-25 17:04 UTC

This package is auto-updated.

Last update: 2026-11-30 19:06:16 UTC


README

Magento version PHP version License Latest Version

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 section cart (customerData.get('cart') in Luma, the private-content-loaded event 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 (createGuestCart or createEmptyCart) 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_id is 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_id is 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: true with a wildcard or reflected origin, because customer/section/load would then be readable cross-origin.
  • Older versions of this module also added the GraphQL customer token to the customer section (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 the generateCustomerToken mutation instead.

Current status

Static Tests Unit Tests Integration Tests Playwright DI Compilation