44
55use Drupal \Core \Cache \CacheableDependencyInterface ;
66use Drupal \Core \Url ;
7+ use Drupal \Core \Routing \LocalRedirectResponse ;
8+ use Drupal \graphql \GraphQL \Buffers \SubRequestResponse ;
79use Drupal \graphql \GraphQL \Cache \CacheableValue ;
810use Symfony \Component \HttpFoundation \Request ;
911use Symfony \Component \HttpFoundation \RequestStack ;
@@ -74,49 +76,79 @@ protected function getBufferId($item) {
7476 }
7577
7678 /**
77- * {@inheritdoc}
79+ * Create a sub-request for the given url.
80+ *
81+ * @param \Symfony\Component\HttpFoundation\Request $current
82+ * The current main request.
83+ * @param string $url
84+ * The url to run the subrequest on.
85+ * @param array $buffer
86+ * The buffer.
87+ *
88+ * @return \Symfony\Component\HttpFoundation\Request
89+ * The request object.
7890 */
79- public function resolveBufferArray (array $ buffer ) {
80- /** @var \Drupal\Core\GeneratedUrl $url */
81- $ url = reset ($ buffer )['url ' ]->toString (TRUE );
82-
83- $ currentRequest = $ this ->requestStack ->getCurrentRequest ();
91+ protected function createRequest (Request $ current , array $ buffer , $ url ) {
8492 $ request = Request::create (
85- $ url-> getGeneratedUrl () ,
93+ $ url ,
8694 'GET ' ,
87- $ currentRequest ->query ->all (),
88- $ currentRequest ->cookies ->all (),
89- $ currentRequest ->files ->all (),
90- $ currentRequest ->server ->all ()
95+ $ current ->query ->all (),
96+ $ current ->cookies ->all (),
97+ $ current ->files ->all (),
98+ $ current ->server ->all ()
9199 );
92- $ request ->setRequestFormat ('_graphql_subrequest ' );
93100
94101 $ request ->attributes ->set ('_graphql_subrequest ' , function () use ($ buffer ) {
95102 return array_map (function ($ item ) {
96103 return $ item ['extract ' ]($ item ['url ' ]);
97104 }, $ buffer );
98105 });
99106
100- if ($ session = $ currentRequest ->getSession ()) {
107+ $ request ->setRequestFormat ('_graphql_subrequest ' );
108+ if ($ session = $ current ->getSession ()) {
101109 $ request ->setSession ($ session );
102110 }
111+
112+ return $ request ;
113+ }
103114
115+ /**
116+ * {@inheritdoc}
117+ */
118+ public function resolveBufferArray (array $ buffer ) {
119+ /** @var \Drupal\Core\GeneratedUrl $url */
120+ $ url = reset ($ buffer )['url ' ]->toString (TRUE );
121+
122+ $ current = $ this ->requestStack ->getCurrentRequest ();
123+ $ target = $ url ->getGeneratedUrl ();
124+ $ request = $ this ->createRequest ($ current , $ buffer , $ target );
125+
104126 /** @var \Drupal\graphql\GraphQL\Buffers\SubRequestResponse $response */
105127 $ response = $ this ->httpKernel ->handle ($ request , HttpKernelInterface::SUB_REQUEST );
106- if ($ url instanceof CacheableDependencyInterface) {
107- $ response ->addCacheableDependency ($ url );
128+ while ($ response instanceof LocalRedirectResponse) {
129+ $ target = $ response ->getTargetUrl ();
130+ $ request = $ this ->createRequest ($ current , $ buffer , $ target );
131+ $ response = $ this ->httpKernel ->handle ($ request , HttpKernelInterface::SUB_REQUEST );
132+ }
133+
134+ if (!($ response instanceof SubRequestResponse)) {
135+ return array_fill_keys (array_keys ($ buffer ), NULL );
108136 }
109137
110138 // TODO:
111139 // Remove the request stack manipulation once the core issue described at
112140 // https://www.drupal.org/node/2613044 is resolved.
113- while ($ this ->requestStack ->getCurrentRequest () === $ request ) {
141+ while ($ this ->requestStack ->getCurrentRequest () !== $ current ) {
114142 $ this ->requestStack ->pop ();
115143 }
116144
145+ if ($ url instanceof CacheableDependencyInterface) {
146+ $ response ->addCacheableDependency ($ url );
147+ }
148+
117149 return array_map (function ($ value ) use ($ response ) {
118150 return new CacheableValue ($ value , [$ response ]);
119151 }, $ response ->getResult ());
120152 }
121153
122- }
154+ }
0 commit comments