From 3841ce83ccbc67fc2bd4a68566e0a26de98895ec Mon Sep 17 00:00:00 2001 From: Hristo Chonov Date: Sat, 30 Jan 2021 14:43:41 +0200 Subject: [PATCH 1/3] #1152 - fire the operation events before after retrieving a query result even if it is cached --- src/GraphQL/Execution/Executor.php | 34 ++++++++++++++++-------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/GraphQL/Execution/Executor.php b/src/GraphQL/Execution/Executor.php index a119beff5..35290653c 100644 --- a/src/GraphQL/Execution/Executor.php +++ b/src/GraphQL/Execution/Executor.php @@ -212,19 +212,29 @@ public static function create( * {@inheritdoc} */ public function doExecute(): Promise { + $event = new OperationEvent($this->context); + $this->dispatcher->dispatch(OperationEvent::GRAPHQL_OPERATION_BEFORE, $event); + $server = $this->context->getServer(); $type = AST::getOperation($this->document, $this->operation); if ($type === 'query' && !!$server->get('caching')) { - return $this->doExecuteCached($this->cachePrefix()); + $result = $this->doExecuteCached($this->cachePrefix()); } + else { + // This operation can never be cached because we are either in development + // mode (caching is disabled) or this is a non-cacheable operation. + $result = $this->doExecuteUncached()->then(function ($result) { + $this->context->mergeCacheMaxAge(0); - // This operation can never be cached because we are either in development - // mode (caching is disabled) or this is a non-cacheable operation. - return $this->doExecuteUncached()->then(function ($result) { - $this->context->mergeCacheMaxAge(0); + $result = new CacheableExecutionResult($result->data, $result->extensions, $result->errors); + $result->addCacheableDependency($this->context); + return $result; + }); + } - $result = new CacheableExecutionResult($result->data, $result->extensions, $result->errors); - $result->addCacheableDependency($this->context); + return $result->then(function ($result) { + $event = new OperationEvent($this->context, $result); + $this->dispatcher->dispatch(OperationEvent::GRAPHQL_OPERATION_AFTER, $event); return $result; }); } @@ -273,15 +283,7 @@ protected function doExecuteUncached() { $this->resolver ); - $event = new OperationEvent($this->context); - $this->dispatcher->dispatch(OperationEvent::GRAPHQL_OPERATION_BEFORE, $event); - - return $executor->doExecute()->then(function ($result) { - $event = new OperationEvent($this->context, $result); - $this->dispatcher->dispatch(OperationEvent::GRAPHQL_OPERATION_AFTER, $event); - - return $result; - }); + return $executor->doExecute(); } /** From a436bf9ff09a0990fbfe02703e2582e5ca6fcd67 Mon Sep 17 00:00:00 2001 From: Hristo Chonov Date: Sun, 7 Feb 2021 01:27:31 +0200 Subject: [PATCH 2/3] #1152 - consider cache contexts for the cache prefix --- src/GraphQL/Execution/Executor.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/GraphQL/Execution/Executor.php b/src/GraphQL/Execution/Executor.php index 35290653c..f8348cb88 100644 --- a/src/GraphQL/Execution/Executor.php +++ b/src/GraphQL/Execution/Executor.php @@ -301,10 +301,17 @@ protected function cachePrefix() { $extensions = $this->context->getOperation()->extensions ?: []; ksort($extensions); + // By firing the operation event before executing the operation and before + // reading from the cache different contexts might be set to control the + // caching and make it possible to cache e.g. by country. + $contexts = $this->context->getCacheContexts(); + $keys = $this->contextsManager->convertTokensToKeys($contexts)->getKeys(); + $hash = hash('sha256', serialize([ 'query' => DocumentSerializer::serializeDocument($this->document), 'variables' => $variables, 'extensions' => $extensions, + 'keys' => $keys, ])); return $hash; From 7117c0bd761ce7d6dbde837aed0ce19a40d5b0e8 Mon Sep 17 00:00:00 2001 From: Hristo Chonov Date: Tue, 21 Sep 2021 00:14:54 +0300 Subject: [PATCH 3/3] update diff