2323
2424use ApiPlatform \Core \Metadata \Property \PropertyMetadata ;
2525use ApiPlatform \Core \Serializer \AbstractItemNormalizer ;
26+ use ApiPlatform \Core \Serializer \ItemNormalizer as BaseItemNormalizer ;
2627
2728/**
2829 * GraphQL normalizer.
2930 *
3031 * @author Kévin Dunglas <dunglas@gmail.com>
3132 */
32- final class ItemNormalizer extends AbstractItemNormalizer
33+ final class ItemNormalizer extends BaseItemNormalizer
3334{
3435 const FORMAT = 'graphql ' ;
3536 const ITEM_KEY = '#item ' ;
3637
38+ /**
39+ * {@inheritdoc}
40+ */
41+ public function supportsNormalization ($ data , $ format = null )
42+ {
43+ return self ::FORMAT === $ format && parent ::supportsNormalization ($ data , $ format );
44+ }
45+
3746 /**
3847 * {@inheritdoc}
3948 */
4049 public function normalize ($ object , $ format = null , array $ context = [])
4150 {
42- $ data = parent ::normalize ($ object , $ format , $ context );
51+ $ data = AbstractItemNormalizer ::normalize ($ object , $ format , $ context );
4352 $ data [self ::ITEM_KEY ] = serialize ($ object ); // calling serialize prevent weird normalization process done by Webonyx's GraphQL PHP
4453
4554 return $ data ;
@@ -57,16 +66,35 @@ protected function normalizeCollectionOfRelations(PropertyMetadata $propertyMeta
5766 /**
5867 * {@inheritdoc}
5968 */
60- public function supportsNormalization ($ data , $ format = null )
69+ public function supportsDenormalization ($ data, $ type , $ format = null )
6170 {
62- return self ::FORMAT === $ format && parent ::supportsNormalization ($ data , $ format );
71+ return self ::FORMAT === $ format && parent ::supportsDenormalization ($ data, $ type , $ format );
6372 }
6473
6574 /**
6675 * {@inheritdoc}
6776 */
68- public function supportsDenormalization ( $ data , $ type , $ format = null )
77+ protected function getAllowedAttributes ( $ classOrObject , array $ context , $ attributesAsString = false )
6978 {
70- 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 );
7199 }
72100}
0 commit comments