Skip to content

Commit

Permalink
fix: Ensure correct EditorBlock.type field resolution (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
justlevine authored Dec 3, 2024
1 parent c09aeb7 commit 9a2ebf7
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-donuts-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpengine/wp-graphql-content-blocks": patch
---

fix: Ensure correct `EditorBlock.type` field resolution.
3 changes: 3 additions & 0 deletions includes/Type/InterfaceType/EditorBlockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public static function register_type(): void {
'type' => [
'type' => 'String',
'description' => __( 'The (GraphQL) type of the block', 'wp-graphql-content-blocks' ),
'resolve' => static function ( $block ) {
return WPGraphQLHelpers::get_type_name_for_block( $block['blockName'] ?? null );
},
],
],
'resolveType' => static function ( $block ) {
Expand Down
12 changes: 10 additions & 2 deletions tests/unit/BlockQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function test_retrieve_non_flatten_editor_blocks() {
databaseId
editorBlocks(flat: false) {
name
type
}
}
}
Expand All @@ -70,6 +71,7 @@ public function test_retrieve_non_flatten_editor_blocks() {
// There should be only one block using that query when not using flat: true
$this->assertEquals( 1, count( $node['editorBlocks'] ) );
$this->assertEquals( 'core/columns', $node['editorBlocks'][0]['name'] );
$this->assertEquals( 'CoreColumns', $node['editorBlocks'][0]['type'] );
}

public function test_retrieve_flatten_editor_blocks() {
Expand All @@ -79,8 +81,9 @@ public function test_retrieve_flatten_editor_blocks() {
nodes {
databaseId
editorBlocks(flat: true) {
name
parentClientId
name
parentClientId
type
}
}
}
Expand All @@ -97,18 +100,23 @@ public function test_retrieve_flatten_editor_blocks() {
$this->assertEquals( 5, count( $node['editorBlocks'] ) );

$this->assertEquals( 'core/columns', $node['editorBlocks'][0]['name'] );
$this->assertEquals( 'CoreColumns', $node['editorBlocks'][0]['type'] );
$this->assertNull( $node['editorBlocks'][0]['parentClientId'] );

$this->assertEquals( 'core/column', $node['editorBlocks'][1]['name'] );
$this->assertEquals( 'CoreColumn', $node['editorBlocks'][1]['type'] );
$this->assertNotNull( $node['editorBlocks'][1]['parentClientId'] );

$this->assertEquals( 'core/paragraph', $node['editorBlocks'][2]['name'] );
$this->assertEquals( 'CoreParagraph', $node['editorBlocks'][2]['type'] );
$this->assertNotNull( $node['editorBlocks'][2]['parentClientId'] );

$this->assertEquals( 'core/column', $node['editorBlocks'][3]['name'] );
$this->assertEquals( 'CoreColumn', $node['editorBlocks'][3]['type'] );
$this->assertNotNull( $node['editorBlocks'][3]['parentClientId'] );

$this->assertEquals( 'core/paragraph', $node['editorBlocks'][4]['name'] );
$this->assertEquals( 'CoreParagraph', $node['editorBlocks'][4]['type'] );
$this->assertNotNull( $node['editorBlocks'][4]['parentClientId'] );
}
}
5 changes: 5 additions & 0 deletions tests/unit/BlockSupportsAnchorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function test_register_anchor_query_field() {
nodes {
editorBlocks {
name
type
... on BlockWithSupportsAnchor {
anchor
}
Expand All @@ -124,15 +125,19 @@ public function test_register_anchor_query_field() {

$this->assertEquals( 4, count( $node['editorBlocks'] ) );
$this->assertEquals( 'core/paragraph', $node['editorBlocks'][0]['name'] );
$this->assertEquals( 'CoreParagraph', $node['editorBlocks'][0]['type'] );
$this->assertEquals( 'example', $node['editorBlocks'][0]['anchor'] );

$this->assertEquals( 'core/paragraph', $node['editorBlocks'][1]['name'] );
$this->assertEquals( 'CoreParagraph', $node['editorBlocks'][1]['type'] );
$this->assertNull( $node['editorBlocks'][1]['anchor'] );

$this->assertEquals( 'core/group', $node['editorBlocks'][2]['name'] );
$this->assertEquals( 'CoreGroup', $node['editorBlocks'][2]['type'] );
$this->assertNull( $node['editorBlocks'][2]['anchor'] );

$this->assertEquals( 'core/paragraph', $node['editorBlocks'][3]['name'] );
$this->assertEquals( 'CoreParagraph', $node['editorBlocks'][3]['type'] );
$this->assertEquals( 'example-inner', $node['editorBlocks'][3]['anchor'] );
}
}
4 changes: 4 additions & 0 deletions tests/unit/CoreCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ className
... on BlockWithSupportsAnchor {
anchor
}
type
...CoreCodeBlockFragment
}
}
Expand Down Expand Up @@ -140,6 +141,7 @@ public function test_retrieve_core_code_attributes() {
$this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' );
$this->assertEquals( 'core/code', $block['name'], 'The block name should be core/code' );
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' );
$this->assertEquals( 'CoreCode', $block['type'], 'The block type should be CoreCode' );
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' );

$attributes = $block['attributes'];
Expand Down Expand Up @@ -204,6 +206,7 @@ public function test_retrieve_core_code_with_custom_styles() {
$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals( 'core/code', $block['name'], 'The block name should be core/code' );
$this->assertEquals( 'CoreCode', $block['type'], 'The block type should be CoreCode' );

$attributes = $block['attributes'];
$this->assertEquals(
Expand Down Expand Up @@ -281,6 +284,7 @@ public function test_retrieve_core_code_with_gradient_and_additional_attributes(
$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals( 'core/code', $block['name'], 'The block name should be core/code' );
$this->assertEquals( 'CoreCode', $block['type'], 'The block type should be CoreCode' );

$attributes = $block['attributes'];
$this->assertEquals(
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/CoreHeadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ className
name
parentClientId
renderedHtml
type
... on BlockWithSupportsAnchor {
anchor
}
Expand Down Expand Up @@ -141,6 +142,7 @@ public function test_retrieve_core_heading_attributes() {
$this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' );
$this->assertEquals( 'core/heading', $block['name'], 'The block name should be core/heading' );
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' );
$this->assertEquals( 'CoreHeading', $block['type'], 'The block type should be CoreHeading' );
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' );

$attributes = $block['attributes'];
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/CoreImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ className
innerBlocks {
name
}
name
parentClientId
renderedHtml
name
type
...CoreImageBlockFragment
}
}
Expand Down Expand Up @@ -140,6 +141,7 @@ public function test_retrieve_core_image_fields_attributes(): void {
$this->assertEquals( $this->post_id, $node['databaseId'] );
$this->assertEquals( 1, count( $node['editorBlocks'] ) );
$this->assertEquals( 'core/image', $node['editorBlocks'][0]['name'] );
$this->assertEquals( 'CoreImage', $node['editorBlocks'][0]['type'] );

$this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' );
$this->assertArrayHasKey( 'data', $actual, 'The data key should be present' );
Expand All @@ -157,6 +159,7 @@ public function test_retrieve_core_image_fields_attributes(): void {
$this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' );
$this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' );
$this->assertEquals( 'core/image', $block['name'], 'The block name should be core/image' );
$this->assertEquals( 'CoreImage', $block['type'], 'The block type should be CoreImage' );
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' );
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' );
$this->assertEquals(
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/CoreListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ className
name
parentClientId
renderedHtml
type
... on CoreListItem {
...CoreListItemBlockFragment
}
Expand All @@ -93,6 +94,7 @@ className
name
parentClientId
renderedHtml
type
...CoreListBlockFragment
}
}
Expand Down Expand Up @@ -149,6 +151,7 @@ public function test_retrieve_core_list_fields_and_attributes(): void {
$this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' );

$this->assertEquals( 'core/list', $block['name'], 'The block name should be core/list' );
$this->assertEquals( 'CoreList', $block['type'], 'The block type should be CoreList' );
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' );
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' );

Expand Down Expand Up @@ -184,6 +187,7 @@ public function test_retrieve_core_list_fields_and_attributes(): void {
$this->assertEmpty( $block['innerBlocks'][0]['cssClassNames'], 'The cssClassNames should be present' );
$this->assertNotEmpty( $block['innerBlocks'][0]['clientId'], 'The clientId should be present' );
$this->assertEquals( 'core/list-item', $block['innerBlocks'][0]['name'], 'The block name should be core/list-item' );
$this->assertEquals( 'CoreListItem', $block['innerBlocks'][0]['type'], 'The block type should be CoreListItem' );
$this->assertNotEmpty( $block['innerBlocks'][0]['renderedHtml'], 'The renderedHtml should be present' );

$this->assertEquals(
Expand Down Expand Up @@ -250,6 +254,7 @@ public function test_retrieve_core_list_attributes_typography_and_lock(): void {
$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals( 'core/list', $block['name'], 'The block name should be core/list' );
$this->assertEquals( 'CoreList', $block['type'], 'The block type should be CoreList' );

$this->assertEquals(
[
Expand Down Expand Up @@ -318,6 +323,7 @@ public function test_retrieve_core_list_attributes_ordered_and_reversed(): void
$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals( 'core/list', $block['name'], 'The block name should be core/list' );
$this->assertEquals( 'CoreList', $block['type'], 'The block type should be CoreList' );

$this->assertEquals(
[
Expand Down Expand Up @@ -383,6 +389,7 @@ public function test_retrieve_core_list_attributes_start_and_styles(): void {
$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals( 'core/list', $block['name'], 'The block name should be core/list' );
$this->assertEquals( 'CoreList', $block['type'], 'The block type should be CoreList' );

$this->assertEquals(
[
Expand Down Expand Up @@ -653,6 +660,7 @@ className
editorBlocks( flat: false ) {
clientId
name
type
parentClientId
innerBlocks {
... on CoreListItem {
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/CoreParagraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ className
name
parentClientId
renderedHtml
type
... on BlockWithSupportsAnchor {
anchor
}
Expand Down Expand Up @@ -141,6 +142,7 @@ public function test_retrieve_core_paragraph_attributes() {

$this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' );
$this->assertEquals( 'core/paragraph', $block['name'], 'The block name should be core/paragraph' );
$this->assertEquals( 'CoreParagraph', $block['type'], 'The block type should be CoreParagraph' );
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' );
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' );

Expand Down Expand Up @@ -210,6 +212,7 @@ public function test_retrieve_core_paragraph_with_drop_cap_and_custom_styles() {
$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals( 'core/paragraph', $block['name'], 'The block name should be core/paragraph' );
$this->assertEquals( 'CoreParagraph', $block['type'], 'The block type should be CoreParagraph' );

$attributes = $block['attributes'];
$this->assertEquals(
Expand Down Expand Up @@ -293,6 +296,7 @@ public function test_retrieve_core_paragraph_with_direction_and_gradient() {
$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals( 'core/paragraph', $block['name'], 'The block name should be core/paragraph' );
$this->assertEquals( 'CoreParagraph', $block['type'], 'The block type should be CoreParagraph' );

$attributes = $block['attributes'];
$this->assertEquals(
Expand Down Expand Up @@ -360,6 +364,7 @@ public function test_retrieve_core_paragraph_with_additional_attributes() {
$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals( 'core/paragraph', $block['name'], 'The block name should be core/paragraph' );
$this->assertEquals( 'CoreParagraph', $block['type'], 'The block type should be CoreParagraph' );

$attributes = $block['attributes'];
$this->assertEquals(
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/CorePreformattedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ className
name
parentClientId
renderedHtml
type
... on BlockWithSupportsAnchor {
anchor
}
Expand Down Expand Up @@ -138,6 +139,7 @@ public function test_retrieve_core_preformatted_attributes() {

$this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' );
$this->assertEquals( 'core/preformatted', $block['name'], 'The block name should be core/preformatted' );
$this->assertEquals( 'CorePreformatted', $block['type'], 'The block type should be CorePreformatted' );
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' );
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' );

Expand Down Expand Up @@ -203,6 +205,7 @@ public function test_retrieve_core_preformatted_with_custom_styles() {
$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals( 'core/preformatted', $block['name'], 'The block name should be core/preformatted' );
$this->assertEquals( 'CorePreformatted', $block['type'], 'The block type should be CorePreformatted' );

$attributes = $block['attributes'];
$this->assertEquals(
Expand Down Expand Up @@ -278,6 +281,7 @@ public function test_retrieve_core_preformatted_with_lock() {
$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals( 'core/preformatted', $block['name'], 'The block name should be core/preformatted' );
$this->assertEquals( 'CorePreformatted', $block['type'], 'The block type should be CorePreformatted' );

$attributes = $block['attributes'];
$this->assertEquals(
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/CoreQuoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ className
isDynamic
name
parentClientId
type
renderedHtml
...CoreQuoteBlockFragment
}
Expand Down Expand Up @@ -126,6 +127,7 @@ public function test_retrieve_core_quote_fields_and_attributes(): void {

$this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' );
$this->assertEquals( 'core/quote', $block['name'], 'The block name should be core/quote' );
$this->assertEquals( 'CoreQuote', $block['type'], 'The block type should be CoreQuote' );
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' );
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' );

Expand Down Expand Up @@ -201,6 +203,7 @@ public function test_retrieve_core_quote_additional_attributes(): void {
$this->assertEquals( 'core/paragraph', $block['innerBlocks'][0]['name'], 'The inner block name should be core/paragraph' );

$this->assertEquals( 'core/quote', $block['name'], 'The block name should be core/quote' );
$this->assertEquals( 'CoreQuote', $block['type'], 'The block type should be CoreQuote' );
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' );
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' );

Expand Down
3 changes: 3 additions & 0 deletions tests/unit/CoreSeparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ className
name
parentClientId
renderedHtml
type
...CoreSeparatorBlockFragment
}
}
Expand Down Expand Up @@ -111,6 +112,7 @@ public function test_retrieve_core_separator_attribute_fields(): void {
$this->assertNotEmpty( $block['cssClassNames'], 'There should be cssClassNames' );
$this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' );
$this->assertEquals( 'core/separator', $block['name'], 'The block name should be core/separator' );
$this->assertEquals( 'CoreSeparator', $block['type'], 'The block type should be CoreSeparator' );
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' );
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' );

Expand Down Expand Up @@ -175,6 +177,7 @@ public function test_retrieve_core_separator_attributes(): void {
$this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' );
$this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) );
$this->assertEquals( 'core/separator', $actual['data']['post']['editorBlocks'][0]['name'], 'The block name should be core/separator' );
$this->assertEquals( 'CoreSeparator', $actual['data']['post']['editorBlocks'][0]['type'], 'The block type should be CoreSeparator' );

// Verify the attributes.
$this->assertEquals(
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/CoreTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ className
name
parentClientId
renderedHtml
type
... on BlockWithSupportsAnchor {
anchor
}
Expand Down Expand Up @@ -145,6 +146,7 @@ public function test_retrieve_core_table_attribute_fields() {

$this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' );
$this->assertEquals( 'core/table', $block['name'], 'The block name should be core/table' );
$this->assertEquals( 'CoreTable', $block['type'], 'The block type should be CoreTable' );
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' );
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' );

Expand Down
2 changes: 2 additions & 0 deletions tests/unit/CoreVideoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ className
}
parentClientId
renderedHtml
type
...CoreVideoBlockFragment
}
}
Expand Down Expand Up @@ -137,6 +138,7 @@ public function test_retrieve_core_video_attributes(): void {
$this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' );
$this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' );
$this->assertEquals( 'core/video', $block['name'], 'The block name should be core/video' );
$this->assertEquals( 'CoreVideo', $block['type'], 'The block type should be CoreVideo' );
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' );
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' );

Expand Down

0 comments on commit 9a2ebf7

Please sign in to comment.