From b28c3e4d5423763c2716c2645080d5b71ce5ad38 Mon Sep 17 00:00:00 2001 From: Radoslav Terezka Date: Tue, 28 May 2019 13:07:09 +0200 Subject: [PATCH 1/4] Introduce extended schema plugin. --- .../Schema/SdlExtendedSchemaPluginBase.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php diff --git a/src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php b/src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php new file mode 100644 index 000000000..b5fdc21d5 --- /dev/null +++ b/src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php @@ -0,0 +1,46 @@ +inDevelopment) && $cache = $this->astCache->get($this->getPluginId())) { + return $cache->data; + } + + $ast = Parser::parse($this->getExtendedSchemaDefinition()); + if (!empty($this->inDevelopment)) { + $this->astCache->set($this->getPluginId(), $ast, CacheBackendInterface::CACHE_PERMANENT, ['graphql']); + } + + return $ast; + } + + /** + * {@inheritdoc} + */ + public function getSchema() { + return SchemaExtender::extend(parent::getSchema(), $this->getExtendedSchemaDocument()); + } + + /** + * Retrieves the raw extended schema definition string. + * + * @return string + * The extended schema definition. + */ + abstract protected function getExtendedSchemaDefinition(); + +} From 68f2ccfa89a3953d209fb8f54ef3c045eeede19e Mon Sep 17 00:00:00 2001 From: Radoslav Terezka Date: Tue, 28 May 2019 17:02:45 +0200 Subject: [PATCH 2/4] Fix comment. --- src/Plugin/GraphQL/Schema/SdlSchemaPluginBase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/GraphQL/Schema/SdlSchemaPluginBase.php b/src/Plugin/GraphQL/Schema/SdlSchemaPluginBase.php index 24d01cf31..ebd851e53 100644 --- a/src/Plugin/GraphQL/Schema/SdlSchemaPluginBase.php +++ b/src/Plugin/GraphQL/Schema/SdlSchemaPluginBase.php @@ -189,7 +189,7 @@ protected function getTypeResolver(TypeDefinitionNode $type) { * The parsed schema document. */ protected function getSchemaDocument() { - // Only use caching of the parsed document if aren't in development mode. + // Only use caching of the parsed document if we aren't in development mode. if (empty($this->inDevelopment) && $cache = $this->astCache->get($this->getPluginId())) { return $cache->data; } From ed0028fad692b2183be15d8cb48c23e7d39ad5e2 Mon Sep 17 00:00:00 2001 From: Radoslav Terezka Date: Tue, 28 May 2019 17:13:15 +0200 Subject: [PATCH 3/4] Add comment. --- src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php b/src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php index b5fdc21d5..2115040f1 100644 --- a/src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php +++ b/src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php @@ -6,6 +6,14 @@ use GraphQL\Language\Parser; use GraphQL\Utils\SchemaExtender; +/** + * Allows to extend the GraphQL schema. + * + * When GraphQL schema is distributed across multiple files it might be useful + * to allow extension of certain types. This schema plugin allows that. See an + * example of code to extend the schema with using webonyx/graphql-php library: + * https://github.com/webonyx/graphql-php/issues/180#issuecomment-444407411 + */ abstract class SdlExtendedSchemaPluginBase extends SdlSchemaPluginBase { /** From 756019cb0d4428d7fb1e66edd773dfa19920273d Mon Sep 17 00:00:00 2001 From: Radoslav Terezka Date: Tue, 28 May 2019 17:50:26 +0200 Subject: [PATCH 4/4] Fix comment. --- src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php b/src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php index 2115040f1..7ce557d36 100644 --- a/src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php +++ b/src/Plugin/GraphQL/Schema/SdlExtendedSchemaPluginBase.php @@ -23,7 +23,7 @@ abstract class SdlExtendedSchemaPluginBase extends SdlSchemaPluginBase { * The parsed extended schema document. */ protected function getExtendedSchemaDocument() { - // Only use caching of the parsed document if aren't in development mode. + // Only use caching of the parsed document if we aren't in development mode. if (empty($this->inDevelopment) && $cache = $this->astCache->get($this->getPluginId())) { return $cache->data; }