Skip to content

Commit

Permalink
GraphQl-93: Implement support for variables in query
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav committed Jan 17, 2019
1 parent a3544dc commit 6f585f4
Show file tree
Hide file tree
Showing 17 changed files with 338 additions and 345 deletions.
16 changes: 4 additions & 12 deletions app/code/Magento/GraphQl/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<argument name="factoryMapByConfigElementType" xsi:type="array">
<item name="graphql_interface" xsi:type="object">Magento\Framework\GraphQl\Config\Element\InterfaceFactory</item>
<item name="graphql_type" xsi:type="object">Magento\Framework\GraphQl\Config\Element\TypeFactory</item>
<item name="graphql_input" xsi:type="object">Magento\Framework\GraphQl\Config\Element\TypeFactory</item>
<item name="graphql_input" xsi:type="object">Magento\Framework\GraphQl\Config\Element\InputFactory</item>
<item name="graphql_enum" xsi:type="object">Magento\Framework\GraphQl\Config\Element\EnumFactory</item>
</argument>
</arguments>
Expand Down Expand Up @@ -55,24 +55,16 @@
</argument>
</arguments>
</virtualType>
<type name="Magento\Framework\GraphQl\Schema\Type\Output\OutputFactory">
<type name="Magento\Framework\GraphQl\Schema\Type\TypeRegistry">
<arguments>
<argument name="prototypes" xsi:type="array">
<argument name="configToTypeMap" xsi:type="array">
<item name="Magento\Framework\GraphQl\Config\Element\Type" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Output\OutputTypeObject</item>
<item name="Magento\Framework\GraphQl\Config\Element\Input" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Input\InputObjectType</item>
<item name="Magento\Framework\GraphQl\Config\Element\InterfaceType" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Output\OutputInterfaceObject</item>
<item name="Magento\Framework\GraphQl\Config\Element\Enum" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Enum\Enum</item>
</argument>
</arguments>
</type>
<type name="Magento\Framework\GraphQl\Schema\Type\Input\InputFactory">
<arguments>
<argument name="prototypes" xsi:type="array">
<item name="Magento\Framework\GraphQl\Config\Element\Type" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Input\InputObjectType</item>
<item name="Magento\Framework\GraphQl\Config\Element\InterfaceType" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Input\InputObjectType</item>
<item name="Magento\Framework\GraphQl\Config\Element\Enum" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Enum\Enum</item>
</argument>
</arguments>
</type>
<type name="Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper">
<arguments>
<argument name="formatter" xsi:type="object">Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper\FormatterComposite</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Enum implements ConfigElementInterface
public function __construct(
string $name,
array $values,
string $description = ""
string $description
) {
$this->name = $name;
$this->values = $values;
Expand Down
74 changes: 74 additions & 0 deletions lib/internal/Magento/Framework/GraphQl/Config/Element/Input.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\GraphQl\Config\Element;

/**
* Class representing 'input' GraphQL config element.
*/
class Input implements TypeInterface
{
/**
* @var string
*/
private $name;

/**
* @var Field[]
*/
private $fields;

/**
* @var string
*/
private $description;

/**
* @param string $name
* @param Field[] $fields
* @param string $description
*/
public function __construct(
string $name,
array $fields,
string $description
) {
$this->name = $name;
$this->fields = $fields;
$this->description = $description;
}

/**
* Get the type name.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* Get a list of fields that make up the possible return or input values of a type.
*
* @return Field[]
*/
public function getFields(): array
{
return $this->fields;
}

/**
* Get a human-readable description of the type.
*
* @return string
*/
public function getDescription(): string
{
return $this->description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\GraphQl\Config\Element;

use Magento\Framework\GraphQl\Config\ConfigElementFactoryInterface;
use Magento\Framework\GraphQl\Config\ConfigElementInterface;
use Magento\Framework\ObjectManagerInterface;

/**
* Factory for config elements of 'input' type.
*/
class InputFactory implements ConfigElementFactoryInterface
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var ArgumentFactory
*/
private $argumentFactory;

/**
* @var FieldFactory
*/
private $fieldFactory;

/**
* @param ObjectManagerInterface $objectManager
* @param ArgumentFactory $argumentFactory
* @param FieldFactory $fieldFactory
*/
public function __construct(
ObjectManagerInterface $objectManager,
ArgumentFactory $argumentFactory,
FieldFactory $fieldFactory
) {
$this->objectManager = $objectManager;
$this->argumentFactory = $argumentFactory;
$this->fieldFactory = $fieldFactory;
}

/**
* Instantiate an object representing 'input' GraphQL config element.
*
* @param array $data
* @return ConfigElementInterface
*/
public function createFromConfigData(array $data): ConfigElementInterface
{
$fields = [];
$data['fields'] = isset($data['fields']) ? $data['fields'] : [];
foreach ($data['fields'] as $field) {
$arguments = [];
foreach ($field['arguments'] as $argument) {
$arguments[$argument['name']] = $this->argumentFactory->createFromConfigData($argument);
}
$fields[$field['name']] = $this->fieldFactory->createFromConfigData(
$field,
$arguments
);
}
return $this->create(
$data,
$fields
);
}

/**
* Create type object based off array of configured GraphQL InputType data.
*
* Type data must contain name and the type's fields. Optional data includes description.
*
* @param array $typeData
* @param array $fields
* @return Input
*/
private function create(
array $typeData,
array $fields
): Input {
return $this->objectManager->create(
Input::class,
[
'name' => $typeData['name'],
'fields' => $fields,
'description' => isset($typeData['description']) ? $typeData['description'] : ''
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Magento\Framework\GraphQl\Config\Element;

/**
* Describes the configured data for a GraphQL interface type.
* Class representing 'interface' GraphQL config element.
*/
class InterfaceType implements TypeInterface
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public function __construct(
string $name,
string $typeResolver,
array $fields,
string $description = ""
string $description
) {
$this->name = $name;
$this->fields = $fields;
Expand Down
20 changes: 1 addition & 19 deletions lib/internal/Magento/Framework/GraphQl/Config/Element/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Magento\Framework\GraphQl\Config\Element;

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

/**
* @var string
*/
private $type;

/**
* @var string
*/
Expand All @@ -41,20 +36,17 @@ class Type implements TypeInterface
* @param string $name
* @param Field[] $fields
* @param string[] $interfaces
* @param string $type
* @param string $description
*/
public function __construct(
string $name,
array $fields,
array $interfaces,
string $type,
string $description
) {
$this->name = $name;
$this->fields = $fields;
$this->interfaces = $interfaces;
$this->type = $type;
$this->description = $description;
}

Expand Down Expand Up @@ -97,14 +89,4 @@ public function getDescription() : string
{
return $this->description;
}

/**
* Get a type.
*
* @return string
*/
public function getType() : string
{
return $this->type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public function createFromConfigData(array $data): ConfigElementInterface
}

/**
* Create type object based off array of configured GraphQL Output/InputType data.
* Create type object based off array of configured GraphQL Type data.
*
* Type data must contain name and the type's fields. Optional data includes 'implements' (i.e. the interfaces
* implemented by the types), and description. An InputType cannot implement an interface.
* implemented by the types), and description.
*
* @param array $typeData
* @param array $fields
Expand All @@ -92,7 +92,6 @@ public function create(
'name' => $typeData['name'],
'fields' => $fields,
'interfaces' => isset($typeData['implements']) ? $typeData['implements'] : [],
'type' => isset($typeData['type']) ? $typeData['type'] : '',
'description' => isset($typeData['description']) ? $typeData['description'] : ''
]
);
Expand Down
10 changes: 5 additions & 5 deletions lib/internal/Magento/Framework/GraphQl/Query/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Fields
*
* @return void
*/
public function setQuery($query, $variables = null)
public function setQuery($query, array $variables = null)
{
$queryFields = [];
try {
Expand All @@ -43,16 +43,16 @@ public function setQuery($query, $variables = null)
]
]
);
if (isset($variables)) {
$queryFields = array_merge($queryFields, $this->getVariables($variables));
}
} catch (\Exception $e) {
// If a syntax error is encountered do not collect fields
}
if (isset($queryFields['IntrospectionQuery'])) {
// It must be possible to query any fields during introspection query
$queryFields = [];
}
if (isset($variables)) {
$queryFields = array_merge($queryFields, $this->getVariables($variables));
}
$this->fieldsUsedInQuery = $queryFields;
}

Expand All @@ -75,7 +75,7 @@ public function getFieldsUsedInQuery()
*
* @return string[]
*/
private function getVariables($variables)
private function getVariables(array $variables): array
{
$fields = [];
foreach ($variables as $key => $value){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getEntityAttributesForEntityFromField(string $fieldName) : array
if (isset($this->attributesInstances[$fieldName])) {
return $this->attributesInstances[$fieldName]->getEntityAttributes();
} else {
throw new \LogicException(sprintf('There is no attrribute class assigned to field %1', $fieldName));
throw new \LogicException(sprintf('There is no attribute class assigned to field %1', $fieldName));
}
}
}
Loading

0 comments on commit 6f585f4

Please sign in to comment.