Skip to content

Commit c38c279

Browse files
committed
Merge remote-tracking branch 'origin/AC-7976' into spartans_pr_15042024
2 parents ce095d5 + 332fd27 commit c38c279

File tree

11 files changed

+365
-5
lines changed

11 files changed

+365
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ public function dispatch(RequestInterface $request): ResponseInterface
187187
$result = [];
188188

189189
$schema = null;
190+
$query = $data['query'] ?? '';
190191
try {
191192
/** @var Http $request */
192193
$this->requestProcessor->validateRequest($request);
193-
$query = $data['query'] ?? '';
194194
$parsedQuery = $this->queryParser->parse($query);
195195
$data['parsedQuery'] = $parsedQuery;
196196

@@ -216,7 +216,7 @@ public function dispatch(RequestInterface $request): ResponseInterface
216216
$jsonResult->renderResult($this->httpResponse);
217217

218218
// log information about the query, unless it is an introspection query
219-
if (!isset($data['query']) || strpos($data['query'], 'IntrospectionQuery') === false) {
219+
if (strpos($query, 'IntrospectionQuery') === false) {
220220
$queryInformation = $this->logDataHelper->getLogData($request, $data, $schema, $this->httpResponse);
221221
$this->loggerPool->execute($queryInformation);
222222
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<item name="graphql_type" xsi:type="object">Magento\Framework\GraphQl\Config\Element\TypeFactory</item>
3636
<item name="graphql_input" xsi:type="object">Magento\Framework\GraphQl\Config\Element\InputFactory</item>
3737
<item name="graphql_enum" xsi:type="object">Magento\Framework\GraphQl\Config\Element\EnumFactory</item>
38+
<item name="graphql_scalar" xsi:type="object">Magento\Framework\GraphQl\Config\Element\ScalarFactory</item>
3839
</argument>
3940
</arguments>
4041
</type>
@@ -69,6 +70,7 @@
6970
<item name="Magento\Framework\GraphQl\Config\Element\InterfaceType" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Output\OutputInterfaceObject</item>
7071
<item name="Magento\Framework\GraphQl\Config\Element\UnionType" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Output\OutputUnionObject</item>
7172
<item name="Magento\Framework\GraphQl\Config\Element\Enum" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Enum\Enum</item>
73+
<item name="Magento\Framework\GraphQl\Config\Element\Scalar" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Scalar\Scalar</item>
7274
</argument>
7375
</arguments>
7476
</type>
@@ -95,6 +97,7 @@
9597
<item name="object_type" xsi:type="object">Magento\Framework\GraphQlSchemaStitching\GraphQlReader\Reader\ObjectType</item>
9698
<item name="input_object_type" xsi:type="object">Magento\Framework\GraphQlSchemaStitching\GraphQlReader\Reader\InputObjectType</item>
9799
<item name="interface_type" xsi:type="object">Magento\Framework\GraphQlSchemaStitching\GraphQlReader\Reader\InterfaceType</item>
100+
<item name="scalar_type" xsi:type="object">Magento\Framework\GraphQlSchemaStitching\GraphQlReader\Reader\ScalarType</item>
98101
</argument>
99102
</arguments>
100103
</type>

app/code/Magento/GraphQl/etc/schema.graphqls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ directive @typeResolver(class: String="") on UNION
4141

4242
directive @cache(cacheIdentity: String="" cacheable: Boolean=true) on QUERY
4343

44+
directive @implementation(class: String="") on SCALAR
45+
| OBJECT
46+
4447
type Query {
4548
}
4649

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\Framework\GraphQl\Config\Element;
9+
10+
use Magento\Framework\GraphQl\Config\ConfigElementInterface;
11+
12+
/**
13+
* Scalar element
14+
*/
15+
class Scalar implements ConfigElementInterface
16+
{
17+
18+
/**
19+
* @var string
20+
*/
21+
private $name;
22+
23+
/**
24+
* @var string
25+
*/
26+
private $description;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $implementation;
32+
33+
/**
34+
* @param string $name
35+
* @param string $description
36+
* @param string $implementation
37+
*/
38+
public function __construct(
39+
string $name,
40+
string $description,
41+
string $implementation
42+
) {
43+
$this->name = $name;
44+
$this->description = $description;
45+
$this->implementation = $implementation;
46+
}
47+
48+
/**
49+
* Returns scalar element name
50+
*
51+
* @return string
52+
*/
53+
public function getName(): string
54+
{
55+
return $this->name;
56+
}
57+
58+
/**
59+
* Returns scalar element description
60+
*
61+
* @return string
62+
*/
63+
public function getDescription(): string
64+
{
65+
return $this->description;
66+
}
67+
68+
/**
69+
* Returns scalar element implementation
70+
*
71+
* @return string
72+
*/
73+
public function getImplementation(): string
74+
{
75+
return $this->implementation;
76+
}
77+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
* Scalars Factory
16+
*/
17+
class ScalarFactory implements ConfigElementFactoryInterface
18+
{
19+
20+
/**
21+
* @var ObjectManagerInterface
22+
*/
23+
private $objectManager;
24+
25+
/**
26+
* @param ObjectManagerInterface $objectManager
27+
*/
28+
public function __construct(
29+
ObjectManagerInterface $objectManager
30+
) {
31+
$this->objectManager = $objectManager;
32+
}
33+
34+
/**
35+
* Create scalar element
36+
*
37+
* @param array $data
38+
* @return ConfigElementInterface
39+
*/
40+
public function createFromConfigData(array $data): ConfigElementInterface
41+
{
42+
return $this->objectManager->create(
43+
Scalar::class,
44+
[
45+
'name' => $data['name'],
46+
'description' => $data['description'] ?? $data['name'],
47+
'implementation' => $data['implementation']
48+
]
49+
);
50+
}
51+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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\Schema\Type;
9+
10+
/**
11+
* Wrapper for CustomScalarType
12+
*/
13+
class CustomScalarType extends \GraphQL\Type\Definition\CustomScalarType implements
14+
InputTypeInterface,
15+
OutputTypeInterface
16+
{
17+
18+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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\Schema\Type\Scalar;
9+
10+
/**
11+
* Custom Scalar
12+
*/
13+
interface CustomScalarInterface
14+
{
15+
/**
16+
* Serialize Value
17+
*
18+
* @param mixed $value
19+
* @return mixed
20+
*/
21+
public function serialize($value);
22+
23+
/**
24+
* Parse Value
25+
*
26+
* @param mixed $value
27+
* @return mixed
28+
*/
29+
public function parseValue($value);
30+
31+
/**
32+
* Parse literal
33+
*
34+
* @param mixed $valueNode
35+
* @param array|null $variables
36+
* @return mixed
37+
*/
38+
public function parseLiteral($valueNode, ?array $variables = null);
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Schema\Type\Scalar;
9+
10+
use GraphQL\Language\AST\Node;
11+
use Magento\Framework\GraphQl\Config\Element\Scalar as ScalarElement;
12+
use Magento\Framework\GraphQl\Schema\Type\CustomScalarType;
13+
14+
/**
15+
* Custom scalar type configuration processor
16+
*/
17+
class Scalar extends CustomScalarType
18+
{
19+
/**
20+
* @param ScalarElement $configElement
21+
* @param ScalarRegistry $scalarRegistry
22+
*/
23+
public function __construct(
24+
ScalarElement $configElement,
25+
ScalarRegistry $scalarRegistry
26+
) {
27+
$scalar = $scalarRegistry->get($configElement);
28+
$config = [
29+
'name' => $configElement->getName(),
30+
'description' => $configElement->getDescription(),
31+
'serialize' =>
32+
static function ($value) use ($scalar) {
33+
return $scalar->serialize($value);
34+
},
35+
'parseValue' =>
36+
static function ($value) use ($scalar) {
37+
return $scalar->parseValue($value);
38+
},
39+
'parseLiteral' =>
40+
static function (Node $valueNode, ?array $variables = null) use ($scalar) {
41+
return $scalar->parseLiteral($valueNode, $variables);
42+
},
43+
];
44+
parent::__construct($config);
45+
}
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Schema\Type\Scalar;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Scalar as ScalarElement;
11+
use Magento\Framework\ObjectManagerInterface;
12+
13+
/**
14+
* It provides method to retrieve custom scalar implementations
15+
*/
16+
class ScalarRegistry
17+
{
18+
/**
19+
* @var ObjectManagerInterface
20+
*/
21+
private ObjectManagerInterface $objectManager;
22+
23+
/**
24+
* @param ObjectManagerInterface $objectManager
25+
*/
26+
public function __construct(
27+
ObjectManagerInterface $objectManager
28+
) {
29+
$this->objectManager = $objectManager;
30+
}
31+
32+
/**
33+
* Create custom scalar
34+
*
35+
* @param ScalarElement $element
36+
* @return CustomScalarInterface
37+
*/
38+
public function get(ScalarElement $element) : CustomScalarInterface
39+
{
40+
return $this->objectManager->get($element->getImplementation());
41+
}
42+
}

lib/internal/Magento/Framework/GraphQlSchemaStitching/GraphQlReader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private function readPartialTypes(string $graphQlSchemaContent): array
193193

194194
foreach ($schema->getTypeMap() as $typeName => $typeMeta) {
195195
// Only process custom types and skip built-in object types
196-
if ((strpos($typeName, '__') !== 0 && (!$typeMeta instanceof ScalarType))) {
196+
if (!in_array($typeMeta, \GraphQL\Type\Definition\Type::builtInTypes())) {
197197
$type = $this->typeReader->read($typeMeta);
198198
if (!empty($type)) {
199199
$partialResults[$typeName] = $type;
@@ -264,7 +264,7 @@ function ($t) {
264264
*/
265265
private function parseTypes(string $graphQlSchemaContent): array
266266
{
267-
$typeKindsPattern = '(type|interface|union|enum|input)';
267+
$typeKindsPattern = '(type|interface|union|enum|input|scalar)';
268268
$typeNamePattern = '([_A-Za-z][_0-9A-Za-z]+)';
269269
$typeDefinitionPattern = '([^\{\}]*)(\{[^\}]*\})';
270270
$spacePattern = '[\s\t\n\r]+';
@@ -377,7 +377,7 @@ private function convertInterfacesToAnnotations(string $graphQlSchemaContent): s
377377
private function addPlaceHolderInSchema(string $graphQlSchemaContent): string
378378
{
379379
$placeholderField = self::GRAPHQL_PLACEHOLDER_FIELD_NAME;
380-
$typesKindsPattern = '(type|interface|input|union)';
380+
$typesKindsPattern = '(type|interface|input|union|scalar)';
381381
$enumKindsPattern = '(enum)';
382382
$typeNamePattern = '([_A-Za-z][_0-9A-Za-z]+)';
383383
$typeDefinitionPattern = '([^\{\}]*)(\{[\s\t\n\r^\}]*\})';

0 commit comments

Comments
 (0)