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

Allow schema caching per server when using a common base schema #1224

Open
wants to merge 11 commits into
base: 8.x-4.x
Choose a base branch
from
1 change: 1 addition & 0 deletions src/Entity/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public function configuration() {

/** @var \Drupal\graphql\Plugin\SchemaPluginInterface $plugin */
$plugin = $manager->createInstance($schema);
$plugin->setServerId($this->name);
if ($plugin instanceof ConfigurableInterface && $config = $this->get('schema_configuration')) {
$plugin->setConfiguration($config[$schema] ?? []);
}
Expand Down
1 change: 1 addition & 0 deletions src/GraphQL/Execution/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ protected function cachePrefix() {
ksort($extensions);

$hash = hash('sha256', serialize([
'server' => $this->context->getServer()->name,
'query' => DocumentSerializer::serializeDocument($this->document),
'variables' => $variables,
'extensions' => $extensions,
Expand Down
18 changes: 16 additions & 2 deletions src/Plugin/GraphQL/Schema/SdlSchemaPluginBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ abstract class SdlSchemaPluginBase extends PluginBase implements SchemaPluginInt
*/
protected $inDevelopment;

/**
* The ID of the server using this plugin.
*
* @var string
*/
protected $serverId;

/**
* The schema extension plugin manager.
*
Expand Down Expand Up @@ -111,6 +118,13 @@ public function __construct(
$this->moduleHandler = $moduleHandler;
}

/**
* {@inheritdoc}
*/
public function setServerId(string $serverId): void {
$this->serverId = $serverId;
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -179,7 +193,7 @@ protected function getExtensions() {
*/
protected function getSchemaDocument(array $extensions = []) {
// Only use caching of the parsed document if we aren't in development mode.
$cid = "schema:{$this->getPluginId()}";
$cid = "server:{$this->serverId}:schema:{$this->getPluginId()}";
if (empty($this->inDevelopment) && $cache = $this->astCache->get($cid)) {
return $cache->data;
}
Expand Down Expand Up @@ -209,7 +223,7 @@ protected function getSchemaDocument(array $extensions = []) {
*/
private function getFullSchemaDocument(Schema $schema, array $extensions): ?DocumentNode {
// Only use caching of the parsed document if we aren't in development mode.
$cid = "full:{$this->getPluginId()}";
$cid = "server:{$this->serverId}:full:{$this->getPluginId()}";
if (empty($this->inDevelopment) && $cache = $this->astCache->get($cid)) {
return $cache->data;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Plugin/SchemaPluginInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
*/
interface SchemaPluginInterface extends PluginInspectionInterface, DerivativeInspectionInterface {

/**
* Set the serverID, required for cache id generation.
*
* @param string $serverId
* The machine name of the server using this plugin.
*/
public function setServerId(string $serverId): void;

/**
* Retrieves the schema.
*
Expand Down