📊 State of Magento 2025

The Home for Magento 2 Excellence

Quality-tested Magento 2 modules. Explore. Evaluate. Elevate. #magento2

613 Modules
401 Ready
211 Need Help
🏆 Leaderboard
Top 10% by Downloads Potentially Abandoned v1.0.5

Magento2 Graphql View Model

hyva-themes/magento2-graphql-view-model

Hyvä Themes - GraphQL ViewModel module

2,396,586
Downloads
11x above avg
8
GitHub Stars
Below average
3y ago
Last Release
0
Open Issues
Build Passing
Ready to install

Build Tests

Composer Install
DI Compile
Templates

Code Quality

CS Coding Standard
29 warnings
L1 PHPStan

Tested on Magento 2.4.8-p3

Recent Test History

Each release is tested against the latest Magento version at that time.

v1.0.5 on Magento 2.4.8-p3
Dec 30, 2025

Share This Module's Status

Magento2 Graphql View Model Magento compatibility status badge

README

Loaded from GitHub

Hyvä Themes - GraphQL ViewModel module

Hyvä Themes

hyva-themes/magento2-graphql-view-model

Supported Magento Versions

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

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

  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

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.txt file within this package, and is also available online at http://opensource.org/licenses/osl-3.0.php.

This content is fetched directly from the module's GitHub repository. We are not the authors of this content and take no responsibility for its accuracy, completeness, or any consequences arising from its use.