Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1152 - fire the operation events before after retrieving a query result even if it is cached #1153

Open
wants to merge 5 commits into
base: 8.x-4.x
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions src/GraphQL/Execution/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -299,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;
Expand Down