Skip to content

Commit c5b0a48

Browse files
committed
Added relatedToProducts and relatedToVariants GraphQL query arguments.
1 parent 9ef98ab commit c5b0a48

File tree

6 files changed

+185
-1
lines changed

6 files changed

+185
-1
lines changed

CHANGELOG-WIP.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# WIP Release notes for Commerce 5.6
1+
# WIP Release notes for Commerce 5.6
2+
3+
- Added `relatedToProducts` and `relatedToVariants` GraphQL query arguments, enabling queries for elements related to specific products or variants. ([#4202](https://github.com/craftcms/commerce/discussions/4202))

src/Plugin.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,19 @@
137137
use craft\events\RegisterComponentTypesEvent;
138138
use craft\events\RegisterElementExportersEvent;
139139
use craft\events\RegisterEmailMessagesEvent;
140+
use craft\events\RegisterGqlArgumentHandlersEvent;
140141
use craft\events\RegisterGqlEagerLoadableFields;
141142
use craft\events\RegisterGqlQueriesEvent;
142143
use craft\events\RegisterGqlSchemaComponentsEvent;
143144
use craft\events\RegisterGqlTypesEvent;
144145
use craft\events\RegisterUserPermissionsEvent;
145146
use craft\fields\Link;
146147
use craft\fixfks\controllers\RestoreController;
148+
use craft\commerce\gql\handlers\RelatedProducts;
149+
use craft\commerce\gql\handlers\RelatedVariants;
150+
use craft\commerce\gql\types\input\criteria\ProductRelation;
151+
use craft\commerce\gql\types\input\criteria\VariantRelation;
152+
use craft\gql\ArgumentManager;
147153
use craft\gql\ElementQueryConditionBuilder;
148154
use craft\helpers\ArrayHelper;
149155
use craft\helpers\Console;
@@ -311,6 +317,7 @@ public function init(): void
311317
$this->_registerGqlQueries();
312318
$this->_registerGqlComponents();
313319
$this->_registerGqlEagerLoadableFields();
320+
$this->_registerGqlArgumentHandlers();
314321
$this->_registerLinkTypes();
315322
$this->_registerCacheTypes();
316323
$this->_registerGarbageCollection();
@@ -1029,6 +1036,41 @@ private function _registerGqlEagerLoadableFields(): void
10291036
});
10301037
}
10311038

1039+
/**
1040+
* Register the Gql argument handlers
1041+
*
1042+
* @since 5.6.0
1043+
*/
1044+
private function _registerGqlArgumentHandlers(): void
1045+
{
1046+
Event::on(ArgumentManager::class, ArgumentManager::EVENT_DEFINE_GQL_ARGUMENT_HANDLERS, static function(RegisterGqlArgumentHandlersEvent $event) {
1047+
$event->handlers['relatedToProducts'] = RelatedProducts::class;
1048+
$event->handlers['relatedToVariants'] = RelatedVariants::class;
1049+
});
1050+
1051+
// Add relatedToProducts and relatedToVariants arguments to element queries
1052+
Event::on(Gql::class, Gql::EVENT_REGISTER_GQL_QUERIES, static function(RegisterGqlQueriesEvent $event) {
1053+
$relatedToProductsArg = [
1054+
'name' => 'relatedToProducts',
1055+
'type' => \GraphQL\Type\Definition\Type::listOf(ProductRelation::getType()),
1056+
'description' => 'Narrows the query results to elements that relate to a product list defined with this argument.',
1057+
];
1058+
$relatedToVariantsArg = [
1059+
'name' => 'relatedToVariants',
1060+
'type' => \GraphQL\Type\Definition\Type::listOf(VariantRelation::getType()),
1061+
'description' => 'Narrows the query results to elements that relate to a variant list defined with this argument.',
1062+
];
1063+
1064+
// Add the arguments to all relevant queries
1065+
foreach ($event->queries as $queryName => &$queryConfig) {
1066+
if (isset($queryConfig['args']) && is_array($queryConfig['args'])) {
1067+
$queryConfig['args']['relatedToProducts'] = $relatedToProductsArg;
1068+
$queryConfig['args']['relatedToVariants'] = $relatedToVariantsArg;
1069+
}
1070+
}
1071+
});
1072+
}
1073+
10321074
/**
10331075
* Register the cache types
10341076
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* @link https://craftcms.com/
4+
* @copyright Copyright (c) Pixel & Tonic, Inc.
5+
* @license https://craftcms.github.io/license/
6+
*/
7+
8+
namespace craft\commerce\gql\handlers;
9+
10+
use craft\commerce\elements\Product;
11+
use craft\gql\base\RelationArgumentHandler;
12+
13+
/**
14+
* Class RelatedProducts
15+
*
16+
* @author Pixel & Tonic, Inc. <[email protected]>
17+
* @since 5.6.0
18+
*/
19+
class RelatedProducts extends RelationArgumentHandler
20+
{
21+
protected string $argumentName = 'relatedToProducts';
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
protected function handleArgument($argumentValue): mixed
27+
{
28+
$argumentValue = parent::handleArgument($argumentValue);
29+
return $this->getIds(Product::class, $argumentValue);
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* @link https://craftcms.com/
4+
* @copyright Copyright (c) Pixel & Tonic, Inc.
5+
* @license https://craftcms.github.io/license/
6+
*/
7+
8+
namespace craft\commerce\gql\handlers;
9+
10+
use craft\commerce\elements\Variant;
11+
use craft\gql\base\RelationArgumentHandler;
12+
13+
/**
14+
* Class RelatedVariants
15+
*
16+
* @author Pixel & Tonic, Inc. <[email protected]>
17+
* @since 5.6.0
18+
*/
19+
class RelatedVariants extends RelationArgumentHandler
20+
{
21+
protected string $argumentName = 'relatedToVariants';
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
protected function handleArgument($argumentValue): mixed
27+
{
28+
$argumentValue = parent::handleArgument($argumentValue);
29+
return $this->getIds(Variant::class, $argumentValue);
30+
}
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* @link https://craftcms.com/
4+
* @copyright Copyright (c) Pixel & Tonic, Inc.
5+
* @license https://craftcms.github.io/license/
6+
*/
7+
8+
namespace craft\commerce\gql\types\input\criteria;
9+
10+
use craft\commerce\gql\arguments\elements\Product as ProductArguments;
11+
use craft\gql\arguments\RelationCriteria;
12+
use craft\gql\GqlEntityRegistry;
13+
use GraphQL\Type\Definition\InputObjectType;
14+
15+
/**
16+
* Class ProductRelation
17+
*
18+
* @author Pixel & Tonic, Inc. <[email protected]>
19+
* @since 5.6.0
20+
*/
21+
class ProductRelation extends InputObjectType
22+
{
23+
/**
24+
* @return mixed
25+
*/
26+
public static function getType(): mixed
27+
{
28+
$typeName = 'ProductRelationCriteriaInput';
29+
30+
return GqlEntityRegistry::getOrCreate($typeName, fn() => new InputObjectType([
31+
'name' => $typeName,
32+
'fields' => fn() => [
33+
...ProductArguments::getArguments(),
34+
...ProductArguments::getContentArguments(),
35+
...RelationCriteria::getArguments(),
36+
],
37+
]));
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* @link https://craftcms.com/
4+
* @copyright Copyright (c) Pixel & Tonic, Inc.
5+
* @license https://craftcms.github.io/license/
6+
*/
7+
8+
namespace craft\commerce\gql\types\input\criteria;
9+
10+
use craft\commerce\gql\arguments\elements\Variant as VariantArguments;
11+
use craft\gql\arguments\RelationCriteria;
12+
use craft\gql\GqlEntityRegistry;
13+
use GraphQL\Type\Definition\InputObjectType;
14+
15+
/**
16+
* Class VariantRelation
17+
*
18+
* @author Pixel & Tonic, Inc. <[email protected]>
19+
* @since 5.6.0
20+
*/
21+
class VariantRelation extends InputObjectType
22+
{
23+
/**
24+
* @return mixed
25+
*/
26+
public static function getType(): mixed
27+
{
28+
$typeName = 'VariantRelationCriteriaInput';
29+
30+
return GqlEntityRegistry::getOrCreate($typeName, fn() => new InputObjectType([
31+
'name' => $typeName,
32+
'fields' => fn() => [
33+
...VariantArguments::getArguments(),
34+
...VariantArguments::getContentArguments(),
35+
...RelationCriteria::getArguments(),
36+
],
37+
]));
38+
}
39+
}

0 commit comments

Comments
 (0)