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;
}

Comment on lines +121 to +127

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a setter to configure the server ID, I'm wondering if it wouldn't be better to make the plugin configurable by implementing ConfigurableInterface. In fact, both schema plugins we ship are already configurable (but the base class SdlSchemaPluginBase isn't, and changing this would be a B/C break).

/**
* {@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()}";
Comment on lines -182 to +196

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that the setter has always been called, but it might not have been, and $this->serverId might be NULL.

Probably using a setter is not the best approach. Since the server ID is required for the plugin to work in the current it is essential that the setter is always called. The setter is not part of the SchemaPluginInterface and it also should not be, since it is not essential for all schema plugins, only for the ones that use caching.

Maybe we can add a server_name key to the standard plugin configuration and check that this is populated in the plugin constructor? That would also enable us to gracefully introduce this change. We can emit a deprecation warning if it is missing.

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