Skip to content

Commit 1589039

Browse files
committed
MAGETWO-83855: Create wrapper for resolver info
- introducing factories on module graphql wrappers types
1 parent 0e61ec0 commit 1589039

20 files changed

+328
-111
lines changed

app/code/Magento/GraphQl/Model/SchemaGenerator.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
namespace Magento\GraphQl\Model;
88

99
use GraphQl\Type\Definition\ResolveInfo;
10-
use Magento\Framework\GraphQl\Type\Definition\ObjectType;
10+
use Magento\Framework\GraphQl\Type\SchemaFactory;
1111
use Magento\Framework\GraphQl\Type\Schema;
1212
use Magento\GraphQl\Model\Type\Generator;
1313
use Magento\Framework\GraphQl\ArgumentFactory;
14+
use Magento\Framework\GraphQl\Type\TypeFactory;
1415

1516
/**
1617
* Generate a query field and concrete types for GraphQL schema
@@ -37,22 +38,38 @@ class SchemaGenerator implements SchemaGeneratorInterface
3738
*/
3839
private $fieldConfig;
3940

41+
/**
42+
* @var TypeFactory
43+
*/
44+
private $typeFactory;
45+
46+
/**
47+
* @var SchemaFactory
48+
*/
49+
private $schemaFactory;
50+
4051
/**
4152
* @param Generator $typeGenerator
4253
* @param ResolverFactory $resolverFactory
4354
* @param ArgumentFactory $argumentFactory
4455
* @param FieldConfig $fieldConfig
56+
* @param TypeFactory $typeFactory
57+
* @param SchemaFactory $schemaFactory
4558
*/
4659
public function __construct(
4760
Generator $typeGenerator,
4861
ResolverFactory $resolverFactory,
4962
ArgumentFactory $argumentFactory,
50-
FieldConfig $fieldConfig
63+
FieldConfig $fieldConfig,
64+
TypeFactory $typeFactory,
65+
SchemaFactory $schemaFactory
5166
) {
5267
$this->typeGenerator = $typeGenerator;
5368
$this->resolverFactory = $resolverFactory;
5469
$this->argumentFactory = $argumentFactory;
5570
$this->fieldConfig = $fieldConfig;
71+
$this->typeFactory = $typeFactory;
72+
$this->schemaFactory = $schemaFactory;
5673
}
5774

5875
/**
@@ -62,7 +79,8 @@ public function __construct(
6279
public function generate()
6380
{
6481
$schemaConfig = $this->typeGenerator->generateTypes('Query');
65-
$config = new ObjectType([
82+
83+
$config = $this->typeFactory->createObject([
6684
'name' => 'Query',
6785
'fields' => $schemaConfig['fields'],
6886
'resolveField' => function ($value, $args, $context, ResolveInfo $info) {
@@ -88,7 +106,7 @@ public function generate()
88106
return $resolver->resolve($fieldArguments);
89107
}
90108
]);
91-
$schema = new Schema(['query' => $config, 'types' => $schemaConfig['types']]);
109+
$schema = $this->schemaFactory->create(['query' => $config, 'types' => $schemaConfig['types']]);
92110
return $schema;
93111
}
94112
}

app/code/Magento/GraphQl/Model/Type/Generator.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\GraphQl\Type\Definition\Type;
1212
use Magento\Framework\Exception\LocalizedException;
1313
use Magento\GraphQl\Model\Type\Handler\Pool;
14+
use Magento\Framework\GraphQl\Type\TypeFactory;
1415

1516
/**
1617
* {@inheritdoc}
@@ -27,14 +28,21 @@ class Generator implements GeneratorInterface
2728
*/
2829
private $typeMap;
2930

31+
/**
32+
* @var TypeFactory
33+
*/
34+
private $typeFactory;
35+
3036
/**
3137
* @param Pool $typePool
3238
* @param array $typeMap
39+
* @param TypeFactory $typeFactory
3340
*/
34-
public function __construct(Pool $typePool, array $typeMap)
41+
public function __construct(Pool $typePool, array $typeMap, TypeFactory $typeFactory)
3542
{
3643
$this->typePool = $typePool;
3744
$this->typeMap = $typeMap;
45+
$this->typeFactory = $typeFactory;
3846
}
3947

4048
/**
@@ -82,8 +90,8 @@ public function generateTypes(string $typeName)
8290
private function decorateType(string $argumentName, string $argumentType)
8391
{
8492
$type = $this->typePool->getType($argumentType);
85-
$type = strpos($argumentName, '!') !== false ? new NonNull($type) : $type;
86-
$type = strpos($argumentName, '[]') !== false ? new ListOfType($type) : $type;
93+
$type = strpos($argumentName, '!') !== false ? $this->typeFactory->createNonNull($type) : $type;
94+
$type = strpos($argumentName, '[]') !== false ? $this->typeFactory->createList($type) : $type;
8795

8896
return $type;
8997
}

app/code/Magento/GraphQl/Model/Type/Handler/AttributeInput.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\GraphQl\Type\Definition\InputObjectType;
1010
use Magento\GraphQl\Model\Type\HandlerInterface;
11+
use Magento\Framework\GraphQl\Type\TypeFactory;
1112

1213
/**
1314
* Defines input type for attributes in ['attribute_code' => 'value', 'entity_type' => 'value'] format
@@ -19,12 +20,19 @@ class AttributeInput implements HandlerInterface
1920
*/
2021
private $typePool;
2122

23+
/**
24+
* @var TypeFactory
25+
*/
26+
private $typeFactory;
27+
2228
/**
2329
* @param Pool $typePool
30+
* @param TypeFactory $typeFactory
2431
*/
25-
public function __construct(Pool $typePool)
32+
public function __construct(Pool $typePool, TypeFactory $typeFactory)
2633
{
2734
$this->typePool = $typePool;
35+
$this->typeFactory = $typeFactory;
2836
}
2937

3038
/**
@@ -34,7 +42,7 @@ public function getType()
3442
{
3543
$reflector = new \ReflectionClass($this);
3644

37-
return new InputObjectType(
45+
return $this->typeFactory->createInputObject(
3846
[
3947
'name' => $reflector->getShortName(),
4048
'fields' => [

app/code/Magento/GraphQl/Model/Type/Handler/ConfigurableProduct.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\GraphQl\Type\Definition\ListOfType;
1010
use Magento\Framework\GraphQl\Type\Definition\ObjectType;
1111
use Magento\GraphQl\Model\Type\HandlerInterface;
12+
use Magento\Framework\GraphQl\Type\TypeFactory;
1213

1314
/**
1415
* Define ConfigurableProduct's GraphQL type
@@ -20,12 +21,19 @@ class ConfigurableProduct implements HandlerInterface
2021
*/
2122
private $typePool;
2223

24+
/**
25+
* @var TypeFactory
26+
*/
27+
private $typeFactory;
28+
2329
/**
2430
* @param Pool $typePool
31+
* @param TypeFactory $typeFactory
2532
*/
26-
public function __construct(Pool $typePool)
33+
public function __construct(Pool $typePool, TypeFactory $typeFactory)
2734
{
2835
$this->typePool = $typePool;
36+
$this->typeFactory = $typeFactory;
2937
}
3038

3139
/**
@@ -37,8 +45,11 @@ public function getType()
3745
$fields = [];
3846
$interface = $this->typePool->getType('Product');
3947
$fields = array_merge($fields, $interface->config['fields']);
40-
$fields['configurable_product_links'] = new ListOfType($this->typePool->getComplexType('SimpleProduct'));
41-
return new ObjectType(
48+
$fields['configurable_product_links'] = $this->typeFactory->createList(
49+
$this->typePool->getComplexType('SimpleProduct')
50+
);
51+
52+
return $this->typeFactory->createObject(
4253
[
4354
'name' => $reflector->getShortName(),
4455
'fields' => $fields,

app/code/Magento/GraphQl/Model/Type/Handler/CustomAttributeMetadata.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\GraphQl\Type\Definition\ListOfType;
1010
use Magento\Framework\GraphQl\Type\Definition\ObjectType;
1111
use Magento\GraphQl\Model\Type\HandlerInterface;
12+
use Magento\Framework\GraphQl\Type\TypeFactory;
1213

1314
/**
1415
* Defines type information for custom attribute metadata
@@ -20,12 +21,19 @@ class CustomAttributeMetadata implements HandlerInterface
2021
*/
2122
private $typePool;
2223

24+
/**
25+
* @var TypeFactory
26+
*/
27+
private $typeFactory;
28+
2329
/**
2430
* @param Pool $typePool
31+
* @param TypeFactory $typeFactory
2532
*/
26-
public function __construct(Pool $typePool)
33+
public function __construct(Pool $typePool, TypeFactory $typeFactory)
2734
{
2835
$this->typePool = $typePool;
36+
$this->typeFactory = $typeFactory;
2937
}
3038

3139
/**
@@ -35,7 +43,7 @@ public function getType()
3543
{
3644
$reflector = new \ReflectionClass($this);
3745

38-
return new ObjectType(
46+
return $this->typeFactory->createObject(
3947
[
4048
'name' => $reflector->getShortName(),
4149
'fields' => $this->getFields()
@@ -51,7 +59,7 @@ public function getType()
5159
private function getFields(): array
5260
{
5361
if (!$this->typePool->isTypeRegistered('Attribute')) {
54-
$attributeType = new ObjectType([
62+
$attributeType = $this->typeFactory->createObject([
5563
'name' => 'Attribute',
5664
'fields' => [
5765
'attribute_code' => $this->typePool->getType('String'),
@@ -62,6 +70,6 @@ private function getFields(): array
6270
$this->typePool->registerType($attributeType);
6371
}
6472

65-
return ['items' => new ListOfType($this->typePool->getType('Attribute'))];
73+
return ['items' => $this->typeFactory->createList($this->typePool->getType('Attribute'))];
6674
}
6775
}

app/code/Magento/GraphQl/Model/Type/Handler/Pool.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,51 @@
88

99
use Magento\Framework\GraphQl\Type\Definition\TypeInterface;
1010
use Magento\GraphQl\Model\Type\HandlerFactory;
11-
use Magento\Framework\GraphQl\Type\Definition\ScalarTypeFactory;
11+
use Magento\Framework\GraphQl\Type\TypeFactory;
1212

1313
/**
1414
* Retrieve type's registered in pool, or generate types yet to be instantiated and register them
1515
*/
1616
class Pool
1717
{
18+
const STRING = 'String';
19+
const INT = 'Int';
20+
const BOOLEAN = 'Boolean';
21+
const FLOAT = 'Float';
22+
const ID = 'ID';
23+
1824
/**
1925
* @var HandlerFactory
2026
*/
2127
private $typeHandlerFactory;
2228

29+
/**
30+
* @var TypeFactory
31+
*/
32+
private $typeFactory;
33+
2334
/**
2435
* @var TypeInterface[]
2536
*/
2637
private $typeRegistry = [];
2738

2839
/**
2940
* @param HandlerFactory $typeHandlerFactory
30-
* @param ScalarTypeFactory $scalarTypeFactory
41+
* @param TypeFactory $typeFactory
3142
*/
3243
public function __construct(
3344
HandlerFactory $typeHandlerFactory,
34-
ScalarTypeFactory $scalarTypeFactory
45+
TypeFactory $typeFactory
3546
) {
3647
$this->typeHandlerFactory = $typeHandlerFactory;
37-
$this->scalarTypeFactory = $scalarTypeFactory;
48+
$this->typeFactory = $typeFactory;
3849
}
3950

4051
/**
52+
* Get a Type
53+
*
4154
* @param string $typeName
42-
* @return TypeInterface|\GraphQL\Type\Definition\Type
55+
* @return TypeInterface
4356
* @throws \LogicException
4457
*/
4558
public function getType(string $typeName)
@@ -48,8 +61,8 @@ public function getType(string $typeName)
4861
return $this->typeRegistry[$typeName];
4962
}
5063

51-
if ($this->scalarTypeFactory->typeExists($typeName)) {
52-
$this->typeRegistry[$typeName] = $this->scalarTypeFactory->create($typeName);
64+
if ($this->isScalar($typeName)) {
65+
$this->typeRegistry[$typeName] = $this->typeFactory->createScalar($typeName);
5366
return $this->typeRegistry[$typeName];
5467
} else {
5568
return $this->getComplexType($typeName);
@@ -82,7 +95,7 @@ public function getComplexType(string $typeName)
8295
/**
8396
* Register type to Pool's type registry.
8497
*
85-
* @param TypeInterface $type
98+
* @param TypeInterface|\GraphQL\Type\Definition\Type $type
8699
* @throws \LogicException
87100
*/
88101
public function registerType(TypeInterface $type)
@@ -103,4 +116,21 @@ public function isTypeRegistered(string $typeName)
103116
{
104117
return isset($this->typeRegistry[$typeName]);
105118
}
119+
120+
/**
121+
* If type is a scalar type
122+
*
123+
* @param string $typeName
124+
* @return bool
125+
*/
126+
private function isScalar(string $typeName)
127+
{
128+
$type = new \ReflectionClass(self::class);
129+
$constants = $type->getConstants();
130+
if (in_array($typeName, $constants)) {
131+
return true;
132+
} else {
133+
return false;
134+
}
135+
}
106136
}

app/code/Magento/GraphQl/Model/Type/Handler/Product.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Exception\InputException;
1313
use Magento\GraphQl\Model\Type\Helper\ServiceContract\TypeGenerator;
1414
use Magento\GraphQl\Model\Type\HandlerInterface;
15+
use Magento\Framework\GraphQl\Type\TypeFactory;
1516

1617
/**
1718
* Define Product's GraphQL type
@@ -33,16 +34,27 @@ class Product implements HandlerInterface
3334
*/
3435
private $management;
3536

37+
/**
38+
* @var TypeFactory
39+
*/
40+
private $typeFactory;
41+
3642
/**
3743
* @param Pool $typePool
3844
* @param TypeGenerator $typeGenerator
3945
* @param AttributeManagementInterface $management
46+
* @param TypeFactory $typeFactory
4047
*/
41-
public function __construct(Pool $typePool, TypeGenerator $typeGenerator, AttributeManagementInterface $management)
42-
{
48+
public function __construct(
49+
Pool $typePool,
50+
TypeGenerator $typeGenerator,
51+
AttributeManagementInterface $management,
52+
TypeFactory $typeFactory
53+
) {
4354
$this->typePool = $typePool;
4455
$this->typeGenerator = $typeGenerator;
4556
$this->management = $management;
57+
$this->typeFactory = $typeFactory;
4658
}
4759

4860
/**
@@ -51,7 +63,7 @@ public function __construct(Pool $typePool, TypeGenerator $typeGenerator, Attrib
5163
public function getType()
5264
{
5365
$reflector = new \ReflectionClass($this);
54-
return new InterfaceType(
66+
return $this->typeFactory->createInterface(
5567
[
5668
'name' => $reflector->getShortName(),
5769
'fields' => $this->getFields($reflector->getShortName()),

0 commit comments

Comments
 (0)