2323use GraphQL \Language \Visitor ;
2424use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
2525use Magento \GraphQl \Model \Query \ContextInterface ;
26+ use Magento \Framework \App \RequestInterface ;
27+ use Magento \Framework \Serialize \SerializerInterface ;
2628
2729class ProductAttributeSortInput
2830{
31+ /**
32+ * @var RequestInterface
33+ */
34+ private $ request ;
35+
36+ /**
37+ * @var SerializerInterface
38+ */
39+ private $ jsonSerializer ;
40+
41+ /**
42+ * @param RequestInterface $request
43+ * @param SerializerInterface $jsonSerializer
44+ */
45+ public function __construct (RequestInterface $ request , SerializerInterface $ jsonSerializer )
46+ {
47+ $ this ->request = $ request ;
48+ $ this ->jsonSerializer = $ jsonSerializer ;
49+ }
2950 /**
3051 * Plugin to preserve the original order of sort fields
3152 *
@@ -47,43 +68,83 @@ public function beforeResolve(
4768 array $ value = null ,
4869 array $ args = null
4970 ): array {
71+
72+ $ data = $ this ->getDataFromRequest ($ this ->request );
5073 if (isset ($ args ['sort ' ])) {
51- $ args ['sort ' ] = $ this ->getSortFieldsOrder ($ info , $ args ['sort ' ]);
74+ $ args ['sort ' ] = $ this ->getSortFieldsOrder (
75+ $ info ,
76+ $ args ['sort ' ],
77+ isset ($ data ['variables ' ]['sort ' ]) ? array_keys ($ data ['variables ' ]['sort ' ]) : null
78+ );
5279 }
5380 return [$ field , $ context , $ info , $ value , $ args ];
5481 }
5582
83+ /**
84+ * Get data from request body or query string
85+ *
86+ * @param RequestInterface $request
87+ * @return array
88+ */
89+ private function getDataFromRequest (RequestInterface $ request ): array
90+ {
91+ $ data = [];
92+ if ($ request ->isPost ()) {
93+ $ data = $ this ->jsonSerializer ->unserialize ($ request ->getContent ());
94+ } elseif ($ request ->isGet ()) {
95+ $ data = $ request ->getParams ();
96+ $ data ['variables ' ] = isset ($ data ['variables ' ]) ?
97+ $ this ->jsonSerializer ->unserialize ($ data ['variables ' ]) : null ;
98+ $ data ['variables ' ] = is_array ($ data ['variables ' ]) ?
99+ $ data ['variables ' ] : null ;
100+ }
101+ return $ data ;
102+ }
103+
56104 /**
57105 * Get sort fields in the original order
58106 *
59107 * @param ResolveInfo $info
60108 * @param array $sortFields
109+ * @param array|null $requestSortFields
61110 * @return array
62111 * @throws \Exception
63112 */
64- private function getSortFieldsOrder (ResolveInfo $ info , array $ sortFields)
113+ private function getSortFieldsOrder (ResolveInfo $ info , array $ sortFields, ? array $ requestSortFields ): array
65114 {
66115 $ sortFieldsOriginal = [];
67116 Visitor::visit (
68117 $ info ->operation ,
69118 [
70119 'enter ' => [
71- NodeKind::ARGUMENT => function (Node $ node ) use (&$ sortFieldsOriginal , $ sortFields ) {
120+ NodeKind::ARGUMENT => function (Node $ node ) use (
121+ &$ sortFieldsOriginal ,
122+ $ sortFields ,
123+ $ requestSortFields
124+ ) {
72125 if ($ node ->name ->value === 'sort ' ) {
73- Visitor::visit (
74- $ node ->value ,
75- [
76- 'enter ' => [
77- NodeKind::OBJECT_FIELD =>
78- function (Node $ node ) use (&$ sortFieldsOriginal , $ sortFields ) {
79- if (isset ($ sortFields [$ node ->name ->value ])) {
80- $ sortFieldsOriginal [$ node ->name ->value ] =
81- $ sortFields [$ node ->name ->value ];
126+ if (isset ($ requestSortFields )) {
127+ foreach ($ requestSortFields as $ fieldName ) {
128+ if (isset ($ sortFields [$ fieldName ])) {
129+ $ sortFieldsOriginal [$ fieldName ] = $ sortFields [$ fieldName ];
130+ }
131+ }
132+ } else {
133+ Visitor::visit (
134+ $ node ->value ,
135+ [
136+ 'enter ' => [
137+ NodeKind::OBJECT_FIELD =>
138+ function (Node $ node ) use (&$ sortFieldsOriginal , $ sortFields ) {
139+ if (isset ($ sortFields [$ node ->name ->value ])) {
140+ $ sortFieldsOriginal [$ node ->name ->value ] =
141+ $ sortFields [$ node ->name ->value ];
142+ }
82143 }
83- }
144+ ]
84145 ]
85- ]
86- );
146+ );
147+ }
87148 return Visitor::stop ();
88149 }
89150 }
0 commit comments