Skip to content

Commit 6f585f4

Browse files
committed
GraphQl-93: Implement support for variables in query
1 parent a3544dc commit 6f585f4

File tree

17 files changed

+338
-345
lines changed

17 files changed

+338
-345
lines changed

app/code/Magento/GraphQl/etc/di.xml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<argument name="factoryMapByConfigElementType" xsi:type="array">
2828
<item name="graphql_interface" xsi:type="object">Magento\Framework\GraphQl\Config\Element\InterfaceFactory</item>
2929
<item name="graphql_type" xsi:type="object">Magento\Framework\GraphQl\Config\Element\TypeFactory</item>
30-
<item name="graphql_input" xsi:type="object">Magento\Framework\GraphQl\Config\Element\TypeFactory</item>
30+
<item name="graphql_input" xsi:type="object">Magento\Framework\GraphQl\Config\Element\InputFactory</item>
3131
<item name="graphql_enum" xsi:type="object">Magento\Framework\GraphQl\Config\Element\EnumFactory</item>
3232
</argument>
3333
</arguments>
@@ -55,24 +55,16 @@
5555
</argument>
5656
</arguments>
5757
</virtualType>
58-
<type name="Magento\Framework\GraphQl\Schema\Type\Output\OutputFactory">
58+
<type name="Magento\Framework\GraphQl\Schema\Type\TypeRegistry">
5959
<arguments>
60-
<argument name="prototypes" xsi:type="array">
60+
<argument name="configToTypeMap" xsi:type="array">
6161
<item name="Magento\Framework\GraphQl\Config\Element\Type" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Output\OutputTypeObject</item>
62+
<item name="Magento\Framework\GraphQl\Config\Element\Input" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Input\InputObjectType</item>
6263
<item name="Magento\Framework\GraphQl\Config\Element\InterfaceType" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Output\OutputInterfaceObject</item>
6364
<item name="Magento\Framework\GraphQl\Config\Element\Enum" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Enum\Enum</item>
6465
</argument>
6566
</arguments>
6667
</type>
67-
<type name="Magento\Framework\GraphQl\Schema\Type\Input\InputFactory">
68-
<arguments>
69-
<argument name="prototypes" xsi:type="array">
70-
<item name="Magento\Framework\GraphQl\Config\Element\Type" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Input\InputObjectType</item>
71-
<item name="Magento\Framework\GraphQl\Config\Element\InterfaceType" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Input\InputObjectType</item>
72-
<item name="Magento\Framework\GraphQl\Config\Element\Enum" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Enum\Enum</item>
73-
</argument>
74-
</arguments>
75-
</type>
7668
<type name="Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper">
7769
<arguments>
7870
<argument name="formatter" xsi:type="object">Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper\FormatterComposite</argument>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Enum implements ConfigElementInterface
3737
public function __construct(
3838
string $name,
3939
array $values,
40-
string $description = ""
40+
string $description
4141
) {
4242
$this->name = $name;
4343
$this->values = $values;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\Framework\GraphQl\Config\Element;
9+
10+
/**
11+
* Class representing 'input' GraphQL config element.
12+
*/
13+
class Input implements TypeInterface
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $name;
19+
20+
/**
21+
* @var Field[]
22+
*/
23+
private $fields;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $description;
29+
30+
/**
31+
* @param string $name
32+
* @param Field[] $fields
33+
* @param string $description
34+
*/
35+
public function __construct(
36+
string $name,
37+
array $fields,
38+
string $description
39+
) {
40+
$this->name = $name;
41+
$this->fields = $fields;
42+
$this->description = $description;
43+
}
44+
45+
/**
46+
* Get the type name.
47+
*
48+
* @return string
49+
*/
50+
public function getName(): string
51+
{
52+
return $this->name;
53+
}
54+
55+
/**
56+
* Get a list of fields that make up the possible return or input values of a type.
57+
*
58+
* @return Field[]
59+
*/
60+
public function getFields(): array
61+
{
62+
return $this->fields;
63+
}
64+
65+
/**
66+
* Get a human-readable description of the type.
67+
*
68+
* @return string
69+
*/
70+
public function getDescription(): string
71+
{
72+
return $this->description;
73+
}
74+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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\Framework\GraphQl\Config\Element;
9+
10+
use Magento\Framework\GraphQl\Config\ConfigElementFactoryInterface;
11+
use Magento\Framework\GraphQl\Config\ConfigElementInterface;
12+
use Magento\Framework\ObjectManagerInterface;
13+
14+
/**
15+
* Factory for config elements of 'input' type.
16+
*/
17+
class InputFactory implements ConfigElementFactoryInterface
18+
{
19+
/**
20+
* @var ObjectManagerInterface
21+
*/
22+
private $objectManager;
23+
24+
/**
25+
* @var ArgumentFactory
26+
*/
27+
private $argumentFactory;
28+
29+
/**
30+
* @var FieldFactory
31+
*/
32+
private $fieldFactory;
33+
34+
/**
35+
* @param ObjectManagerInterface $objectManager
36+
* @param ArgumentFactory $argumentFactory
37+
* @param FieldFactory $fieldFactory
38+
*/
39+
public function __construct(
40+
ObjectManagerInterface $objectManager,
41+
ArgumentFactory $argumentFactory,
42+
FieldFactory $fieldFactory
43+
) {
44+
$this->objectManager = $objectManager;
45+
$this->argumentFactory = $argumentFactory;
46+
$this->fieldFactory = $fieldFactory;
47+
}
48+
49+
/**
50+
* Instantiate an object representing 'input' GraphQL config element.
51+
*
52+
* @param array $data
53+
* @return ConfigElementInterface
54+
*/
55+
public function createFromConfigData(array $data): ConfigElementInterface
56+
{
57+
$fields = [];
58+
$data['fields'] = isset($data['fields']) ? $data['fields'] : [];
59+
foreach ($data['fields'] as $field) {
60+
$arguments = [];
61+
foreach ($field['arguments'] as $argument) {
62+
$arguments[$argument['name']] = $this->argumentFactory->createFromConfigData($argument);
63+
}
64+
$fields[$field['name']] = $this->fieldFactory->createFromConfigData(
65+
$field,
66+
$arguments
67+
);
68+
}
69+
return $this->create(
70+
$data,
71+
$fields
72+
);
73+
}
74+
75+
/**
76+
* Create type object based off array of configured GraphQL InputType data.
77+
*
78+
* Type data must contain name and the type's fields. Optional data includes description.
79+
*
80+
* @param array $typeData
81+
* @param array $fields
82+
* @return Input
83+
*/
84+
private function create(
85+
array $typeData,
86+
array $fields
87+
): Input {
88+
return $this->objectManager->create(
89+
Input::class,
90+
[
91+
'name' => $typeData['name'],
92+
'fields' => $fields,
93+
'description' => isset($typeData['description']) ? $typeData['description'] : ''
94+
]
95+
);
96+
}
97+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\Framework\GraphQl\Config\Element;
99

1010
/**
11-
* Describes the configured data for a GraphQL interface type.
11+
* Class representing 'interface' GraphQL config element.
1212
*/
1313
class InterfaceType implements TypeInterface
1414
{
@@ -42,7 +42,7 @@ public function __construct(
4242
string $name,
4343
string $typeResolver,
4444
array $fields,
45-
string $description = ""
45+
string $description
4646
) {
4747
$this->name = $name;
4848
$this->fields = $fields;

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\Framework\GraphQl\Config\Element;
99

1010
/**
11-
* Describes all the configured data of an Output or Input type in GraphQL.
11+
* Class representing 'type' GraphQL config element.
1212
*/
1313
class Type implements TypeInterface
1414
{
@@ -27,11 +27,6 @@ class Type implements TypeInterface
2727
*/
2828
private $interfaces;
2929

30-
/**
31-
* @var string
32-
*/
33-
private $type;
34-
3530
/**
3631
* @var string
3732
*/
@@ -41,20 +36,17 @@ class Type implements TypeInterface
4136
* @param string $name
4237
* @param Field[] $fields
4338
* @param string[] $interfaces
44-
* @param string $type
4539
* @param string $description
4640
*/
4741
public function __construct(
4842
string $name,
4943
array $fields,
5044
array $interfaces,
51-
string $type,
5245
string $description
5346
) {
5447
$this->name = $name;
5548
$this->fields = $fields;
5649
$this->interfaces = $interfaces;
57-
$this->type = $type;
5850
$this->description = $description;
5951
}
6052

@@ -97,14 +89,4 @@ public function getDescription() : string
9789
{
9890
return $this->description;
9991
}
100-
101-
/**
102-
* Get a type.
103-
*
104-
* @return string
105-
*/
106-
public function getType() : string
107-
{
108-
return $this->type;
109-
}
11092
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ public function createFromConfigData(array $data): ConfigElementInterface
7373
}
7474

7575
/**
76-
* Create type object based off array of configured GraphQL Output/InputType data.
76+
* Create type object based off array of configured GraphQL Type data.
7777
*
7878
* Type data must contain name and the type's fields. Optional data includes 'implements' (i.e. the interfaces
79-
* implemented by the types), and description. An InputType cannot implement an interface.
79+
* implemented by the types), and description.
8080
*
8181
* @param array $typeData
8282
* @param array $fields
@@ -92,7 +92,6 @@ public function create(
9292
'name' => $typeData['name'],
9393
'fields' => $fields,
9494
'interfaces' => isset($typeData['implements']) ? $typeData['implements'] : [],
95-
'type' => isset($typeData['type']) ? $typeData['type'] : '',
9695
'description' => isset($typeData['description']) ? $typeData['description'] : ''
9796
]
9897
);

lib/internal/Magento/Framework/GraphQl/Query/Fields.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Fields
2828
*
2929
* @return void
3030
*/
31-
public function setQuery($query, $variables = null)
31+
public function setQuery($query, array $variables = null)
3232
{
3333
$queryFields = [];
3434
try {
@@ -43,16 +43,16 @@ public function setQuery($query, $variables = null)
4343
]
4444
]
4545
);
46+
if (isset($variables)) {
47+
$queryFields = array_merge($queryFields, $this->getVariables($variables));
48+
}
4649
} catch (\Exception $e) {
4750
// If a syntax error is encountered do not collect fields
4851
}
4952
if (isset($queryFields['IntrospectionQuery'])) {
5053
// It must be possible to query any fields during introspection query
5154
$queryFields = [];
5255
}
53-
if (isset($variables)) {
54-
$queryFields = array_merge($queryFields, $this->getVariables($variables));
55-
}
5656
$this->fieldsUsedInQuery = $queryFields;
5757
}
5858

@@ -75,7 +75,7 @@ public function getFieldsUsedInQuery()
7575
*
7676
* @return string[]
7777
*/
78-
private function getVariables($variables)
78+
private function getVariables(array $variables): array
7979
{
8080
$fields = [];
8181
foreach ($variables as $key => $value){

lib/internal/Magento/Framework/GraphQl/Query/Resolver/Argument/FieldEntityAttributesPool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function getEntityAttributesForEntityFromField(string $fieldName) : array
3838
if (isset($this->attributesInstances[$fieldName])) {
3939
return $this->attributesInstances[$fieldName]->getEntityAttributes();
4040
} else {
41-
throw new \LogicException(sprintf('There is no attrribute class assigned to field %1', $fieldName));
41+
throw new \LogicException(sprintf('There is no attribute class assigned to field %1', $fieldName));
4242
}
4343
}
4444
}

0 commit comments

Comments
 (0)