定制 scandipwa/persisted-query 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

scandipwa/persisted-query

Composer 安装命令:

composer require scandipwa/persisted-query

包简介

ScandiPWA persisted query module

README 文档

README

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

  1. Varnish
  2. Redis
  3. 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:

  1. Optimistically request query execution, referencing query by hash, passing necessary query variables as request parameters: GET /graphql?hash=135811058&hideChildren=true
  2. Server has multiple options:
  • respond with resolved query (status code 200)
  • respond with Unknown query error (status code 410)
  1. Status code 410 - client must issue PUT request, with the same hash and entire GraphQl query document within the body. PUT /graphql?hash=135811058
  2. Server responds with status code 201 on successful query registration.
  3. Server will now executes the registered query referenced by hashGET /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

scandipwa/persisted-query 适用场景与选型建议

scandipwa/persisted-query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 249.57k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2019 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 scandipwa/persisted-query 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 scandipwa/persisted-query 我们能提供哪些服务?
定制开发 / 二次开发

基于 scandipwa/persisted-query 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 249.57k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 14
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 5
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: OSL-3.0
  • 更新时间: 2019-03-12