Skip to content

Commit 4e489cd

Browse files
authored
GraphQL: fix creating or updating an ID (#1795)
* GraphQL: fix creating or updating an ID * Add tests
1 parent 729042f commit 4e489cd

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

features/graphql/mutation.feature

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,44 @@ Feature: GraphQL mutation support
183183
And the JSON node "data.updateCompositeRelation.value" should be equal to "Modified value."
184184
And the JSON node "data.updateCompositeRelation.clientMutationId" should be equal to "myId"
185185

186+
Scenario: Create an item with a custom UUID
187+
When I send the following GraphQL request:
188+
"""
189+
mutation {
190+
createWritableId(input: {_id: "c6b722fe-0331-48c4-a214-f81f9f1ca082", name: "Foo", clientMutationId: "m"}) {
191+
id
192+
_id
193+
name
194+
clientMutationId
195+
}
196+
}
197+
"""
198+
And the response should be in JSON
199+
And the header "Content-Type" should be equal to "application/json"
200+
And the JSON node "data.createWritableId.id" should be equal to "/writable_ids/c6b722fe-0331-48c4-a214-f81f9f1ca082"
201+
And the JSON node "data.createWritableId._id" should be equal to "c6b722fe-0331-48c4-a214-f81f9f1ca082"
202+
And the JSON node "data.createWritableId.name" should be equal to "Foo"
203+
And the JSON node "data.createWritableId.clientMutationId" should be equal to "m"
204+
205+
Scenario: Update an item with a custom UUID
206+
When I send the following GraphQL request:
207+
"""
208+
mutation {
209+
updateWritableId(input: {id: "/writable_ids/c6b722fe-0331-48c4-a214-f81f9f1ca082", _id: "f8a708b2-310f-416c-9aef-b1b5719dfa47", name: "Foo", clientMutationId: "m"}) {
210+
id
211+
_id
212+
name
213+
clientMutationId
214+
}
215+
}
216+
"""
217+
And the response should be in JSON
218+
And the header "Content-Type" should be equal to "application/json"
219+
And the JSON node "data.updateWritableId.id" should be equal to "/writable_ids/f8a708b2-310f-416c-9aef-b1b5719dfa47"
220+
And the JSON node "data.updateWritableId._id" should be equal to "f8a708b2-310f-416c-9aef-b1b5719dfa47"
221+
And the JSON node "data.updateWritableId.name" should be equal to "Foo"
222+
And the JSON node "data.updateWritableId.clientMutationId" should be equal to "m"
223+
186224
@dropSchema
187225
Scenario: Trigger a validation error
188226
When I send the following GraphQL request:

src/GraphQl/Resolver/Factory/ItemMutationResolverFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
8686
case 'create':
8787
case 'update':
8888
unset($args['input']['id']);
89+
if (isset($args['input']['_id'])) {
90+
$args['input']['id'] = $args['input']['_id'];
91+
}
92+
8993
$context = null === $item ? ['resource_class' => $resourceClass] : ['resource_class' => $resourceClass, 'object_to_populate' => $item];
9094
$item = $this->normalizer->denormalize($args['input'], $resourceClass, ItemNormalizer::FORMAT, $context);
9195
$this->validate($item, $info, $resourceMetadata, $operationName);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\ORM\Mapping as ORM;
18+
use Symfony\Component\Validator\Constraints as Assert;
19+
20+
/**
21+
* @author Kévin Dunglas <[email protected]>
22+
*
23+
* @ApiResource
24+
* @ORM\Entity
25+
*/
26+
class WritableId
27+
{
28+
/**
29+
* @ORM\Id
30+
* @Assert\Uuid
31+
* @ORM\Column(type="guid")
32+
*/
33+
public $id;
34+
35+
/**
36+
* @ORM\Column
37+
*/
38+
public $name;
39+
}

0 commit comments

Comments
 (0)