Skip to content

Commit 0c2c517

Browse files
dunglasabluchet
authored andcommitted
GraphQL: fix creating or updating an ID (#1795)
* GraphQL: fix creating or updating an ID * Add tests
1 parent 51f4d82 commit 0c2c517

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

features/graphql/mutation.feature

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,45 @@ Feature: GraphQL mutation support
221221
And the JSON node "data.updateCompositeRelation.value" should be equal to "Modified value."
222222
And the JSON node "data.updateCompositeRelation.clientMutationId" should be equal to "myId"
223223

224+
Scenario: Create an item with a custom UUID
225+
When I send the following GraphQL request:
226+
"""
227+
mutation {
228+
createWritableId(input: {_id: "c6b722fe-0331-48c4-a214-f81f9f1ca082", name: "Foo", clientMutationId: "m"}) {
229+
id
230+
_id
231+
name
232+
clientMutationId
233+
}
234+
}
235+
"""
236+
And the response should be in JSON
237+
And the header "Content-Type" should be equal to "application/json"
238+
And the JSON node "data.createWritableId.id" should be equal to "/writable_ids/c6b722fe-0331-48c4-a214-f81f9f1ca082"
239+
And the JSON node "data.createWritableId._id" should be equal to "c6b722fe-0331-48c4-a214-f81f9f1ca082"
240+
And the JSON node "data.createWritableId.name" should be equal to "Foo"
241+
And the JSON node "data.createWritableId.clientMutationId" should be equal to "m"
242+
243+
Scenario: Update an item with a custom UUID
244+
When I send the following GraphQL request:
245+
"""
246+
mutation {
247+
updateWritableId(input: {id: "/writable_ids/c6b722fe-0331-48c4-a214-f81f9f1ca082", _id: "f8a708b2-310f-416c-9aef-b1b5719dfa47", name: "Foo", clientMutationId: "m"}) {
248+
id
249+
_id
250+
name
251+
clientMutationId
252+
}
253+
}
254+
"""
255+
And the response should be in JSON
256+
And the header "Content-Type" should be equal to "application/json"
257+
And the JSON node "data.updateWritableId.id" should be equal to "/writable_ids/f8a708b2-310f-416c-9aef-b1b5719dfa47"
258+
And the JSON node "data.updateWritableId._id" should be equal to "f8a708b2-310f-416c-9aef-b1b5719dfa47"
259+
And the JSON node "data.updateWritableId.name" should be equal to "Foo"
260+
And the JSON node "data.updateWritableId.clientMutationId" should be equal to "m"
261+
262+
@dropSchema
224263
Scenario: Trigger a validation error
225264
When I send the following GraphQL request:
226265
"""

src/GraphQl/Resolver/Factory/ItemMutationResolverFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
9090
case 'create':
9191
case 'update':
9292
unset($args['input']['id']);
93+
if (isset($args['input']['_id'])) {
94+
$args['input']['id'] = $args['input']['_id'];
95+
}
96+
9397
$context = null === $item ? ['resource_class' => $resourceClass] : ['resource_class' => $resourceClass, 'object_to_populate' => $item];
9498
$item = $this->normalizer->denormalize($args['input'], $resourceClass, ItemNormalizer::FORMAT, $context);
9599
$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)