Skip to content

Commit a153e1a

Browse files
author
Alex Paliarush
committed
MAGETWO-82543: Create Webonyx wrappers
1 parent 5d17951 commit a153e1a

File tree

65 files changed

+1208
-249
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1208
-249
lines changed

app/code/Magento/GraphQl/Controller/GraphQl.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66

77
namespace Magento\GraphQl\Controller;
88

9-
use GraphQL\Error\FormattedError;
109
use Magento\Framework\App\FrontControllerInterface;
1110
use Magento\Framework\App\RequestInterface;
1211
use Magento\Framework\App\ResponseInterface;
1312
use Magento\Framework\Exception\LocalizedException;
1413
use Magento\Framework\Serialize\SerializerInterface;
1514
use Magento\Framework\Webapi\Response;
1615
use Magento\GraphQl\Model\SchemaGeneratorInterface;
16+
use Magento\Framework\GraphQl\Executor;
17+
use Magento\Framework\GraphQl\ExceptionFormatter;
1718

1819
/**
1920
* Front controller for web API GraphQL area.
@@ -35,19 +36,33 @@ class GraphQl implements FrontControllerInterface
3536
*/
3637
private $jsonSerializer;
3738

39+
/**
40+
* @var Executor
41+
*/
42+
private $graphQlExecutor;
43+
44+
/** @var ExceptionFormatter */
45+
private $graphQlError;
46+
3847
/**
3948
* @param Response $response
4049
* @param SchemaGeneratorInterface $schemaGenerator
4150
* @param SerializerInterface $jsonSerializer
51+
* @param Executor $graphQlExecutor
52+
* @param ExceptionFormatter $graphQlError
4253
*/
4354
public function __construct(
4455
Response $response,
4556
SchemaGeneratorInterface $schemaGenerator,
46-
SerializerInterface $jsonSerializer
57+
SerializerInterface $jsonSerializer,
58+
Executor $graphQlExecutor,
59+
ExceptionFormatter $graphQlError
4760
) {
4861
$this->response = $response;
4962
$this->schemaGenerator = $schemaGenerator;
5063
$this->jsonSerializer = $jsonSerializer;
64+
$this->graphQlExecutor = $graphQlExecutor;
65+
$this->graphQlError = $graphQlError;
5166
}
5267

5368
/**
@@ -68,15 +83,15 @@ public function dispatch(RequestInterface $request)
6883
throw new LocalizedException(__('Request content type must be application/json'));
6984
}
7085
$schema = $this->schemaGenerator->generate();
71-
$result = \GraphQL\GraphQL::execute(
86+
$result = $this->graphQlExecutor->execute(
7287
$schema,
7388
isset($data['query']) ? $data['query'] : '',
7489
null,
7590
null,
7691
isset($data['variables']) ? $data['variables'] : []
7792
);
7893
} catch (\Exception $error) {
79-
$result['extensions']['exception'] = FormattedError::createFromException($error);
94+
$result['extensions']['exception'] = $this->graphQlError->create($error);
8095
$this->response->setStatusCode(500);
8196
}
8297
$this->response->setBody($this->jsonSerializer->serialize($result))->setHeader(
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\GraphQl\Model;
8+
9+
use Magento\Framework\GraphQl\Argument\ValueParserInterface;
10+
use Magento\Framework\GraphQl\Argument\ArgumentValueInterface;
11+
12+
/**
13+
* Data object that holds the configuration for a argument of a field
14+
*/
15+
class ArgumentConfig
16+
{
17+
/**
18+
* @var ArgumentValueInterface|int|string|float|bool
19+
*/
20+
21+
private $defaultValue;
22+
23+
/**
24+
* @var ValueParserInterface
25+
*/
26+
private $valueParser;
27+
28+
/**
29+
* @param ArgumentValueInterface|int|string|float|bool|null $defaultValue
30+
* @param ValueParserInterface|null $valueParser
31+
*/
32+
public function __construct(
33+
$defaultValue = null,
34+
ValueParserInterface $valueParser = null
35+
) {
36+
$this->defaultValue = $defaultValue;
37+
$this->valueParser = $valueParser;
38+
}
39+
40+
/**
41+
* Return the default value
42+
*
43+
* @return ArgumentValueInterface|int|string|float|bool|null $defaultValue
44+
*/
45+
public function getDefaultValue()
46+
{
47+
return $this->defaultValue;
48+
}
49+
50+
/**
51+
* Return a value parser if one is set
52+
*
53+
* @return ValueParserInterface|null
54+
*/
55+
public function getValueParser()
56+
{
57+
return $this->valueParser;
58+
}
59+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\GraphQl\Model;
8+
9+
/**
10+
* Class that stores configuration for processing value of arguments for GraphQl fields
11+
*/
12+
class FieldConfig
13+
{
14+
/**
15+
* Map as array for classes that represent field arguments
16+
*
17+
* @var array
18+
*/
19+
private $config = [];
20+
21+
/**
22+
* @var array
23+
*/
24+
private $instances = [];
25+
26+
/**
27+
* @var ArgumentConfigFactory
28+
*/
29+
private $argumentConfigFactory;
30+
31+
/**
32+
* @param ArgumentConfigFactory $argumentConfigFactory
33+
* @param array $config
34+
*/
35+
public function __construct(ArgumentConfigFactory $argumentConfigFactory, array $config = [])
36+
{
37+
$this->config = $config;
38+
$this->argumentConfigFactory = $argumentConfigFactory;
39+
}
40+
41+
/**
42+
* Returns a field configuration that is configured through DI
43+
*
44+
* @param string $fieldName
45+
* @param array $arguments
46+
* @return ArgumentConfig[]
47+
*/
48+
public function getFieldConfig(string $fieldName, array $arguments)
49+
{
50+
if (isset($this->instances[$fieldName])) {
51+
return $this->instances[$fieldName];
52+
}
53+
if (isset($this->config[$fieldName])) {
54+
$this->instances[$fieldName] = [];
55+
foreach ($this->config[$fieldName] as $argumentName => $fieldConfig) {
56+
$this->instances[$fieldName][$argumentName] = $this->argumentConfigFactory->create(
57+
[
58+
'defaultValue' => isset($fieldConfig['defaultValue']) ? $fieldConfig['defaultValue'] : null,
59+
'valueParser'=> isset($fieldConfig['valueParser'])
60+
? $fieldConfig['valueParser']
61+
: null
62+
]
63+
);
64+
}
65+
foreach ($arguments as $argumentName => $argumentValue) {
66+
if (!isset($this->instances[$fieldName][$argumentName])) {
67+
$this->instances[$fieldName][$argumentName] = $this->argumentConfigFactory->create(
68+
[
69+
'defaultValue' => null,
70+
'valueParser'=> null
71+
]
72+
);
73+
}
74+
}
75+
} else {
76+
foreach (array_keys($arguments) as $argument) {
77+
$this->instances[$fieldName][$argument] = $this->argumentConfigFactory->create([
78+
'defaultValue' => null,
79+
'valueParser' => null
80+
]);
81+
}
82+
}
83+
return $this->instances[$fieldName];
84+
}
85+
}

app/code/Magento/GraphQl/Model/Resolver/Product.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
use Magento\Framework\Exception\NoSuchEntityException;
1111
use Magento\Framework\Serialize\SerializerInterface;
1212
use Magento\Framework\Webapi\ServiceOutputProcessor;
13-
use GraphQL\Type\Definition\ResolveInfo;
1413
use Magento\GraphQl\Model\ResolverInterface;
15-
use Magento\Framework\Exception\InputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1615

1716
/**
1817
* Product field resolver, used for GraphQL request processing.
@@ -60,15 +59,15 @@ public function __construct(
6059
/**
6160
* {@inheritdoc}
6261
*/
63-
public function resolve(array $args, ResolveInfo $info)
62+
public function resolve(array $args)
6463
{
6564
if (isset($args['sku'])) {
66-
return $this->getProduct($args['sku']);
65+
return $this->getProduct($args['sku']->getValue());
6766
} elseif (isset($args['id'])) {
68-
return $this->getProductById($args['id']);
67+
return $this->getProductById($args['id']->getValue());
6968
}
7069

71-
throw new InputException(__('Missing arguments for correct type resolution.'));
70+
throw new GraphQlInputException(__('Missing arguments for correct type resolution.'));
7271
}
7372

7473
/**

app/code/Magento/GraphQl/Model/Resolver/Products.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
namespace Magento\GraphQl\Model\Resolver;
88

99
use Magento\Catalog\Api\ProductRepositoryInterface;
10-
use GraphQL\Type\Definition\ResolveInfo;
1110
use Magento\GraphQl\Model\ResolverInterface;
12-
use Magento\GraphQl\Model\Resolver\Products\SearchCriteriaFactory;
11+
use Magento\Framework\GraphQl\Argument\SearchCriteria\Builder;
1312

1413
/**
1514
* Products field resolver, used for GraphQL request processing.
@@ -21,29 +20,27 @@ class Products implements ResolverInterface
2120
*/
2221
private $productRepository;
2322

24-
/** @var SearchCriteriaFactory */
25-
private $searchCriteriaFactory;
23+
/** @var Builder */
24+
private $searchCriteriaBuilder;
2625

2726
/**
2827
* @param ProductRepositoryInterface $productRepository
29-
* @param \Magento\GraphQl\Model\Resolver\Products\SearchCriteriaFactory
28+
* @param Builder $searchCriteriaBuilder
3029
*/
3130
public function __construct(
3231
ProductRepositoryInterface $productRepository,
33-
\Magento\GraphQl\Model\Resolver\Products\SearchCriteriaFactory $searchCriteriaFactory
32+
Builder $searchCriteriaBuilder
3433
) {
3534
$this->productRepository = $productRepository;
36-
$this->searchCriteriaFactory = $searchCriteriaFactory;
35+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
3736
}
3837

3938
/**
4039
* {@inheritdoc}
41-
* @throws \GraphQL\Error\Error
4240
*/
43-
public function resolve(array $args, ResolveInfo $info)
41+
public function resolve(array $args)
4442
{
45-
$searchCriteria = $this->searchCriteriaFactory->create($info);
46-
43+
$searchCriteria = $this->searchCriteriaBuilder->build($args);
4744
$itemsResults = $this->productRepository->getList($searchCriteria);
4845

4946
$items = $itemsResults->getItems();
@@ -56,10 +53,10 @@ public function resolve(array $args, ResolveInfo $info)
5653

5754
$maxPages = ceil($itemsResults->getTotalCount() / $searchCriteria->getPageSize());
5855
if ($searchCriteria->getCurrentPage() > $maxPages && $itemsResults->getTotalCount() > 0) {
59-
throw new \GraphQL\Error\Error(
60-
sprintf(
56+
throw new \Magento\Framework\GraphQl\Exception\GraphQlInputException(
57+
__(
6158
'The value specified in the currentPage attribute is greater than the number'
62-
. ' of pages available (%s).',
59+
. ' of pages available (%1).',
6360
$maxPages
6461
)
6562
);

0 commit comments

Comments
 (0)