@@ -48,39 +48,18 @@ public function __construct(IdentifiersExtractorInterface $identifiersExtractor,
48
48
public function createItemMutationResolver (string $ resourceClass , string $ mutationName ): callable
49
49
{
50
50
return function ($ root , $ args , $ context , ResolveInfo $ info ) use ($ resourceClass , $ mutationName ) {
51
- $ identifiers = $ this ->identifiersExtractor ->getIdentifiersFromResourceClass ($ resourceClass );
52
- if (\count ($ identifiers ) > 1 ) {
53
- $ identifierPairs = \array_map (function ($ identifier ) use ($ args , $ info ) {
54
- if (\is_array ($ args ['input ' ][$ identifier ])) {
55
- if (\count ($ args ['input ' ][$ identifier ]) > 1 ) {
56
- throw Error::createLocatedError ('Composite identifiers are not allowed for a resource already used as a composite identifier ' , $ info ->fieldNodes , $ info ->path );
57
- }
58
-
59
- return $ identifier .'= ' .\reset ($ args ['input ' ][$ identifier ]);
60
- }
61
-
62
- return "{$ identifier }= {$ args ['input ' ][$ identifier ]}" ;
63
- }, $ identifiers );
64
- $ id = \implode ('; ' , $ identifierPairs );
65
- } else {
66
- $ id = $ args ['input ' ][$ identifiers [0 ]];
67
- }
51
+ $ id = $ this ->getIdentifier ($ this ->identifiersExtractor ->getIdentifiersFromResourceClass ($ resourceClass ), $ args , $ info );
52
+ $ item = null ;
68
53
69
54
if ('update ' === $ mutationName || 'delete ' === $ mutationName ) {
70
- $ item = $ this ->itemDataProvider ->getItem ($ resourceClass , $ id );
71
- if (null === $ item ) {
72
- throw Error::createLocatedError ("Item $ resourceClass $ id not found " , $ info ->fieldNodes , $ info ->path );
73
- }
55
+ $ item = $ this ->getItem ($ resourceClass , $ id , $ info );
74
56
}
75
57
76
58
switch ($ mutationName ) {
77
- case 'delete ' :
78
- $ this ->dataPersister ->remove ($ item );
79
-
80
- return $ args ['input ' ];
81
-
59
+ case 'create ' :
82
60
case 'update ' :
83
- $ item = $ this ->serializer ->denormalize ($ args ['input ' ], $ resourceClass , null , ['resource_class ' => $ resourceClass , 'object_to_populate ' => $ item ]);
61
+ $ context = null === $ item ? ['resource_class ' => $ resourceClass ] : ['resource_class ' => $ resourceClass , 'object_to_populate ' => $ item ];
62
+ $ item = $ this ->serializer ->denormalize ($ args ['input ' ], $ resourceClass , null , $ context );
84
63
$ this ->dataPersister ->persist ($ item );
85
64
86
65
return $ this ->serializer ->normalize (
@@ -90,7 +69,46 @@ public function createItemMutationResolver(string $resourceClass, string $mutati
90
69
->create ($ resourceClass )
91
70
->getGraphqlAttribute ($ mutationName , 'normalization_context ' , [], true )
92
71
);
72
+
73
+ case 'delete ' :
74
+ $ this ->dataPersister ->remove ($ item );
75
+
76
+ return $ args ['input ' ];
93
77
}
94
78
};
95
79
}
80
+
81
+ private function getIdentifier (array $ identifiers , $ args , $ info )
82
+ {
83
+ if (\count ($ identifiers ) === 1 ) {
84
+ return $ args ['input ' ][$ identifiers [0 ]];
85
+ }
86
+
87
+ $ identifierPairs = [];
88
+ foreach ($ identifiers as $ key => $ identifier ) {
89
+ if (!\is_array ($ args ['input ' ][$ identifier ])) {
90
+ $ identifierPairs [$ key ] = "{$ identifier }= {$ args ['input ' ][$ identifier ]}" ;
91
+
92
+ continue ;
93
+ }
94
+
95
+ if (\count ($ args ['input ' ][$ identifier ]) > 1 ) {
96
+ throw Error::createLocatedError ('Composite identifiers are not allowed for a resource already used as a composite identifier ' , $ info ->fieldNodes , $ info ->path );
97
+ }
98
+
99
+ $ identifierPairs [$ key ] = "$ identifier= " .reset ($ args ['input ' ][$ identifier ]);
100
+ }
101
+
102
+ return implode ('; ' , $ identifierPairs );
103
+ }
104
+
105
+ private function getItem (string $ resourceClass , $ id , $ info )
106
+ {
107
+ $ item = $ this ->itemDataProvider ->getItem ($ resourceClass , $ id );
108
+ if (null === $ item ) {
109
+ throw Error::createLocatedError ("Item $ resourceClass $ id not found " , $ info ->fieldNodes , $ info ->path );
110
+ }
111
+
112
+ return $ item ;
113
+ }
96
114
}
0 commit comments