Skip to content

Commit c887ac7

Browse files
alanpoulainabluchet
authored andcommitted
[GraphQL] Fix identifiers (#1805)
* Remove final from ItemNormalizer (add a PHPDoc final instead) * Modify GraphQL item normalizer to manage identifiers correctly * Remove uneeded code in mutation resolver
1 parent 1e45c6c commit c887ac7

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/GraphQl/Resolver/Factory/ItemMutationResolverFactory.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
8989
switch ($operationName) {
9090
case 'create':
9191
case 'update':
92-
unset($args['input']['id']);
93-
if (isset($args['input']['_id'])) {
94-
$args['input']['id'] = $args['input']['_id'];
95-
}
96-
9792
$context = null === $item ? ['resource_class' => $resourceClass] : ['resource_class' => $resourceClass, 'object_to_populate' => $item];
9893
$item = $this->normalizer->denormalize($args['input'], $resourceClass, ItemNormalizer::FORMAT, $context);
9994
$this->validate($item, $info, $resourceMetadata, $operationName);

src/GraphQl/Serializer/ItemNormalizer.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,26 @@
2323

2424
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
2525
use ApiPlatform\Core\Serializer\AbstractItemNormalizer;
26-
use ApiPlatform\Core\Serializer\ItemNormalizer as GenericItemNormalizer;
26+
use ApiPlatform\Core\Serializer\ItemNormalizer as BaseItemNormalizer;
2727

2828
/**
2929
* GraphQL normalizer.
3030
*
3131
* @author Kévin Dunglas <[email protected]>
3232
*/
33-
final class ItemNormalizer extends GenericItemNormalizer
33+
final class ItemNormalizer extends BaseItemNormalizer
3434
{
3535
const FORMAT = 'graphql';
3636
const ITEM_KEY = '#item';
3737

38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function supportsNormalization($data, $format = null)
42+
{
43+
return self::FORMAT === $format && parent::supportsNormalization($data, $format);
44+
}
45+
3846
/**
3947
* {@inheritdoc}
4048
*/
@@ -58,16 +66,35 @@ protected function normalizeCollectionOfRelations(PropertyMetadata $propertyMeta
5866
/**
5967
* {@inheritdoc}
6068
*/
61-
public function supportsNormalization($data, $format = null)
69+
public function supportsDenormalization($data, $type, $format = null)
6270
{
63-
return self::FORMAT === $format && parent::supportsNormalization($data, $format);
71+
return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format);
6472
}
6573

6674
/**
6775
* {@inheritdoc}
6876
*/
69-
public function supportsDenormalization($data, $type, $format = null)
77+
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
7078
{
71-
return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format);
79+
$allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
80+
81+
if (($context['api_denormalize'] ?? false) && false !== ($indexId = array_search('id', $allowedAttributes, true))) {
82+
$allowedAttributes[] = '_id';
83+
array_splice($allowedAttributes, $indexId, 1);
84+
}
85+
86+
return $allowedAttributes;
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = [])
93+
{
94+
if ('_id' === $attribute) {
95+
$attribute = 'id';
96+
}
97+
98+
parent::setAttributeValue($object, $attribute, $value, $format, $context);
7299
}
73100
}

0 commit comments

Comments
 (0)