From 1e224bce52321cf7395031134c291df1e4581a59 Mon Sep 17 00:00:00 2001 From: "K.I.T.T" Date: Thu, 2 Jun 2022 12:17:42 +0000 Subject: [PATCH] chore: release version 1.21.0 --- CHANGELOG.md | 11 ++++ composer.json | 2 +- package.json | 4 +- .../SilverbackGatsbySchemaExtension.php | 34 +++++++++++ tests/queries/editor.gql | 5 ++ tests/src/Kernel/EditorBlocksTest.php | 56 +++++++++++++------ 6 files changed, 91 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e913eb..98e5cf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [1.21.0](https://github.com/AmazeeLabs/silverback-mono/compare/@-amazeelabs/silverback_gatsby@1.20.0...@-amazeelabs/silverback_gatsby@1.21.0) (2022-06-02) + + +### Features + +* **gatsby:** introduce `__original_typename` field on each graphql type ([edbf247](https://github.com/AmazeeLabs/silverback-mono/commit/edbf2477c3c4de547e9600c6670d2175ced113c7)) + + + + + # [1.20.0](https://github.com/AmazeeLabs/silverback-mono/compare/@-amazeelabs/silverback_gatsby@1.19.5...@-amazeelabs/silverback_gatsby@1.20.0) (2022-05-27) diff --git a/composer.json b/composer.json index b64f7c7..08e9bbf 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "amazeelabs/silverback_gatsby", "type": "drupal-module", - "version": "1.20.0", + "version": "1.21.0", "description": "Bridge module between Gatsby and Drupal.", "homepage": "https://silverback.netlify.app", "license": "GPL-2.0+", diff --git a/package.json b/package.json index 208f2e0..30ff2e7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@-amazeelabs/silverback_gatsby", - "version": "1.20.0", + "version": "1.21.0", "main": "index.js", "scripts": { "version": "sync-composer-version", @@ -16,5 +16,5 @@ "repository": "git@github.com:AmazeeLabs/silverback_gatsby.git", "branch": "main" }, - "gitHead": "78cebd6b4f85a05d86c2c924e6363bee4e603d06" + "gitHead": "8d54bdfbe0d6b243e676ffa1b5b2948e508f4f1f" } diff --git a/src/Plugin/GraphQL/SchemaExtension/SilverbackGatsbySchemaExtension.php b/src/Plugin/GraphQL/SchemaExtension/SilverbackGatsbySchemaExtension.php index a0721bb..63a18b9 100644 --- a/src/Plugin/GraphQL/SchemaExtension/SilverbackGatsbySchemaExtension.php +++ b/src/Plugin/GraphQL/SchemaExtension/SilverbackGatsbySchemaExtension.php @@ -302,6 +302,7 @@ public function getExtensionDefinition() { // Collect all active feeds and prepend their definitions to the schema. $schema = array_map(fn (FeedInterface $feed) => $this->getSchemaDefinitions($feed), $this->getFeeds()); array_unshift($schema, parent::getExtensionDefinition()); + array_unshift($schema, $this->getOriginalTypenameDefinitions()); return implode("\n", $schema); } @@ -312,6 +313,39 @@ public function registerResolvers(ResolverRegistryInterface $registry) { $builder = new ResolverBuilder(); $this->addFieldResolvers($registry, $builder); $this->addTypeResolvers($registry, $builder); + $this->addOriginalTypenameResolvers($registry, $builder); + } + + /** + * Attach a _original_typename field to every type. + * + * To preserve the original type in cases where the types are namespaced and + * merged into a different graphql schema (e.g. Gatsby). + * + * @return string + */ + protected function getOriginalTypenameDefinitions() { + $types = []; + foreach ($this->parentAst->definitions->getIterator() as $definition) { + if ($definition instanceof ObjectTypeDefinitionNode) { + $name = $definition->name->value; + $types[] = "extend type {$name} { _original_typename: String! }"; + } + } + return implode("\n", $types); + } + + /** + * Attach a _original_typename resolvers. + * + * @return void + */ + protected function addOriginalTypenameResolvers(ResolverRegistry $registry, ResolverBuilder $builder) { + foreach ($this->parentAst->definitions->getIterator() as $definition) { + if ($definition instanceof ObjectTypeDefinitionNode) { + $registry->addFieldResolver($definition->name->value, '_original_typename', $builder->fromValue($definition->name->value)); + } + } } /** diff --git a/tests/queries/editor.gql b/tests/queries/editor.gql index 7cae0a2..d8c2caa 100644 --- a/tests/queries/editor.gql +++ b/tests/queries/editor.gql @@ -3,15 +3,18 @@ fragment BlockPage on Page { content { __typename ... on Text { + _original_typename content } ... on Figure { + _original_typename caption image { alt } } ... on Columns { + _original_typename columns { __typename } @@ -21,10 +24,12 @@ fragment BlockPage on Page { query { en:loadPage(id: "1:en") { + _original_typename ...BlockPage } de:loadPage(id: "1:de") { + _original_typename ...BlockPage } } diff --git a/tests/src/Kernel/EditorBlocksTest.php b/tests/src/Kernel/EditorBlocksTest.php index 637e33f..9df9f54 100644 --- a/tests/src/Kernel/EditorBlocksTest.php +++ b/tests/src/Kernel/EditorBlocksTest.php @@ -123,44 +123,64 @@ function testEditorBlockResolution() { 'en' => [ 'title' => 'Editor test', 'content' => [ - ['__typename' => 'Text', 'content' => '

A test paragraph

Another test paragraph

'], + [ + '__typename' => 'Text', + 'content' => '

A test paragraph

Another test paragraph

', + '_original_typename' => 'Text', + ], [ '__typename' => 'Figure', 'caption' => 'This is the caption', 'image' => [ 'alt' => 'Screaming hairy armadillo' - ] - ], - ['__typename' => 'Columns', 'columns' => [ - [ - '__typename' => 'Text', ], - [ - '__typename' => 'Text', + '_original_typename' => 'Figure', + ], + [ + '__typename' => 'Columns', + 'columns' => [ + [ + '__typename' => 'Text', + ], + [ + '__typename' => 'Text', + ], ], - ]], + '_original_typename' => 'Columns', + ], ], + '_original_typename' => 'Page', ], 'de' => [ 'title' => 'Editor test DE', 'content' => [ - ['__typename' => 'Text', 'content' => '

A test paragraph

Another test paragraph

'], + [ + '__typename' => 'Text', + 'content' => '

A test paragraph

Another test paragraph

', + '_original_typename' => 'Text', + ], [ '__typename' => 'Figure', 'caption' => 'This is the caption', 'image' => [ 'alt' => 'Screaming hairy armadillo DE' - ] - ], - ['__typename' => 'Columns', 'columns' => [ - [ - '__typename' => 'Text', ], - [ - '__typename' => 'Text', + '_original_typename' => 'Figure', + ], + [ + '__typename' => 'Columns', + 'columns' => [ + [ + '__typename' => 'Text', + ], + [ + '__typename' => 'Text', + ], ], - ]], + '_original_typename' => 'Columns' + ], ], + '_original_typename' => 'Page', ], ], $metadata); }