PHPackages                             hyva-themes/magento2-graphql-view-model - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. hyva-themes/magento2-graphql-view-model

ActiveMagento2-module[API Development](/categories/api)

hyva-themes/magento2-graphql-view-model
=======================================

Provide ability to customize GraphQL queries and mutations for Hyvä themes.

1.0.5(3y ago)82.6M—8.1%41BSD-3-ClausePHPPHP &gt;=7.3.0

Since May 28Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/hyva-themes/magento2-graphql-view-model)[ Packagist](https://packagist.org/packages/hyva-themes/magento2-graphql-view-model)[ RSS](/packages/hyva-themes-magento2-graphql-view-model/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (9)Used By (1)

Hyvä Themes - GraphQL ViewModel module
======================================

[](#hyvä-themes---graphql-viewmodel-module)

[![Hyvä Themes](https://repository-images.githubusercontent.com/300568807/f00eb480-55b1-11eb-93d2-074c3edd2d07)](https://hyva.io/)

hyva-themes/magento2-graphql-view-model
---------------------------------------

[](#hyva-themesmagento2-graphql-view-model)

[![Supported Magento Versions](https://camo.githubusercontent.com/0b6d28a963c77797b2df0442dbf1b6340361b3125bd3ce1dc10ce7a7d1a1fdcf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d6167656e746f2d253230322e33253230253743253230322e342d627269676874677265656e2e7376673f6c6f676f3d6d6167656e746f266c6f6e6743616368653d74727565267374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/0b6d28a963c77797b2df0442dbf1b6340361b3125bd3ce1dc10ce7a7d1a1fdcf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d6167656e746f2d253230322e33253230253743253230322e342d627269676874677265656e2e7376673f6c6f676f3d6d6167656e746f266c6f6e6743616368653d74727565267374796c653d666c61742d737175617265)

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?
----------------

[](#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\GraphqlQueryEditor` which 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 the `GraphqlQueryEditor`

Usage
-----

[](#usage)

In `.phtml` templates, to make queries customizable, wrap them with the `GraphqlViewModel::query()` method:

```

```

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
------------

[](#installation)

1. 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

    ```
2. Enable module ```
    bin/magento module:enable Hyva_GraphqlViewModel

    ```

Configuration
-------------

[](#configuration)

No configuration needed.

### License

[](#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.txt` file within this package, and is also available online at .

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance58

Moderate activity, may be stable

Popularity49

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 92.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~130 days

Recently: every ~160 days

Total

6

Last Release

1162d ago

PHP version history (2 changes)1.0.0PHP ^7.3.0

1.0.3PHP &gt;=7.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/1905417db5fbd31a61553b29bd441fdc66945da7cfb14f1b20a72275c68184e8?d=identicon)[vinai](/maintainers/vinai)

![](https://www.gravatar.com/avatar/7cd0062c670f61aa554c5d30193848ac11031b6cdb8f8616249b95a235eb475d?d=identicon)[wigman](/maintainers/wigman)

---

Top Contributors

[![Vinai](https://avatars.githubusercontent.com/u/72463?v=4)](https://github.com/Vinai "Vinai (25 commits)")[![JeroenBoersma](https://avatars.githubusercontent.com/u/1163348?v=4)](https://github.com/JeroenBoersma "JeroenBoersma (1 commits)")[![schmengler](https://avatars.githubusercontent.com/u/367320?v=4)](https://github.com/schmengler "schmengler (1 commits)")

### Embed Badge

![Health badge](/badges/hyva-themes-magento2-graphql-view-model/health.svg)

```
[![Health](https://phpackages.com/badges/hyva-themes-magento2-graphql-view-model/health.svg)](https://phpackages.com/packages/hyva-themes-magento2-graphql-view-model)
```

###  Alternatives

[smile/elasticsuite

Magento 2 merchandising and search engine built on ElasticSearch

8044.5M33](/packages/smile-elasticsuite)[thecodingmachine/graphqlite

Write your GraphQL queries in simple to write controllers (using webonyx/graphql-php).

5723.1M30](/packages/thecodingmachine-graphqlite)[wheelpros/fitment-platform-api

Magento 2 (Open Source)

12.1k1.2k](/packages/wheelpros-fitment-platform-api)[graycore/magento2-cors

A Magento 2 module that enables CORS on the GraphQL and REST Apis

99516.1k](/packages/graycore-magento2-cors)[elgentos/module-prismicio

Magento 2 - Prismic.io integration

39112.0k3](/packages/elgentos-module-prismicio)[flowpack/media-ui

This module allows managing media assets including pictures, videos, audio and documents.

2184.5k2](/packages/flowpack-media-ui)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
