Skip to content

Commit f7f85ac

Browse files
committed
MC-22838: Required input type values validation does not work correctly
1 parent 2e3073d commit f7f85ac

File tree

4 files changed

+86
-8
lines changed

4 files changed

+86
-8
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Framework;
9+
10+
use Magento\TestFramework\TestCase\GraphQlAbstract;
11+
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
12+
13+
/**
14+
* Test that required input parameters are properly validated on framework level
15+
*/
16+
class RequiredInputArgumentTest extends GraphQlAbstract
17+
{
18+
19+
/**
20+
* Test that a simple input value will be treated as required
21+
*
22+
* We should see error message from framework not the Resolver
23+
* urlResolver query has required input arg "url"
24+
*/
25+
public function testSimpleInputArgumentRequired()
26+
{
27+
$query = <<<QUERY
28+
{
29+
urlResolver{
30+
id
31+
type
32+
}
33+
}
34+
QUERY;
35+
36+
$expectedExceptionsMessage = 'GraphQL response contains errors:'
37+
. ' Field "urlResolver" argument "url" of type "String!" is required but not provided.';
38+
$this->expectException(ResponseContainsErrorsException::class);
39+
$this->expectExceptionMessage($expectedExceptionsMessage);
40+
41+
$this->graphQlQuery($query);
42+
}
43+
44+
/**
45+
* Test that a more complex required argument is handled properly
46+
*
47+
* updateCartItems mutation has required parameter input.cart_items.cart_item_id
48+
*/
49+
public function testInputObjectArgumentRequired()
50+
{
51+
$query = <<<QUERY
52+
mutation {
53+
updateCartItems(
54+
input: {
55+
cart_id: "foobar"
56+
cart_items: [
57+
{
58+
quantity: 2
59+
}
60+
]
61+
}
62+
) {
63+
cart {
64+
total_quantity
65+
}
66+
}
67+
}
68+
QUERY;
69+
70+
$expectedExceptionsMessage = 'GraphQL response contains errors:'
71+
. ' Field CartItemUpdateInput.cart_item_id of required type Int! was not provided.';
72+
$this->expectException(ResponseContainsErrorsException::class);
73+
$this->expectExceptionMessage($expectedExceptionsMessage);
74+
75+
$this->graphQlMutation($query);
76+
}
77+
}

lib/internal/Magento/Framework/GraphQl/Config.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,9 @@ public function getConfigElement(string $configElementName) : ConfigElementInter
6262
$fieldsInQuery = $this->queryFields->getFieldsUsedInQuery();
6363
if (isset($data['fields'])) {
6464
if (!empty($fieldsInQuery)) {
65-
foreach (array_keys($data['fields']) as $fieldName) {
66-
if (!isset($fieldsInQuery[$fieldName])) {
67-
unset($data['fields'][$fieldName]);
68-
}
69-
}
65+
$data['fieldsInQuery'] = array_intersect_key($data['fields'], $fieldsInQuery);
66+
ksort($data['fieldsInQuery']);
7067
}
71-
7268
ksort($data['fields']);
7369
}
7470

lib/internal/Magento/Framework/GraphQl/Config/Element/InterfaceFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ public function __construct(
4747

4848
/**
4949
* Instantiate an object representing 'interface' GraphQL config element.
50+
*
51+
* @param array $data
52+
* @return ConfigElementInterface
5053
*/
5154
public function createFromConfigData(array $data): ConfigElementInterface
5255
{
56+
$fieldsData = $data['fieldsInQuery'] ?? ($data['fields'] ?? []);
5357
$fields = [];
54-
foreach ($data['fields'] as $field) {
58+
foreach ($fieldsData as $field) {
5559
$arguments = [];
5660
foreach ($field['arguments'] as $argument) {
5761
$arguments[$argument['name']] = $this->argumentFactory->createFromConfigData($argument);

lib/internal/Magento/Framework/GraphQl/Config/Element/TypeFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public function __construct(
4646
*/
4747
public function createFromConfigData(array $data): ConfigElementInterface
4848
{
49-
$fields = isset($data['fields']) ? $this->fieldsFactory->createFromConfigData($data['fields']) : [];
49+
$fieldsData = $data['fieldsInQuery'] ?? ($data['fields'] ?? []);
50+
$fields = $this->fieldsFactory->createFromConfigData($fieldsData);
5051

5152
return $this->create(
5253
$data,

0 commit comments

Comments
 (0)