1. 14
    Expose Gutenberg Blocks to the Headless WordPress GraphQL Endpoint
    1m 53s

Expose Gutenberg Blocks to the Headless WordPress GraphQL Endpoint

Share this video with your friends

Send Tweet
Published 3 years ago
Updated 3 years ago

The same problem occurs as the previous lesson with the blocks that Gutenberg exposes in our API with the GraphQL endpoint.

In this lesson, we will show how to expose the Gutenberg Block data to the GraphQL layer by adding the wp-graphql-gutenberg plugin. There are a couple of things to note about the plugin. Whenever you use a new block type in your editor, you will have to update the blocks registry in the plugin so that it properly exposes the data in GraphQL. Also, when you query for block data you will need to be explicit in the attributes that you request. This behavior is different from REST where REST will serve you all the data available.

Instructor: [0:00] If we look at the schema for one of our post types chairs, for example, we'll see that we've got some content. We got access to the content, which is that HTML rendered blob that we normally expect to get, but we don't have access to the blocks.

[0:14] We install the module to expose the blocks to the RESTful API, and we need to do the same if we want to expose the blocks to the GraphQL API. I'm going to use the module WP GraphQL Gutenberg. It's not available in the WordPress directory, so we're going to download the zip file, go to our plugins, add new and upload that plugin, and then install, and then activate the plugin.

[0:39] We have a new menu item here of GraphQL Gutenberg, let's have a look at it. This manually tracks which blocks are being used in your posts and pages. If you add a new block, if you change and use a different block, you need to press Update in here, to update the block registry.

[0:55] If we turn to our GraphQL IDA, when the schema loads, you'll see we have these extra options, our blocks and our blocks JSON. For each block, we can get its name. All of the attributes we can get in the RESTful API, we can get here as well.

[1:08] Then we can also get the inner HTMLs. Let's see what we get when we do that. Blocks. For the core lists, we have these attributes as well as the actual original list. By default, a RESTful API sends all of the data.

[1:21] For a GraphQL API, we have to request any attributes that we want for a particular block explicitly. Let's look at a different block that we might call. If we had an image block, we'll want some of its attributes, like the alignment and the alt text, and the anchor, and the caption. We maybe want all of these.

[1:38] Then, we'll see for this image block, I have all of these attributes, including the image, and I can build up the image block.

[1:44] For each block that we allow our editors to use, our GraphQL query needs to correctly gather and request any of the attributes that we want to use.

~ 3 years ago

Note: following along from the previous lessons, I needed to disable the Markdown plugin, WP Githuber MD (1.15.2), before using WP GraphQL Gutenberg (0.3.5) and WP GraphQL (1.0.5) without errors.

Andrew HIlls
Andrew HIlls
~ 3 years ago

Been jumping around your course as I only want the block editor and WP GraphQL Gutenberg. Looks like the github project has to be built, not entirely sure. In the readme, I came across this link https://github.com/pristas-peter/wp-graphql-gutenberg/releases all built.

Kevin Cunningham
Kevin Cunninghaminstructor
~ 2 years ago

A bit late to the party here, Andrew, sorry! As this plugin is still not available in the plugin store, you need to download the zip file and then upload it to the WordPress instance like I've done in the video.

I did this earlier in the course for wp-rest-blocks which is why I rushed through it a bit here.