Skip to content

GraphQL proxy

The browser never talks to Magento. Client-side GraphQL goes to /api/graphql on the storefront, a thin proxy that forwards only operations the storefront itself defines.

Magento GraphQLCloudflare/api/graphqlBrowserMagento GraphQLCloudflare/api/graphqlBrowseralt[not on the allowlist][protected operation]POST operation (+ Turnstile token)hash matches an engine operation?403verify token (action, hostname)success / failureforward, with customer token from cookieresultresult

At startup the proxy hashes every GraphQL document the engine defines. An incoming operation is normalised the same way and hashed. Anything without a matching hash gets 403.

  • Documents are normalised before hashing: whitespace is reprinted and the __typename fields Apollo adds are removed. The storefront’s own requests always match.
  • A request may batch at most 10 operations. The storefront never batches, so a large batch is only ever abuse.
  • Server-only operations, such as CMS block queries, are kept off the allowlist. Only server components run them, over the private network.
  • The allowlist is enforced in production only, so GraphQL tools keep working during development.

A hand-written query, an introspection query or a query copied from another site is rejected before it reaches Magento.

Abuse-prone operations need a Cloudflare Turnstile token: sign-in, registration, contact, newsletter, guest order and return lookup, guest tickets. The proxy verifies each token with Cloudflare and checks:

  • success;
  • action, which ties the token to the form that produced it;
  • hostname, against TURNSTILE_HOSTNAMES.

Rejections are logged as [graphql proxy] turnstile <reason>. See Configuration for the two halves of the setup.

A POST to /api/graphql is never cached: it carries customer tokens and mutations. nginx sets Cache-Control: no-store on it and caps the request body.

Guest reads go out as a hashed GET instead: an allowlisted query named by its hash, with no token or cookie forwarded, so one answer is right for every guest.

  • By default nginx’s graphql_get zone keeps each answer 60 seconds.
  • With SF_CACHE_GRAPHQL=1 (on top of the shared store: SF_CACHE_NEXT=1 and REDIS_URL), the proxy asks Magento by GET, keeps the answer in Valkey with the tags Magento sends (X-Magento-Tags), and deletes it when a Magento PURGE names one of those tags. An answer stays fresh for up to 6 hours, never past local midnight, and is then served once more while one request refreshes it. nginx keeps it 2 seconds. Only a clean 200 with tags is stored.
  • The X-Storefront-Cache response header says what happened: HIT, STALE, MISS, or BYPASS (not stored).
  • A purge is recorded with its time, and every read checks that record. An answer asked of Magento before one of its tags was purged is a miss, even when Valkey evicted the list of answers carrying that tag (it evicts any key under memory pressure), and even when the answer was written after the purge.
  • If Valkey stops answering, the proxy falls back to asking Magento as before.

Signed-in shoppers send the same hashed GET with ctx=1. The proxy then forwards the customer’s token from the httpOnly cookie, as on POST, so group prices and catalog price rules apply, and answers private, no-store: the separate URL means nginx or a CDN can never hand a signed-in shopper a guest’s copy, or the reverse.

  • With SF_CACHE_CONTEXT=1 the answer is also shared in Valkey between the shoppers of one pricing context: Magento’s X-Magento-Cache-Id, which follows the customer group, store and currency, never the customer.
  • A token’s context is learned only from Magento’s own clean answers to that token, kept 15 minutes under a hash of the token, and refreshed by every signed-in POST. A group change (leaving the first-order group after the first order, say) shows on the shopper’s next request to Magento; an admin change to a shopper who only browses cached answers shows within 15 minutes.
  • An answer is stored only under the context Magento reported for it and only with Magento’s tags, so Magento’s PURGE reaches it like a guest answer. Fields Magento marks uncacheable come without tags and are never stored.
  • Off, a signed-in GET goes to Magento with the token every time, the same as a POST.
  • The public JSON routes built on "use cache" data (/api/product-links, /api/brand-products, /api/product-blog-posts, /api/deals, /api/best-sellers) work the same way: with SF_CACHE_NEXT=1 their data is purged in Valkey, so nginx keeps them 2 seconds instead of 300. The browser keeps 300 seconds either way.