hyva-themes/magento2-graphql-view-model
Composer 安装命令:
composer require hyva-themes/magento2-graphql-view-model
包简介
Provide ability to customize GraphQL queries and mutations for Hyvä themes.
README 文档
README
hyva-themes/magento2-graphql-view-model
This module adds a GraphQL ViewModel to allow GraphQL queries and mutations to be customized before they are rendered in the output.
Compatible with Magento 2.3.4 and higher.
What does it do?
It provides:
\Hyva\GraphqlViewModel\ViewModel\GraphqlViewModel, to be accessed via the view model registry (or injected via Layout XML).\Hyva\GraphqlViewModel\Model\GraphqlQueryEditorwhich can be used to add fields and arguments to GraphQL queries.- The event
hyva_graphql_render_before_+ query identifier Event observers receive the query string and can manipulate it with theGraphqlQueryEditor
Usage
In .phtml templates, to make queries customizable, wrap them with the GraphqlViewModel::query() method:
<?= $gqlViewModel->query("product_list_query", " products(filter: {} pageSize: 20) { items { {$type}_products { sku id small_image { url } } } }", ['type' => $type]) ?>
The first argument is the event name suffix. The second argument is the query or mutation as a string. The third argument is optional and - if specified - will be merged into the event arguments.
In the above example the full event name would be hyva_graphql_render_before_product_list_query
To manipulate a query in an event observer, the GraphqlQueryEditor can be used:
public function execute(Observer $event) { $gqlEditor = new GraphqlQueryEditor(); // or use dependency injection $queryString = $event->getData('gql_container')->getData('query'); $linkType = $event->getData('type'); $path = ['products', 'items', ($linkType ? "{$linkType}_products" : 'products'), 'small_image']; // add a single field to a result object $queryString = $gqlEditor->addFieldIn($queryString, $path, 'url_webp'); // add multiple fields to a result object $queryString = $gqlEditor->addFieldIn($queryString, ['products', 'items', 'products', 'image'], 'label url_webp'); // add a query argument $queryString = $gqlEditor->addArgumentIn($queryString, ['products', 'filter', 'name'], 'match', 'Tank'); $queryString = $gqlEditor->addArgumentIn($queryString, ['products'], 'pageSize', 2); // set updated query back on container $event->getData('gql_container')->setData('query', $queryString); }
The result of the example method call
$gqlEditor->addFieldIn($queryString, ['products', 'items', 'products', 'small_image'], 'label url_webp')
is that in the query the fields at the specified path are set:
products { items { products { small_image { label url_webp } } } }
Both the addFieldIn and the addArgumentIn methods are idempotent, so if the specified values already exist in the
query string they are not changed.
The addArgumentIn method can be used to add new arguments to queries or mutations, or to overwrite values of existing arguments.
For more examples including inline fragments please have a look at the \Hyva\GraphqlViewModel\Model\GraphqlQueryEditorTest class.
Installation
- Install via composer
composer config repositories.hyva-themes/magento2-graphql-view-model git git@github.com:hyva-themes/magento2-graphql-view-model.git composer require hyva-themes/magento2-graphql-view-model - Enable module
bin/magento module:enable Hyva_GraphqlViewModel
Configuration
No configuration needed.
License
This package is licensed under the Open Software License (OSL 3.0).
- Copyright: Copyright © 2020-present Hyvä Themes. All rights reserved.
- License Text (OSL 3.0): The full text of the OSL 3.0 license can be found in the
LICENSE.txtfile within this package, and is also available online at http://opensource.org/licenses/osl-3.0.php.
hyva-themes/magento2-graphql-view-model 适用场景与选型建议
hyva-themes/magento2-graphql-view-model 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.95M 次下载、GitHub Stars 达 8, 最近一次更新时间为 2021 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hyva-themes/magento2-graphql-view-model 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hyva-themes/magento2-graphql-view-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.95M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2021-05-28