scandipwa / persisted-query
scandipwa/persisted-query
ScandiPWA persisted query module
ScandiPWA persisted query
The main goal of persisted query approach is to reduce the amount of data transfered from the client to the server within a POST body containing GraphQl Document.
However, current module extends the usage of persisted queries to actually cache the responses in Varnish or CDN node
for fast responses. (avg. 5ms vs 5000ms on dev machine)
Prerequisites
- Varnish
- Redis
- PHP ext-phpredis is suggested for faster serialization and deserialization
Config
magento setup:config:set
For the convenience there are additional flags available for php bin/magento setup:config:set command:
--pq-host[mandatory] - persisted query redis host (redis for ScandiPWA docker setup)
--pq-port[mandatory] - persisted query redis port (6379 for ScandiPWA docker setup)
--pq-database[mandatory] - persisted query redis database (5 for ScandiPWA docker setup)
--pq-scheme[mandatory] - persisted query redis scheme
--pq-password[optional, empty password is not allowed] - persisted query redis password
Manual configuration
Configuration for custom Redis storage, where hashes and GraphQl documents are kept in environment config
(app/etc/env.php) -> cache/persisted-query and can be configured manually:
'persisted-query' => [
'redis' => [
'host' => 'redis',
'scheme' => 'tcp',
'port' => '6379',
'database' => '5'
]
]
Cache control
Available from v1.3.0
CLI command magento cache:flush and admin panel Cache Management has necessary logic to flush GraphQl responses
stored in varnish.
persisted_query_response - can be disabled, controls varnish caches (graphql response caches).
bin/magento scandipwa:pq:flush - flushes persisted query REDIS storage(query body)
Usage
Dynamic persisted query suppose Client to register unknown queries with a series of request-response.
Recommended usage:
- Optimistically request query execution, referencing query by hash, passing necessary query variables as request
parameters:GET /graphql?hash=135811058&hideChildren=true - Server has multiple options:
- respond with resolved query (status code
200) - respond with Unknown query error (status code
410)
- Status code
410- client must issue PUT request, with the same hash and entire GraphQl query document within
the body.PUT /graphql?hash=135811058 - Server responds with status code
201on successful query registration. - Server will now executes the registered query referenced by hash
GET /graphql?hash=135811058
In order to effectively utilize persisted query mechanism and avoid unnecessary time-consuming request-response
interactions GraphQl query must utilize variables.
Variables
Variables must be passed in pseudo-json format. You must keep the structure, but skip usage of quotes.
Array
Array is a list of values, separated with coma, i.e.: cmsBlocks_identifiers=homepage-promo-categories,homepage-top-items,homepage-about-us
Let's consider the details:
cmsBlocks_identifiers - GraphQl variable name
homepage-promo-categories,homepage-top-items,homepage-about-us - Array of values
Complex structures
Complex structures must keep structures described with special chars {, }, :.
Array within complex structs MUST use special chars: [, ].
Example:
_filter={category_url_path:{eq:men},max_price:{lteq:300},color:{in:[74,75]}}
Let's consider the details:
_filter - GraphQl variable name
{ category_url_path: { eq:men }, max_price:{ lteq:300 }, color:{ in:[74,75] } } - "Object", passed as GET parameter. It has not quotation marks, as these are automatically added by the server
during the processing.
Examples
Query body:
query ($cmsBlocks_identifiers:[String]) {cmsBlocks:cmsBlocks(identifiers:$cmsBlocks_identifiers){ items{ title, content, identifier } }}
Request URI:
GET /graphql?hash=2443957263&cmsBlocks_identifiers=homepage-promo-categories,homepage-top-items,homepage-about-us
Query body:
"query ($_currentPage:Int!, $_pageSize:Int!, $_filter:ProductFilterInput!, $category_url_path:String!) {products(currentPage:$_currentPage, pageSize:$_pageSize, filter:$_filter){ total_count, items{ id, name, short_description, url_key, special_price, sku, categories{ name, url_path, breadcrumbs{ category_name, category_url_key } }, price{ regularPrice{ amount{ value, currency } }, minimalPrice{ amount{ value, currency } } }, thumbnail, thumbnail_label, small_image, small_image_label, brand, color, size, shoes_size, type_id }, filters{ name, request_var, filter_items{ label, value_string, ... on SwatchLayerFilterItem { label, swatch_data{ type, value } } } } }, category:category(url_path:$category_url_path){ id, name, description, url_path, image, url_key, product_count, meta_title, meta_description, breadcrumbs{ category_name, category_url_key, category_level }, children{ id, name, description, url_path, image, url_key, product_count, meta_title, meta_description, breadcrumbs{ category_name, category_url_key, category_level } } }}"
Request URI:
https://scandipwa.local/graphql?hash=1713013963&_currentPage=1&_pageSize=12&_filter={category_url_path:{eq:men},max_price:{lteq:300}}&category_url_path=men
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]
[2.1.0] - 2019-10-28
Changed
- Replace predis/predis with
colinmollenhour/credis, that supportsPhpRedismodule for high load.
[1.5.0] - 2019-10-25
Changed
- Replace predis/predis with
colinmollenhour/credis, that supportsPhpRedismodule for high load.
[2.0.2] - 2019-10-23
Changed
- Fix argument value partial escaping
[1.4.8] - 2019-10-23
Changed
- Fix argument value partial escaping
[2.0.1] - 2019-10-18
Changed
- Fix float parameter handling
[1.4.7] - 2019-10-18
Changed
- Fix float parameter handling
[2.0.0] - 2019-07-29
Changed
- Magento 2.3.2 adoption
- Fixes
[1.4.3] - 2019-07-26
Added
- verbose error messages for Varnish flush requests
[1.3.0] - 2019-05-23
Added
- Event observer for
bin/magento cache:flushto trigger persisted query varnish storage flush (happens also on
setup:upgrade) bin/magento scandipwa:pq:flush- command for flushing query document (redis) storage (does not happen on
setup:upgrade)
Removed
persisted_querycache is unlisted in CLI and Admin cache control menu
Changed
- File restructuring improving readability
- Updating README.md & CHANGELOG.md
- Update Magento module version to fit release tag
- Minor CS improvements
[1.2.0] - 2019-05-07
Added
- flush option for persisted query redis
- cache control for PQ and PQ responses
- flushing logic for varnish and redis
Removed
- version tag in composer.json
Changed
- Update README.md
- improve code styling and comments
[1.1.1] - 2019-03-20
Changed
--pq-schemeis not settingtcpby default.- Move CHANGELOG.md to the root of the module
- README.md
[1.1.0] - 2019-03-20
Added
- Added custom flags to
setup:config:setCLI command
Changed
- Changed README.md
[1.0.0] - 2019-03-08
Added
- Initial commit
- predis/predis as dependency
- persisted query support
- Magento 2 module registration: ScandiPWA_PersistedQuery
Plugin\PersistedQueryregistered forMagento\GraphQl\Controller\GraphQlPlugin\PersistedQueryregistered forScandiPWA\GraphQl\Controller\GraphQl
| Version | Stability | QA Status | Released |
|---|---|---|---|
| 3.1.1 | stable | Fail | 2023-04-12 07:33:36 |
| 3.1.0 | stable | Not tested | 2023-04-04 02:38:40 |
| 3.0.5 | stable | Not tested | 2022-06-11 00:02:43 |
| 3.0.4 | stable | Not tested | 2022-06-09 19:16:51 |
| 3.0.3 | stable | Not tested | 2022-01-25 20:47:22 |
| 3.0.2 | stable | Not tested | 2021-09-22 17:27:15 |
| 3.0.1 | stable | Not tested | 2020-11-05 10:32:23 |
| 2.5.2 | stable | Not tested | 2020-11-05 10:30:50 |
| 3.0.0 | stable | Not tested | 2020-11-05 09:39:42 |
| 2.5.1 | stable | Not tested | 2020-11-05 09:36:09 |
| 2.5.0 | stable | Not tested | 2020-09-07 10:57:15 |
| 2.4.0 | stable | Not tested | 2020-08-26 15:57:41 |
| 2.2.2 | stable | Not tested | 2020-08-07 08:46:02 |
| 2.3.2 | stable | Not tested | 2020-08-07 08:45:21 |
| 2.3.1 | stable | Not tested | 2020-07-24 13:17:53 |
| 2.3.0 | stable | Not tested | 2020-06-09 13:23:26 |
| 2.2.1 | stable | Not tested | 2020-06-09 13:21:13 |
| 2.2.0 | stable | Not tested | 2020-05-30 07:19:29 |
| 2.1.2 | stable | Not tested | 2020-01-22 13:30:42 |
| 1.5.1 | stable | Not tested | 2019-11-05 10:02:31 |
| 2.1.0 | stable | Not tested | 2019-10-28 10:55:26 |
| 2.1.1 | stable | Not tested | 2019-10-28 10:54:51 |
| 1.5.0 | stable | Not tested | 2019-10-25 14:42:55 |
| 1.4.8 | stable | Not tested | 2019-10-23 07:16:18 |
| 2.0.2 | stable | Not tested | 2019-10-23 07:16:07 |
| 2.0.1 | stable | Not tested | 2019-10-18 08:06:43 |
| 1.4.6 | stable | Not tested | 2019-10-18 08:06:16 |
| 1.4.7 | stable | Not tested | 2019-10-18 08:05:57 |
| 1.4.5 | stable | Not tested | 2019-08-28 09:58:41 |
| 2.0.0 | stable | Not tested | 2019-08-28 09:57:25 |
| 1.4.3 | stable | Not tested | 2019-07-26 13:05:40 |
| 1.4.4 | stable | Not tested | 2019-07-26 13:02:23 |
| 1.4.2 | stable | Not tested | 2019-06-11 13:32:43 |
| 1.4.1 | stable | Not tested | 2019-05-30 14:14:04 |
| 1.4.0 | stable | Not tested | 2019-05-30 12:42:00 |
| 1.3.0 | stable | Not tested | 2019-05-23 10:07:48 |
| 1.2.0 | stable | Not tested | 2019-05-07 07:37:38 |
| 1.1.1 | stable | Not tested | 2019-03-20 12:25:42 |
| 1.1.0 | stable | Not tested | 2019-03-20 11:51:58 |
| 1.0.0 | stable | Not tested | 2019-03-08 14:36:19 |
Requires 6
| Package | Constraint |
|---|---|
| magento/framework | * |
| magento/module-cache-invalidate | * |
| magento/module-page-cache | * |
| magento/module-store | * |
| magento/module-graph-ql | ^100.3 |
| colinmollenhour/credis | ^1.10 |
Suggests 1
| Package | Reason |
|---|---|
| magento/module-catalog | To invalidate persisted_query cache after full reindex |
| Tool | Status | Findings | Summary |
|---|---|---|---|
| PHPCS | Pass | 0 | |
| PHPStan | Fail | 24 | 24 errors (level 4, ruleset: phpstan + bitexpert/phpstan-magento) |
| Cpd | Pass | 0 | |
| Security | Pass | 0 |
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.