Skip to content

Commit dea2633

Browse files
committed
Merge remote-tracking branch 'origin/MC-21524-schema' into MC-21694-bucket
2 parents f5be3b1 + d621ef1 commit dea2633

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/IntrospectionQueryTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,124 @@ public function testIntrospectionQuery()
5656
$this->assertArrayHasKey('__schema', $this->graphQlQuery($query));
5757
}
5858

59+
/**
60+
* Tests that Introspection is allowed by default
61+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
62+
*/
63+
public function testIntrospectionQueryWithOnlySchema()
64+
{
65+
$query
66+
= <<<QUERY
67+
{
68+
__schema {
69+
queryType { name }
70+
types{
71+
...FullType
72+
}
73+
}
74+
}
75+
fragment FullType on __Type{
76+
name
77+
kind
78+
fields(includeDeprecated:true){
79+
name
80+
args{
81+
...InputValue
82+
}
83+
}
84+
}
85+
86+
fragment TypeRef on __Type {
87+
kind
88+
name
89+
ofType{
90+
kind
91+
name
92+
}
93+
}
94+
fragment InputValue on __InputValue {
95+
name
96+
description
97+
type { ...TypeRef }
98+
defaultValue
99+
}
100+
QUERY;
101+
$this->assertArrayHasKey('__schema', $this->graphQlQuery($query));
102+
$response = $this->graphQlQuery($query);
103+
104+
$query
105+
= <<<QUERY
106+
query IntrospectionQuery {
107+
__schema {
108+
queryType { name }
109+
types{
110+
...FullType
111+
}
112+
}
113+
}
114+
fragment FullType on __Type{
115+
name
116+
kind
117+
fields(includeDeprecated:true){
118+
name
119+
args{
120+
...InputValue
121+
}
122+
}
123+
}
124+
125+
fragment TypeRef on __Type {
126+
kind
127+
name
128+
ofType{
129+
kind
130+
name
131+
}
132+
}
133+
fragment InputValue on __InputValue {
134+
name
135+
description
136+
type { ...TypeRef }
137+
defaultValue
138+
}
139+
QUERY;
140+
$this->assertArrayHasKey('__schema', $this->graphQlQuery($query));
141+
$responseFields = $this->graphQlQuery($query);
142+
$this->assertResponseFields($response, $responseFields);
143+
$this->assertEquals($responseFields, $response);
144+
}
145+
146+
/**
147+
* Tests that Introspection is allowed by default
148+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
149+
*/
150+
public function testIntrospectionQueryWithOnlyType()
151+
{
152+
$query
153+
= <<<QUERY
154+
{
155+
__type(name:"Query")
156+
{
157+
name
158+
kind
159+
fields(includeDeprecated:true){
160+
name
161+
type{
162+
kind
163+
name
164+
}
165+
description
166+
isDeprecated
167+
deprecationReason
168+
}
169+
}
170+
}
171+
QUERY;
172+
$this->assertArrayHasKey('__type', $this->graphQlQuery($query));
173+
$response = $this->graphQlQuery($query);
174+
$this->assertNotEmpty($response['__type']['fields']);
175+
}
176+
59177
/**
60178
* Tests that Introspection Query with deprecated annotations on enum values, fields are read.
61179
*/

lib/internal/Magento/Framework/GraphQl/Query/Fields.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ public function setQuery($query, array $variables = null)
4646
if (isset($variables)) {
4747
$queryFields = array_merge($queryFields, $this->extractVariables($variables));
4848
}
49+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
4950
} catch (\Exception $e) {
5051
// If a syntax error is encountered do not collect fields
5152
}
52-
if (isset($queryFields['IntrospectionQuery'])) {
53+
if (isset($queryFields['IntrospectionQuery']) || (isset($queryFields['__schema'])) ||
54+
(isset($queryFields['__type']))) {
5355
// It must be possible to query any fields during introspection query
5456
$queryFields = [];
5557
}
@@ -80,6 +82,7 @@ private function extractVariables(array $variables): array
8082
$fields = [];
8183
foreach ($variables as $key => $value) {
8284
if (is_array($value)) {
85+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
8386
$fields = array_merge($fields, $this->extractVariables($value));
8487
}
8588
$fields[$key] = $key;

0 commit comments

Comments
 (0)