Skip to content

Commit d83d502

Browse files
authored
change operation to OperationName in the parser (#3568)
1 parent cfc05d1 commit d83d502

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* GraphQL: Add page-based pagination (#3175, #3517)
1010
* GraphQL: Possibility to add a custom description for queries, mutations and subscriptions (#3477, #3514)
1111
* GraphQL: Support for field name conversion (serialized name) (#3455, #3516)
12+
* GraphQL: **BC** `operation` is now `operationName` to follow the standard (#3568)
1213
* OpenAPI: Add PHP default values to the documentation (#2386)
1314
* Deprecate using a validation groups generator service not implementing `ApiPlatform\Core\Bridge\Symfony\Validator\ValidationGroupsGeneratorInterface` (#3346)
1415

@@ -34,7 +35,6 @@
3435
* HTTP: Location header is only set on POST with a 201 or between 300 and 400 #3497
3536
* GraphQL: Do not allow empty cursor values on `before` or `after` #3360
3637
* Bump versions of Swagger UI, GraphiQL and GraphQL Playground #3510
37-
>>>>>>> 2.5
3838

3939
## 2.5.4
4040

features/bootstrap/GraphqlContext.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public function ISendTheGraphqlRequestWithVariables(PyStringNode $variables)
9090
}
9191

9292
/**
93-
* @When I send the GraphQL request with operation :operation
93+
* @When I send the GraphQL request with operationName :operationName
9494
*/
95-
public function ISendTheGraphqlRequestWithOperation(string $operation)
95+
public function ISendTheGraphqlRequestWithOperation(string $operationName)
9696
{
97-
$this->graphqlRequest['operation'] = $operation;
97+
$this->graphqlRequest['operationName'] = $operationName;
9898
$this->sendGraphqlRequest();
9999
}
100100

features/graphql/query.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ Feature: GraphQL query support
102102
}
103103
}
104104
"""
105-
And I send the GraphQL request with operation "DummyWithId2"
105+
And I send the GraphQL request with operationName "DummyWithId2"
106106
Then the response status code should be 200
107107
And the response should be in JSON
108108
And the header "Content-Type" should be equal to "application/json"
109109
And the JSON node "data.dummyItem.id" should be equal to "/dummies/2"
110110
And the JSON node "data.dummyItem.name" should be equal to "Dummy #2"
111-
And I send the GraphQL request with operation "DummyWithId1"
111+
And I send the GraphQL request with operationName "DummyWithId1"
112112
And the JSON node "data.dummyItem.name" should be equal to "Dummy #1"
113113

114114
Scenario: Use serialization groups

src/GraphQl/Action/EntrypointAction.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ public function __invoke(Request $request): Response
6969
}
7070
}
7171

72-
[$query, $operation, $variables] = $this->parseRequest($request);
72+
[$query, $operationName, $variables] = $this->parseRequest($request);
7373
if (null === $query) {
7474
throw new BadRequestHttpException('GraphQL query is not valid.');
7575
}
7676

7777
$executionResult = $this->executor
78-
->executeQuery($this->schemaBuilder->getSchema(), $query, null, null, $variables, $operation)
78+
->executeQuery($this->schemaBuilder->getSchema(), $query, null, null, $variables, $operationName)
7979
->setErrorFormatter([$this->normalizer, 'normalize']);
8080
} catch (\Exception $exception) {
8181
$executionResult = (new ExecutionResult(null, [new Error($exception->getMessage(), null, null, null, null, $exception)]))
@@ -91,34 +91,34 @@ public function __invoke(Request $request): Response
9191
private function parseRequest(Request $request): array
9292
{
9393
$query = $request->query->get('query');
94-
$operation = $request->query->get('operation');
94+
$operationName = $request->query->get('operationName');
9595
if ($variables = $request->query->get('variables', [])) {
9696
$variables = $this->decodeVariables($variables);
9797
}
9898

9999
if (!$request->isMethod('POST')) {
100-
return [$query, $operation, $variables];
100+
return [$query, $operationName, $variables];
101101
}
102102

103103
if ('json' === $request->getContentType()) {
104-
return $this->parseData($query, $operation, $variables, $request->getContent());
104+
return $this->parseData($query, $operationName, $variables, $request->getContent());
105105
}
106106

107107
if ('graphql' === $request->getContentType()) {
108108
$query = $request->getContent();
109109
}
110110

111111
if ('multipart' === $request->getContentType()) {
112-
return $this->parseMultipartRequest($query, $operation, $variables, $request->request->all(), $request->files->all());
112+
return $this->parseMultipartRequest($query, $operationName, $variables, $request->request->all(), $request->files->all());
113113
}
114114

115-
return [$query, $operation, $variables];
115+
return [$query, $operationName, $variables];
116116
}
117117

118118
/**
119119
* @throws BadRequestHttpException
120120
*/
121-
private function parseData(?string $query, ?string $operation, array $variables, string $jsonContent): array
121+
private function parseData(?string $query, ?string $operationName, array $variables, string $jsonContent): array
122122
{
123123
if (!\is_array($data = json_decode($jsonContent, true))) {
124124
throw new BadRequestHttpException('GraphQL data is not valid JSON.');
@@ -132,24 +132,23 @@ private function parseData(?string $query, ?string $operation, array $variables,
132132
$variables = \is_array($data['variables']) ? $data['variables'] : $this->decodeVariables($data['variables']);
133133
}
134134

135-
if (isset($data['operation'])) {
136-
$operation = $data['operation'];
135+
if (isset($data['operationName'])) {
136+
$operationName = $data['operationName'];
137137
}
138138

139-
return [$query, $operation, $variables];
139+
return [$query, $operationName, $variables];
140140
}
141141

142142
/**
143143
* @throws BadRequestHttpException
144144
*/
145-
private function parseMultipartRequest(?string $query, ?string $operation, array $variables, array $bodyParameters, array $files): array
145+
private function parseMultipartRequest(?string $query, ?string $operationName, array $variables, array $bodyParameters, array $files): array
146146
{
147147
if ((null === $operations = $bodyParameters['operations'] ?? null) || (null === $map = $bodyParameters['map'] ?? null)) {
148148
throw new BadRequestHttpException('GraphQL multipart request does not respect the specification.');
149149
}
150150

151-
/** @var string $operations */
152-
[$query, $operation, $variables] = $this->parseData($query, $operation, $variables, $operations);
151+
[$query, $operationName, $variables] = $this->parseData($query, $operationName, $variables, $operations);
153152

154153
/** @var string $map */
155154
if (!\is_array($decodedMap = json_decode($map, true))) {
@@ -158,7 +157,7 @@ private function parseMultipartRequest(?string $query, ?string $operation, array
158157

159158
$variables = $this->applyMapToVariables($decodedMap, $variables, $files);
160159

161-
return [$query, $operation, $variables];
160+
return [$query, $operationName, $variables];
162161
}
163162

164163
/**

tests/GraphQl/Action/EntrypointActionTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testGetHtmlAction(): void
5858

5959
public function testGetAction(): void
6060
{
61-
$request = new Request(['query' => 'graphqlQuery', 'variables' => '["graphqlVariable"]', 'operation' => 'graphqlOperationName']);
61+
$request = new Request(['query' => 'graphqlQuery', 'variables' => '["graphqlVariable"]', 'operationName' => 'graphqlOperationName']);
6262
$request->setRequestFormat('json');
6363
$mockedEntrypoint = $this->getEntrypointAction();
6464

@@ -67,7 +67,7 @@ public function testGetAction(): void
6767

6868
public function testPostRawAction(): void
6969
{
70-
$request = new Request(['variables' => '["graphqlVariable"]', 'operation' => 'graphqlOperationName'], [], [], [], [], [], 'graphqlQuery');
70+
$request = new Request(['variables' => '["graphqlVariable"]', 'operationName' => 'graphqlOperationName'], [], [], [], [], [], 'graphqlQuery');
7171
$request->setFormat('graphql', 'application/graphql');
7272
$request->setMethod('POST');
7373
$request->headers->set('Content-Type', 'application/graphql');
@@ -78,7 +78,7 @@ public function testPostRawAction(): void
7878

7979
public function testPostJsonAction(): void
8080
{
81-
$request = new Request([], [], [], [], [], [], '{"query": "graphqlQuery", "variables": "[\"graphqlVariable\"]", "operation": "graphqlOperationName"}');
81+
$request = new Request([], [], [], [], [], [], '{"query": "graphqlQuery", "variables": "[\"graphqlVariable\"]", "operationName": "graphqlOperationName"}');
8282
$request->setMethod('POST');
8383
$request->headers->set('Content-Type', 'application/json');
8484
$mockedEntrypoint = $this->getEntrypointAction();
@@ -119,14 +119,14 @@ public function multipartRequestProvider(): array
119119

120120
return [
121121
'upload a single file' => [
122-
'{"query": "graphqlQuery", "variables": {"file": null}, "operation": "graphqlOperationName"}',
122+
'{"query": "graphqlQuery", "variables": {"file": null}, "operationName": "graphqlOperationName"}',
123123
'{"file": ["variables.file"]}',
124124
['file' => $file],
125125
['file' => $file],
126126
new JsonResponse(['GraphQL']),
127127
],
128128
'upload multiple files' => [
129-
'{"query": "graphqlQuery", "variables": {"files": [null, null, null]}, "operation": "graphqlOperationName"}',
129+
'{"query": "graphqlQuery", "variables": {"files": [null, null, null]}, "operationName": "graphqlOperationName"}',
130130
'{"0": ["variables.files.0"], "1": ["variables.files.1"], "2": ["variables.files.2"]}',
131131
[
132132
'0' => $file,
@@ -150,7 +150,7 @@ public function multipartRequestProvider(): array
150150
new Response('{"errors":[{"message":"GraphQL multipart request does not respect the specification.","extensions":{"category":"user","status":400}}]}'),
151151
],
152152
'upload without providing map' => [
153-
'{"query": "graphqlQuery", "variables": {"file": null}, "operation": "graphqlOperationName"}',
153+
'{"query": "graphqlQuery", "variables": {"file": null}, "operationName": "graphqlOperationName"}',
154154
null,
155155
['file' => $file],
156156
['file' => null],
@@ -164,28 +164,28 @@ public function multipartRequestProvider(): array
164164
new Response('{"errors":[{"message":"GraphQL data is not valid JSON.","extensions":{"category":"user","status":400}}]}'),
165165
],
166166
'upload with invalid map JSON' => [
167-
'{"query": "graphqlQuery", "variables": {"file": null}, "operation": "graphqlOperationName"}',
167+
'{"query": "graphqlQuery", "variables": {"file": null}, "operationName": "graphqlOperationName"}',
168168
'{invalid}',
169169
['file' => $file],
170170
['file' => null],
171171
new Response('{"errors":[{"message":"GraphQL multipart request map is not valid JSON.","extensions":{"category":"user","status":400}}]}'),
172172
],
173173
'upload with no file' => [
174-
'{"query": "graphqlQuery", "variables": {"file": null}, "operation": "graphqlOperationName"}',
174+
'{"query": "graphqlQuery", "variables": {"file": null}, "operationName": "graphqlOperationName"}',
175175
'{"file": ["file"]}',
176176
[],
177177
['file' => null],
178178
new Response('{"errors":[{"message":"GraphQL multipart request file has not been sent correctly.","extensions":{"category":"user","status":400}}]}'),
179179
],
180180
'upload with wrong map' => [
181-
'{"query": "graphqlQuery", "variables": {"file": null}, "operation": "graphqlOperationName"}',
181+
'{"query": "graphqlQuery", "variables": {"file": null}, "operationName": "graphqlOperationName"}',
182182
'{"file": ["file"]}',
183183
['file' => $file],
184184
['file' => null],
185185
new Response('{"errors":[{"message":"GraphQL multipart request path in map is invalid.","extensions":{"category":"user","status":400}}]}'),
186186
],
187187
'upload when variable path does not exist' => [
188-
'{"query": "graphqlQuery", "variables": {"file": null}, "operation": "graphqlOperationName"}',
188+
'{"query": "graphqlQuery", "variables": {"file": null}, "operationName": "graphqlOperationName"}',
189189
'{"file": ["variables.wrong"]}',
190190
['file' => $file],
191191
['file' => null],
@@ -217,7 +217,7 @@ public function testBadMethodAction(): void
217217

218218
public function testBadVariablesAction(): void
219219
{
220-
$request = new Request(['query' => 'graphqlQuery', 'variables' => 'graphqlVariable', 'operation' => 'graphqlOperationName']);
220+
$request = new Request(['query' => 'graphqlQuery', 'variables' => 'graphqlVariable', 'operationName' => 'graphqlOperationName']);
221221
$request->setRequestFormat('json');
222222
$mockedEntrypoint = $this->getEntrypointAction();
223223

0 commit comments

Comments
 (0)