hyva-themes / magento2-graphql-view-model

hyva-themes/magento2-graphql-view-model

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

magento2-module Compatibility: 2.4.7-2.4.9 Code Quality: Fail Tests: N/A Security: Pass BSD-3-Clause

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 [email protected]: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

The BSD-3-Clause License. Please see License File for more information.

No changelog yet

The vendor hasn't published a changelog. Tagged releases appear in the Versions tab.

Versions
Version Stability QA Status Compatibility Released
1.0.5 stable Fail Magento 2.4.7-2.4.9 Details 2023-03-13 17:01:18
1.0.4 stable Not tested Not yet tested Details 2023-01-07 10:28:47
1.0.3 stable Not tested Not yet tested Details 2022-03-01 14:26:17
1.0.2 stable Not tested Not yet tested Details 2022-01-12 16:25:17
1.0.1 stable Not tested Not yet tested Details 2021-06-11 08:26:12
1.0.0 stable Not tested Not yet tested Details 2021-05-28 11:57:41

Requires 3

Package Constraint
php >=7.3.0
magento/framework ^101.0.0 || ^102.0.0 || ^103.0.0
webonyx/graphql-php ^14.0.0 || ^15.0.0

Compatibility

Each Magento release line is installed on its supported PHP versions, then the module is built (DI compilation + static-content deploy) and its unit and integration suites are run. The matrix shows the lines and PHP versions the module is confirmed to install and run on. Code-quality results further down (phpstan, phpcs, …) are reported separately and never affect compatibility.

Compatibility matrix (Magento × PHP)
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 Pass Pass
2.4.8 Pass Pass
2.4.9 Pass Pass

Code Quality

Advisory checks against the module's source. Static analysis runs once across the whole module; PHPStan re-runs per Magento + PHP version because resolvable symbols differ between releases. These NEVER affect the Compatibility badge — a phpcs finding can't make a module incompatible.

Static analysis

Coding standards (phpcs), mess detection (phpmd), copy-pasted code (cpd), PHP cross-version compatibility, composer.json validity. Each runs once for the whole module.

Static analysis results
Tool Status Findings Summary
PHPCS Warning 3 3 warnings (ruleset: Magento2) — 1 auto-fixable with phpcbf
PHPMD Warning 4 4 rule violations (MissingImport:4)
Cpd Pass 0
Composer validate Pass 0

PHPStan

Type-checks the module's PHP against a real Magento install at the configured gate level. Re-runs per Magento and PHP version because resolvable symbols differ between releases. Cell → details modal.

PHPStan results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 6 6
2.4.8 6 6
2.4.9 6 6

Tests

Unit and integration suites, run for each applicable Magento and PHP version. A test failure speaks to the module's behaviour, not its compatibility with a Magento line, so it is reported here separately and never reddens the compatibility matrix.

Unit tests

Unit tests results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 N/A N/A
2.4.8 N/A N/A
2.4.9 N/A N/A

Integration tests

Integration tests results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 N/A N/A
2.4.8 N/A N/A
2.4.9 N/A N/A

Security

Security checks run directly against the module: an audit of its declared dependencies for known vulnerabilities (composer audit) and a scan of its source for malware and web-shell signatures. Each runs once. A malware detection fails the version outright.

Security results
Tool Status Findings Summary
Composer audit Pass 0
Malware scan Pass 0
License
BSD-3-Clause

More from hyva-themes

View vendor
Make it pay

Turn an existing module into recurring revenue.

If you already maintain a Magento 2 module on GitHub or GitLab, listing it on Packagento takes about five minutes. We mirror your tags, handle distribution signing, and route paid licenses through Stripe Connect, so you can keep shipping the way you already do.