22
33namespace Drupal \graphql \GraphQL \Buffers ;
44
5+ use Drupal \Core \Cache \CacheableDependencyInterface ;
56use Drupal \Core \Url ;
7+ use Drupal \graphql \GraphQL \Cache \CacheableValue ;
68use Symfony \Component \HttpFoundation \Request ;
79use Symfony \Component \HttpFoundation \RequestStack ;
810use Symfony \Component \HttpKernel \HttpKernelInterface ;
@@ -60,20 +62,27 @@ public function add(Url $url, callable $extract) {
6062 * {@inheritdoc}
6163 */
6264 protected function getBufferId ($ item ) {
63- /** @var \Drupal\Core\Url $url */
64- $ url = $ item ['url ' ];
65- return $ url ->toString ();
65+ /** @var \Drupal\Core\GeneratedUrl $url */
66+ $ url = $ item ['url ' ]->toString (TRUE );
67+
68+ return hash ('sha256 ' , json_encode ([
69+ 'url ' => $ url ->getGeneratedUrl (),
70+ 'tags ' => $ url ->getCacheTags (),
71+ 'contexts ' => $ url ->getCacheContexts (),
72+ 'age ' => $ url ->getCacheMaxAge (),
73+ ]));
6674 }
6775
6876 /**
6977 * {@inheritdoc}
7078 */
7179 public function resolveBufferArray (array $ buffer ) {
72- /** @var \Drupal\Core\Url $url */
73- $ url = reset ($ buffer )['url ' ];
80+ /** @var \Drupal\Core\GeneratedUrl $url */
81+ $ url = reset ($ buffer )['url ' ]->toString (TRUE );
82+
7483 $ currentRequest = $ this ->requestStack ->getCurrentRequest ();
7584 $ request = Request::create (
76- $ url ->getOption ( ' routed_path ' ) ?: $ url -> toString (),
85+ $ url ->getGeneratedUrl (),
7786 'GET ' ,
7887 $ currentRequest ->query ->all (),
7988 $ currentRequest ->cookies ->all (),
@@ -93,6 +102,9 @@ public function resolveBufferArray(array $buffer) {
93102
94103 /** @var \Drupal\graphql\GraphQL\Buffers\SubRequestResponse $response */
95104 $ response = $ this ->httpKernel ->handle ($ request , HttpKernelInterface::SUB_REQUEST );
105+ if ($ url instanceof CacheableDependencyInterface) {
106+ $ response ->addCacheableDependency ($ url );
107+ }
96108
97109 // TODO:
98110 // Remove the request stack manipulation once the core issue described at
@@ -101,7 +113,9 @@ public function resolveBufferArray(array $buffer) {
101113 $ this ->requestStack ->pop ();
102114 }
103115
104- return $ response ->getResult ();
116+ return array_map (function ($ value ) use ($ response ) {
117+ return new CacheableValue ($ value , [$ response ]);
118+ }, $ response ->getResult ());
105119 }
106120
107121}
0 commit comments