yireo / magento2-graph-ql-rate-limiting

yireo/magento2-graph-ql-rate-limiting

Magento 2 module to add rate limiting to GraphQL resources

magento2-module Compatibility: 2.4.7-2.4.9 Code Quality: Warning Tests: Pass Security: Pass OSL-3.0

Yireo GraphQlRateLimiting

Magento version
PHP version
License
Latest Version

Magento 2 module to add rate limiting to GraphQL resources

This module checks how many GraphQL mutations and/or GraphQL queries are sent from a specific client to a Magento instance. If the number of these requests exceeds a configured maximum within a timeframe, a GraphQL error is returned with the HTTP status 429 Too Many Requests and a Retry-After header.

This module is specifically recommended for limiting mutations, so that your Magento shop is not flooded with fake requests to create sessions, customers or other data. Usually, in a headless environment, the amount of mutations is limited.

Usage

Install this extension:

composer require yireo/magento2-graph-ql-rate-limiting
bin/magento module:enable Yireo_GraphQlRateLimiting
bin/magento setup:upgrade

Next, login to the Magento Admin Panel, navigate to Store Configuration and then Yireo > Yireo GraphQL Rate Limiting > Settings and modify the settings to your needs:

  • Enabled: Enable rate limiting.
  • Limit Mutations and Limit Queries: Whether mutations and queries are limited at all.
  • Maximum Mutations and Maximum Queries: The maximum number of calls per root field, client and timeframe. A value of 0 or lower disables limiting.
  • Timeframe: The length of the window in seconds. The window starts with the first call and is not extended by later calls.
  • Identify by User Agent: Count clients with a different user agent separately. Not recommended, because a user agent is easily spoofed.

How requests are counted

  • Counting happens per root field of the operation that is executed (like generateCustomerToken or createEmptyCart). Every root field, including every alias of the same field, counts as one call. Fragments are resolved, so fragment names and the order of definitions do not matter.
  • A request that would exceed the maximum is denied as a whole and is not counted.
  • A client is identified by its IP address. IPv6 addresses are grouped by their /64 network.
  • Counters are stored in the Magento cache (Redis is recommended) with the tag GRAPHQL_RATE_LIMITING. Disabling the cache type GraphQL Rate Limiting does not disable rate limiting, but cleaning or flushing the cache resets all counters.

Running behind a proxy, load balancer or CDN

Behind a proxy, all visitors share the IP address of that proxy, unless Magento is configured to read the client IP from a header like X-Forwarded-For. When doing so, always configure the trusted proxies as well. Otherwise, Magento uses the first IP of the header, which is set by the client itself, and rate limiting can be bypassed by sending a random IP address with every request. Both are DI arguments of Magento\Framework\HTTP\PhpEnvironment\RemoteAddress:

<type name="Magento\Framework\HTTP\PhpEnvironment\RemoteAddress">
    <arguments>
        <argument name="alternativeHeaders" xsi:type="array">
            <item name="x-forwarded-for" xsi:type="string">HTTP_X_FORWARDED_FOR</item>
        </argument>
        <argument name="trustedProxies" xsi:type="array">
            <item name="load-balancer" xsi:type="string">10.0.0.1</item>
        </argument>
    </arguments>
</type>

GraphQL GET queries that are served from the Magento Full Page Cache or from Varnish never reach Magento, and are therefore not counted.

Testing to see if this works

Open up GraphiQL or some other client and create a simple request like the following:

query {
  products(filter: {name: {match: "jacket"}}) {
    items {
      sku
    }
  }
}

Configure the following settings in this Magento module (under the Store Configuration):

  • Enabled: Yes
  • Limit Queries: Yes
  • Maximum Queries: 3

After running the same query three times, the next request returns HTTP status 429 with the following error:

{
  "errors": [
    {
      "message": "Too many requests, please try again later",
      "extensions": {
        "category": "graphql"
      }
    }
  ]
}

Current status

Static Tests
Unit Tests
Integration Tests
Playwright
DI Compilation

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog,
and this project adheres to Semantic Versioning.

[Unreleased]

[1.0.0] - 25 November 2026

Changed

  • BREAKING: Rate limits are counted per executed root field (aliases included) in a fixed window, so fragment names, fragment order and aliases can no longer be used to bypass the limit
  • Counters are stored directly in the cache backend under a per-client lock, so disabling the cache type no longer turns off rate limiting
  • The Limit Queries and Limit Mutations settings are honoured; a value of 0 or lower disables limiting and numeric settings are validated
  • IPv6 clients are grouped per /64 network and long user agents are truncated
  • BREAKING: Removed the cache_ttl setting and the dependency on the abandoned sunspikes/php-ratelimiter
  • BREAKING: Supports PHP 8.2 up to 8.5 (PHP 8.1 dropped)
  • Documented counting, proxy setup and full page cache behaviour

Fixed

  • Rate-limited GraphQL requests return HTTP 429 with Retry-After instead of 500 (#3)
  • Rate-limited requests receive a generic error message
  • GraphQL queries broke on Magento 2.4.8, which passes a pre-parsed query document
  • Restricted admin roles could not be granted access to the configuration section, because its ACL resource was undefined
  • The query plugin is registered in the GraphQL area only instead of globally
  • Composer and module.xml dependencies match actual usage

[0.0.4] - 12 April 2024

Added

  • Limit to PHP 7.4 because sunspikes/php-ratelimiter is dead

[0.0.3] - 29 July 2020

Added

  • Magento 2.4 compatibility

[0.0.2] - Undocumented

Versions
Version Stability QA Status Compatibility Released
1.0.0 stable Pass Magento 2.4.7-2.4.9 Details 2026-11-25 19:00:23
0.0.5 stable Pass Not yet tested Details 2024-04-12 13:49:56
0.0.4 stable Not tested Not yet tested Details 2024-04-12 09:36:56
0.0.3 stable Not tested Not yet tested Details 2020-07-29 09:47:40
0.0.2 stable Not tested Not yet tested Details 2020-04-19 19:30:26
0.0.1 stable Not tested Not yet tested Details 2020-04-18 13:00:29

Requires 5

Package Constraint
magento/framework ^100.1|^101.0|^102.0|^103.0
magento/module-backend ^102.0
magento/module-config ^101.2
php >=8.2 <8.6
webonyx/graphql-php ^15.0

Requires-dev 4

Package Constraint
ext-json *
magento/module-graph-ql ^100.4
phpunit/phpunit ^10.5
yireo/magento2-integration-test-helper @dev

Compatibility

Each Magento release line is installed on its supported PHP versions, then the module is built (DI compilation + static-content deploy) and its unit and integration suites are run. The matrix shows the lines and PHP versions the module is confirmed to install and run on. Code-quality results further down (phpstan, phpcs, …) are reported separately and never affect compatibility.

Compatibility matrix (Magento × PHP)
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 Pass Pass
2.4.8 Pass Pass
2.4.9 Pass Pass

Code Quality

Advisory checks against the module's source. Static analysis runs once across the whole module; PHPStan re-runs per Magento + PHP version because resolvable symbols differ between releases. These NEVER affect the Compatibility badge. A phpcs finding can't make a module incompatible.

Static analysis

Coding standards (phpcs), mess detection (phpmd), copy-pasted code (cpd), PHP cross-version compatibility, composer.json validity. Each runs once for the whole module.

Static analysis results
Tool Status Findings Summary
PHPCS Warning 7 7 warnings (ruleset: Magento2)
PHPMD Warning 7 7 rule violations (UnusedFormalParameter:5, MissingImport:1, TooManyPublicMethods:1)
Cpd Pass 0
Composer validate Info 1 valid; 1 advisory note (composer validate --strict)

PHPStan

Type-checks the module's PHP against a real Magento install at the configured gate level. Re-runs per Magento and PHP version because resolvable symbols differ between releases.

PHPStan results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 N/A N/A
2.4.8 Pass Pass
2.4.9 Pass Pass

Tests

Unit and integration suites, run for each applicable Magento and PHP version. A test failure speaks to the module's behaviour, not its compatibility with a Magento line, so it is reported here separately and never reddens the compatibility matrix.

Unit tests

Unit tests results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 N/A N/A
2.4.8 N/A N/A
2.4.9 N/A N/A

Integration tests

Integration tests results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 Pass Pass
2.4.8 Pass Pass
2.4.9 Pass Pass

Security

Security checks run directly against the module: an audit of its declared dependencies for known vulnerabilities (composer audit) and a scan of its source for malware and web-shell signatures. Each runs once. A malware detection fails the version outright.

Security results
Tool Status Findings Summary
Composer audit Pass 0
Malware scan Pass 0

More from yireo

View vendor
Make it pay

Turn an existing module into recurring revenue.

If you already maintain a Magento 2 module on GitHub or GitLab, listing it on Packagento takes about five minutes. We mirror your tags, handle distribution signing, and route paid licenses through Stripe Connect, so you can keep shipping the way you already do.