Skip to content

Commit c2201b6

Browse files
PWA-2356: Fixing schema introspection test to override GQL types first
1 parent 6b196b5 commit c2201b6

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed

dev/tests/integration/testsuite/Magento/GraphQl/GraphQlIntrospectionTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Framework\GraphQl\Schema\Type\InputObjectType;
1111
use Magento\Framework\GraphQl\Schema\Type\ObjectType;
12+
use Magento\Framework\GraphQl\Type\TypeManagement;
1213
use Magento\Framework\ObjectManagerInterface;
1314

1415
class GraphQlIntrospectionTest extends \PHPUnit\Framework\TestCase
@@ -27,6 +28,8 @@ protected function setUp(): void
2728

2829
public function testIntrospectionQuery()
2930
{
31+
$typeManagement = new TypeManagement();
32+
$typeManagement->overrideStandardGraphQLTypes();
3033
$emptySchema = $this->schemaFactory->create(
3134
[
3235
'query' => new ObjectType(
@@ -272,10 +275,10 @@ public function testIntrospectsIncludeTheDeprecatedParameter()
272275
description
273276
isDeprecated
274277
deprecationReason
275-
278+
276279
}
277280
}
278-
}
281+
}
279282
280283
QUERY;
281284
$response = \GraphQL\GraphQL::executeQuery($testSchema, $request);

lib/internal/Magento/Framework/GraphQl/SchemaFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\Framework\GraphQl;
99

10+
use Magento\Framework\GraphQl\Type\TypeManagement;
11+
1012
/**
1113
* Factory for @see Schema
1214
*/
@@ -20,6 +22,8 @@ class SchemaFactory
2022
*/
2123
public function create(array $config) : Schema
2224
{
25+
$typeManagement = new TypeManagement();
26+
$typeManagement->overrideStandardGraphQLTypes();
2327
return new Schema($config);
2428
}
2529
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Type;
9+
10+
use GraphQL\GraphQL;
11+
use GraphQL\Type\Definition\Type as GraphQLType;
12+
use Magento\Framework\GraphQl\Type\Definition\FloatType;
13+
use Magento\Framework\GraphQl\Type\Definition\IntType;
14+
use Magento\Framework\GraphQl\Type\Definition\StringType;
15+
16+
/**
17+
* Class containing shared methods for GraphQL type management
18+
*/
19+
class TypeManagement
20+
{
21+
/**
22+
* Replace the standard type definitions with ones that know how to cast input values
23+
*/
24+
public function overrideStandardGraphQLTypes(): void
25+
{
26+
$standardTypes = GraphQLType::getStandardTypes();
27+
$overrideTypes = [];
28+
if (!($standardTypes[GraphQLType::INT] instanceof IntType)) {
29+
$overrideTypes[GraphQLType::INT] = new IntType($standardTypes[GraphQLType::INT]->config);
30+
}
31+
if (!($standardTypes[GraphQLType::FLOAT] instanceof FloatType)) {
32+
$overrideTypes[GraphQLType::FLOAT] = new FloatType($standardTypes[GraphQLType::FLOAT]->config);
33+
}
34+
if (!($standardTypes[GraphQLType::STRING] instanceof StringType)) {
35+
$overrideTypes[GraphQLType::STRING] = new StringType($standardTypes[GraphQLType::STRING]->config);
36+
}
37+
if ($overrideTypes) {
38+
GraphQL::overrideStandardTypes($overrideTypes);
39+
}
40+
}
41+
}

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77

88
namespace Magento\Framework\GraphQlSchemaStitching;
99

10-
use GraphQL\GraphQL;
1110
use GraphQL\Type\Definition\ScalarType;
12-
use GraphQL\Type\Definition\Type as GraphQLType;
1311
use GraphQL\Utils\BuildSchema;
1412
use Magento\Framework\Component\ComponentRegistrar;
1513
use Magento\Framework\Config\FileResolverInterface;
1614
use Magento\Framework\Config\ReaderInterface;
17-
use Magento\Framework\GraphQl\Type\Definition\FloatType;
18-
use Magento\Framework\GraphQl\Type\Definition\IntType;
19-
use Magento\Framework\GraphQl\Type\Definition\StringType;
15+
use Magento\Framework\GraphQl\Type\TypeManagement;
2016
use Magento\Framework\GraphQlSchemaStitching\GraphQlReader\TypeMetaReaderInterface as TypeReaderComposite;
2117
use Magento\Framework\GraphQlSchemaStitching\GraphQlReader\Reader\InterfaceType;
2218

@@ -78,7 +74,8 @@ public function __construct(
7874
$this->typeReader = $typeReader;
7975
$this->defaultScope = $defaultScope;
8076
$this->fileName = $fileName;
81-
$this->overrideStandardGraphQLTypes();
77+
$typeManagement = new TypeManagement();
78+
$typeManagement->overrideStandardGraphQLTypes();
8279
}
8380

8481
/**
@@ -352,22 +349,4 @@ private function addModuleNameToTypes(array $source, string $filePath): array
352349

353350
return $source;
354351
}
355-
356-
/**
357-
* Replace the standard type definitions with ones that know how to cast input values
358-
*/
359-
private function overrideStandardGraphQLTypes(): void
360-
{
361-
if (!self::$typesOverridden) {
362-
$standardTypes = GraphQLType::getStandardTypes();
363-
364-
GraphQL::overrideStandardTypes([
365-
GraphQLType::INT => new IntType($standardTypes[GraphQLType::INT]->config),
366-
GraphQLType::FLOAT => new FloatType($standardTypes[GraphQLType::FLOAT]->config),
367-
GraphQLType::STRING => new StringType($standardTypes[GraphQLType::STRING]->config)
368-
]);
369-
370-
self::$typesOverridden = true;
371-
}
372-
}
373352
}

0 commit comments

Comments
 (0)