scandipwa / persisted-query
ScandiPWA 持久查询模块
Requires
- colinmollenhour/credis: ^1.10
- magento/framework: *
- magento/module-cache-invalidate: *
- magento/module-graph-ql: ^100.3
- magento/module-page-cache: *
- magento/module-store: *
Suggests
- magento/module-catalog: To invalidate persisted_query cache after full reindex
This package is auto-updated.
Last update: 2024-09-12 10:40:14 UTC
README
持久查询方法的主要目标是减少客户端到服务器在包含 GraphQl 文档的 POST 主体中传输的数据量。然而,当前模块扩展了持久查询的使用,实际上是将响应缓存到 Varnish 或 CDN 节点以实现快速响应。(平均 5ms 相比于开发机器上的 5000ms)
先决条件
- Varnish
- Redis
- 建议使用 PHP ext-phpredis 扩展以实现更快的序列化和反序列化
配置
magento setup:config:set
为了方便,为 php bin/magento setup:config:set
命令提供了额外的标志
--pq-host
[必填] - 持久查询 Redis 服务器地址(ScandiPWA Docker 设置中为 redis
)
--pq-port
[必填] - 持久查询 Redis 端口(ScandiPWA Docker 设置中为 6379
)
--pq-database
[必填] - 持久查询 Redis 数据库(ScandiPWA Docker 设置中为 5
)
--pq-scheme
[必填] - 持久查询 Redis 方案
--pq-password
[可选,不允许空密码] - 持久查询 Redis 密码
手动配置
针对自定义 Redis 存储的配置,其中散列和 GraphQl 文档保存在环境配置(app/etc/env.php
)中 -> cache/persisted-query
,并且可以手动配置
'persisted-query' => [
'redis' => [
'host' => 'redis',
'scheme' => 'tcp',
'port' => '6379',
'database' => '5'
]
]
缓存控制
从 v1.3.0 版本开始提供
CLI 命令 magento cache:flush
和管理员面板 缓存管理
具有必要的逻辑,用于刷新保存在 Varnish 中的 GraphQl 响应。
persisted_query_response
- 可以禁用,控制 varnish
缓存(graphql 响应缓存)。
bin/magento scandipwa:pq:flush
- 清除持久查询 REDIS 存储(查询体)
用法
动态持久查询假设客户端通过一系列请求-响应来注册未知查询。
推荐用法
- 乐观地请求查询执行,通过查询散列引用查询,并将必要的查询变量作为请求参数传递:
GET /graphql?hash=135811058&hideChildren=true
- 服务器有多种选择
- 以解析的查询响应(状态码
200
)响应 - 以未知查询错误(状态码
410
)响应
- 状态码
410
- 客户端必须发出带有相同散列和 整个 GraphQl 查询文档 的 PUT 请求。PUT /graphql?hash=135811058
- 服务器在成功注册查询后以状态码
201
响应。 - 服务器现在将执行通过散列引用的已注册查询
GET /graphql?hash=135811058
为了有效地利用持久查询机制并避免不必要的耗时请求-响应交互,GraphQl 查询必须使用变量。
变量
变量必须以伪 JSON 格式传递。您必须保持结构,但省略引号的使用。
数组
数组是一系列值的列表,用逗号分隔,例如:cmsBlocks_identifiers=homepage-promo-categories,homepage-top-items,homepage-about-us
让我们详细看看:cmsBlocks_identifiers
- GraphQl 变量名 homepage-promo-categories,homepage-top-items,homepage-about-us
- 值数组
复杂结构
复杂结构必须保持使用特殊字符({
、}
、:
)描述的结构。复杂结构中的数组必须使用特殊字符:[
、]
。
示例
_filter={category_url_path:{eq:men},max_price:{lteq:300},color:{in:[74,75]}}
让我们来看看细节: _filter
- GraphQl 变量名称 { category_url_path: { eq:men }, max_price:{ lteq:300 }, color:{ in:[74,75] } }
- "对象",作为 GET 参数传递。它没有引号,因为这些引号将由服务器在处理过程中自动添加。
示例
查询体
query ($cmsBlocks_identifiers:[String]) {cmsBlocks:cmsBlocks(identifiers:$cmsBlocks_identifiers){ items{ title, content, identifier } }}
请求 URI
GET /graphql?hash=2443957263&cmsBlocks_identifiers=homepage-promo-categories,homepage-top-items,homepage-about-us
查询体
"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 } } }}"
请求 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