(Gutenberg) blokken
Dit is hoe je Gutenberg-blokdata ophaalt.
Het GraphQL-schema heeft de volgende velden toegevoegd aan alle CustomPost-types (zoals Post en Page):
blocksblockDataItemsblockFlattenedDataItems
Deze velden zijn niet ingeschakeld als de Classic Editor-plugin actief is.
blocks
Het veld CustomPost.blocks: [BlockUnion!] haalt de lijst op van alle blokken in de custom post.
blocks geeft een lijst terug van de Block-types die zijn gekoppeld aan het GraphQL-schema. Al deze Block-types maken deel uit van het BlockUnion-type en implementeren de Block-interface.
De plugin implementeert één Block-type, GenericBlock, wat al voldoende is om de data van elk blok op te halen (via het veld attributes: JSONObject).
Deze query:
{
post(by: { id: 1 }) {
blocks {
...on Block {
name
attributes
innerBlocks {
...on Block {
name
attributes
innerBlocks {
...on Block {
name
attributes
}
}
}
}
}
}
}
}
}...geeft deze respons terug:
{
"data": {
"post": {
"blocks": [
{
"name": "core/gallery",
"attributes": {
"linkTo": "none",
"className": "alignnone",
"images": [
{
"url": "https://d.pr/i/zd7Ehu+",
"alt": "",
"id": "1706"
},
{
"url": "https://d.pr/i/jXLtzZ+",
"alt": "",
"id": "1705"
}
],
"ids": [],
"shortCodeTransforms": [],
"imageCrop": true,
"fixedHeight": true,
"sizeSlug": "large",
"allowResize": false
},
"innerBlocks": null
},
{
"name": "core/heading",
"attributes": {
"content": "List Block",
"level": 2
},
"innerBlocks": null
},
{
"name": "core/list",
"attributes": {
"ordered": false,
"values": "<li>List item 1</li><li>List item 2</li><li>List item 3</li><li>List item 4</li>"
},
"innerBlocks": null
},
{
"name": "core/heading",
"attributes": {
"className": "has-top-margin",
"content": "Columns Block",
"level": 2
},
"innerBlocks": null
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"innerBlocks": [
{
"name": "core/column",
"attributes": {},
"innerBlocks": [
{
"name": "core/image",
"attributes": {
"id": 1701,
"className": "layout-column-1",
"url": "https://d.pr/i/fW6V3V+",
"alt": ""
},
"innerBlocks": null
}
]
},
{
"name": "core/column",
"attributes": {},
"innerBlocks": [
{
"name": "core/paragraph",
"attributes": {
"className": "layout-column-2",
"content": "Phosfluorescently morph intuitive relationships rather than customer directed human capital.",
"dropCap": false
},
"innerBlocks": null
}
]
}
]
},
{
"name": "core/heading",
"attributes": {
"content": "Columns inside Columns (nested inner blocks)",
"level": 2
},
"innerBlocks": null
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"innerBlocks": [
{
"name": "core/column",
"attributes": {},
"innerBlocks": [
{
"name": "core/image",
"attributes": {
"id": 1701,
"className": "layout-column-1",
"url": "https://d.pr/i/fW6V3V+",
"alt": ""
},
"innerBlocks": null
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"innerBlocks": [
{
"name": "core/column",
"attributes": {
"width": "33.33%"
},
"innerBlocks": [
{
"name": "core/heading",
"attributes": {
"fontSize": "large",
"content": "Life is so rich",
"level": 2
},
"innerBlocks": null
},
{
"name": "core/heading",
"attributes": {
"level": 3,
"content": "Life is so dynamic"
},
"innerBlocks": null
}
]
},
{
"name": "core/column",
"attributes": {
"width": "66.66%"
},
"innerBlocks": [
{
"name": "core/paragraph",
"attributes": {
"content": "This rhyming poem is the spark that can reignite the fires within you. It challenges you to go out and live your life in the present moment as a \u201chero\u201d and leave your mark on this world.",
"dropCap": false
},
"innerBlocks": null
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"innerBlocks": [
{
"name": "core/column",
"attributes": {},
"innerBlocks": [
{
"name": "core/image",
"attributes": {
"id": 361,
"sizeSlug": "large",
"linkDestination": "none",
"url": "https://gato-graphql.lndo.site/wp-content/uploads/2022/05/graphql-voyager-public-1024x622.jpg",
"alt": ""
},
"innerBlocks": null
}
]
},
{
"name": "core/column",
"attributes": {},
"innerBlocks": null
},
{
"name": "core/column",
"attributes": {},
"innerBlocks": [
{
"name": "core/image",
"attributes": {
"id": 362,
"sizeSlug": "large",
"linkDestination": "none",
"url": "https://gato-graphql.lndo.site/wp-content/uploads/2022/05/namespaced-interactive-schema-1024x598.webp",
"alt": ""
},
"innerBlocks": null
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
}
}Het GraphQL-schema voor de Block-types ziet er als volgt uit:
interface Block {
name: String!
attributes: JSONObject
innerBlocks: [BlockUnion!]
contentSource: HTML!
}
type GenericBlock implements Block {
name: String!
attributes: JSONObject
innerBlocks: [BlockUnion!]
contentSource: HTML!
}
union BlockUnion = GenericBlockVelden van Block
De Block-interface (en daarmee ook het type GeneralBlock) bevat de volgende velden:
namehaalt de naam van het blok op:"core/paragraph","core/heading","core/image", enz.attributeshaalt een JSON-object op met alle attributen van het blok.innerBlockshaalt[BlockUnion!]op, waardoor we de hiërarchie van blokken met inner blocks kunnen doorlopen en de data voor al deze blokken kunnen ophalen, voor zoveel niveaus diep als er in onze inhoud aanwezig zijn.contentSourcehaalt de (Gutenberg) HTML-broncode van het blok op, inclusief de commentaarscheidingstekens die de attributen bevatten. Dit veld haalt echter niet exact dezelfde data op als hoe deze is opgeslagen in de database (zie #2346), dus gebruik dit veld met zorg.
GeneralBlock rechtstreeks ophalen (in plaats van BlockUnion)
Omdat er momenteel maar één Block-type is dat blokken koppelt — GeneralBlock — is het logisch om CustomPost.blocks (en ook Block.innerBlocks) dit type rechtstreeks te laten ophalen, in plaats van het BlockUnion-type.
Dit kun je doen op de instellingenpagina onder het tabblad Blocks, door de optie Use single type instead of union type? aan te vinken:

De GraphQL-query wordt dan vereenvoudigd:
{
post(by: { id: 1 }) {
blocks {
name
attributes
innerBlocks {
name
attributes
}
}
}
}Let op: het responstype als BlockUnion behouden is gunstig voor achterwaartse compatibiliteit. Als we ooit besluiten blok-specifieke types aan het schema toe te voegen (zie de sectie hieronder), zijn er geen breaking changes.
Blok-specifieke types koppelen
Het type JSONObject (zoals opgehaald via Block.attributes) is niet strikt getypeerd: de eigenschappen kunnen elk type en kardinaliteit hebben (String, Int, [Boolean!], enz.), waardoor we deze informatie voor elk blok moeten kennen en elk geval aan de clientzijde moeten afhandelen.
Als we strikte typering nodig hebben, moeten we het GraphQL-schema uitbreiden via PHP-code, door blok-specifieke types toe te voegen die de specifieke attributen van een blok als velden koppelen, en ze onderdeel te maken van BlockUnion.
Zo kunnen we bijvoorbeeld het type CoreParagraphBlock toevoegen dat het blok core/paragraph koppelt, met een veld content van het type String.
Raadpleeg de documentatie in GatoGraphQL/GatoGraphQL om te leren hoe je het GraphQL-schema kunt uitbreiden (currently a work in progress).
Blokken filteren
Het veld CustomPost.blocks bevat het argument filterBy met twee eigenschappen: include en exclude. Hiermee kun je filteren welke blokken worden opgehaald, op basis van de bloknaam:
{
post(by: { id: 1 }) {
id
blocks(
filterBy: {
include: [
"core/heading",
"core/gallery"
]
}
) {
name
attributes
}
}
}Dit geeft het volgende terug:
{
"data": {
"post": {
"blocks": [
{
"name": "core/gallery",
"attributes": {
"linkTo": "none",
"className": "alignnone",
"images": [
{
"url": "https://d.pr/i/zd7Ehu+",
"alt": "",
"id": "1706"
},
{
"url": "https://d.pr/i/jXLtzZ+",
"alt": "",
"id": "1705"
}
],
"ids": [],
"shortCodeTransforms": [],
"imageCrop": true,
"fixedHeight": true,
"sizeSlug": "large",
"allowResize": false
},
"innerBlocks": null
},
{
"name": "core/heading",
"attributes": {
"content": "List Block",
"level": 2
},
"innerBlocks": null
},
{
"name": "core/heading",
"attributes": {
"className": "has-top-margin",
"content": "Columns Block",
"level": 2
},
"innerBlocks": null
},
{
"name": "core/heading",
"attributes": {
"content": "Columns inside Columns (nested inner blocks)",
"level": 2
},
"innerBlocks": null
}
]
}
}
}Let op: niet alle blokken van het type core/heading zijn opgenomen. Die welke genest zijn onder core/column zijn uitgesloten, omdat er geen manier is om ze te bereiken (omdat de blokken core/columns en core/column zelf ook zijn uitgesloten).
Nadelen van het veld blocks
Het veld blocks heeft als nadeel dat je, om alle blokdata in de custom post op te halen — inclusief de data van inner blocks, hun eigen inner blocks, enzovoort — moet weten hoeveel geneste blokniveaus er in de inhoud aanwezig zijn, en deze informatie moet weerspiegelen in de GraphQL-query.
Of, als je dat niet weet, moet je de query samenstellen met voldoende niveaus om zeker te zijn dat alle data wordt opgehaald.
Deze query haalt bijvoorbeeld tot 7 niveaus van inner block-nesting op:
{
post(by: { id: 1 }) {
blocks {
...BlockData
}
}
}
fragment BlockData on Block {
name
attributes
innerBlocks {
...on Block {
name
attributes
innerBlocks {
...on Block {
name
attributes
innerBlocks {
...on Block {
name
attributes
innerBlocks {
...on Block {
name
attributes
innerBlocks {
...on Block {
name
attributes
innerBlocks {
...on Block {
name
attributes
innerBlocks {
...on Block {
name
attributes
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}blockDataItems
Om het nadeel te vermijden van hoe het veld blocks alle data ophaalt (inclusief data van inner blocks, hun inner blocks, enzovoort), bestaat het veld CustomPost.blockDataItems.
Dit veld geeft, in plaats van [BlockUnion], [JSONObject!] terug:
type CustomPost {
blockDataItems: [JSONObject!]
}Met andere woorden: in plaats van de typische GraphQL-aanpak waarbij entiteiten naar entiteiten verwijzen en je erdoorheen navigeert, produceert elke Block-entiteit op het hoogste niveau al de volledige blokdata voor zichzelf en al haar kinderen, in één enkel JSONObject-resultaat.
Het JSON-object bevat de eigenschappen van het blok (onder de items name en attributes) en die van zijn inner blocks (onder het item innerBlocks), recursief.
De volgende query:
{
post(by: { id: 1 }) {
blockDataItems
}
}...geeft het volgende terug:
{
"data": {
"post": {
"blockDataItems": [
{
"name": "core/gallery",
"attributes": {
"linkTo": "none",
"className": "alignnone",
"images": [
{
"url": "https://d.pr/i/zd7Ehu+",
"alt": "",
"id": "1706"
},
{
"url": "https://d.pr/i/jXLtzZ+",
"alt": "",
"id": "1705"
}
],
"ids": [],
"shortCodeTransforms": [],
"imageCrop": true,
"fixedHeight": true,
"sizeSlug": "large",
"allowResize": false
}
},
{
"name": "core/heading",
"attributes": {
"content": "List Block",
"level": 2
}
},
{
"name": "core/list",
"attributes": {
"ordered": false,
"values": "<li>List item 1</li><li>List item 2</li><li>List item 3</li><li>List item 4</li>"
}
},
{
"name": "core/heading",
"attributes": {
"className": "has-top-margin",
"content": "Columns Block",
"level": 2
}
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"innerBlocks": [
{
"name": "core/column",
"attributes": {},
"innerBlocks": [
{
"name": "core/image",
"attributes": {
"id": 1701,
"className": "layout-column-1",
"url": "https://d.pr/i/fW6V3V+",
"alt": ""
}
}
]
},
{
"name": "core/column",
"attributes": {},
"innerBlocks": [
{
"name": "core/paragraph",
"attributes": {
"className": "layout-column-2",
"content": "Phosfluorescently morph intuitive relationships rather than customer directed human capital.",
"dropCap": false
}
}
]
}
]
},
{
"name": "core/heading",
"attributes": {
"content": "Columns inside Columns (nested inner blocks)",
"level": 2
}
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"innerBlocks": [
{
"name": "core/column",
"attributes": {},
"innerBlocks": [
{
"name": "core/image",
"attributes": {
"id": 1701,
"className": "layout-column-1",
"url": "https://d.pr/i/fW6V3V+",
"alt": ""
}
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"innerBlocks": [
{
"name": "core/column",
"attributes": {
"width": "33.33%"
},
"innerBlocks": [
{
"name": "core/heading",
"attributes": {
"fontSize": "large",
"content": "Life is so rich",
"level": 2
}
},
{
"name": "core/heading",
"attributes": {
"level": 3,
"content": "Life is so dynamic"
}
}
]
},
{
"name": "core/column",
"attributes": {
"width": "66.66%"
},
"innerBlocks": [
{
"name": "core/paragraph",
"attributes": {
"content": "This rhyming poem is the spark that can reignite the fires within you. It challenges you to go out and live your life in the present moment as a \u201chero\u201d and leave your mark on this world.",
"dropCap": false
}
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"innerBlocks": [
{
"name": "core/column",
"attributes": {},
"innerBlocks": [
{
"name": "core/image",
"attributes": {
"id": 361,
"sizeSlug": "large",
"linkDestination": "none",
"url": "https://gato-graphql.lndo.site/wp-content/uploads/2022/05/graphql-voyager-public-1024x622.jpg",
"alt": ""
}
}
]
},
{
"name": "core/column",
"attributes": {}
},
{
"name": "core/column",
"attributes": {},
"innerBlocks": [
{
"name": "core/image",
"attributes": {
"id": 362,
"sizeSlug": "large",
"linkDestination": "none",
"url": "https://gato-graphql.lndo.site/wp-content/uploads/2022/05/namespaced-interactive-schema-1024x598.webp",
"alt": ""
}
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
}
}Blokdata-items filteren
Net als blocks biedt blockDataItems ook de mogelijkheid om te filteren welke blokken worden opgehaald, via het argument filterBy.
Deze query:
{
post(by: { id: 1 }) {
id
blockDataItems(
filterBy: {
include: [
"core/heading"
]
}
)
}
}...geeft het volgende terug:
{
"data": {
"post": {
"blockDataItems": [
{
"name": "core/heading",
"attributes": {
"content": "List Block",
"level": 2
},
"innerBlocks": null
},
{
"name": "core/heading",
"attributes": {
"className": "has-top-margin",
"content": "Columns Block",
"level": 2
},
"innerBlocks": null
},
{
"name": "core/heading",
"attributes": {
"content": "Columns inside Columns (nested inner blocks)",
"level": 2
},
"innerBlocks": null
}
]
}
}
}Let op: net als bij blocks zijn niet alle blokken van het type core/heading opgenomen. Die welke genest zijn onder core/column zijn uitgesloten, omdat er geen manier is om ze te bereiken (omdat de blokken core/columns en core/column zelf ook zijn uitgesloten).
blockFlattenedDataItems
Beide velden, blocks en blockDataItems, bieden de mogelijkheid om te filteren welke blokken worden opgehaald (via het argument filterBy). In beide gevallen wordt een blok dat voldoet aan de inclusievoorwaarde maar genest is in een blok dat dat niet doet, uitgesloten.
Er zijn echter situaties waarin je alle blokken van een bepaald type uit de custom post moet ophalen, ongeacht waar die blokken zich bevinden in de hiërarchie. Je wilt bijvoorbeeld alle blokken van het type core/image ophalen om alle afbeeldingen in een blogpost te verzamelen.
Om in deze behoefte te voorzien bestaat het veld CustomPost.blockFlattenedDataItems. In tegenstelling tot de velden blocks en blockDataItems maakt het de blokhiërarchie plat naar één enkel niveau.
Deze query:
{
post(by: { id: 1 }) {
blockFlattenedDataItems
}
}...geeft het volgende terug:
{
"data": {
"post": {
"blockFlattenedDataItems": [
{
"name": "core/gallery",
"attributes": {
"linkTo": "none",
"className": "alignnone",
"images": [
{
"url": "https://d.pr/i/zd7Ehu+",
"alt": "",
"id": "1706"
},
{
"url": "https://d.pr/i/jXLtzZ+",
"alt": "",
"id": "1705"
}
],
"ids": [],
"shortCodeTransforms": [],
"imageCrop": true,
"fixedHeight": true,
"sizeSlug": "large",
"allowResize": false
},
"innerBlockPositions": null,
"parentBlockPosition": null
},
{
"name": "core/heading",
"attributes": {
"content": "List Block",
"level": 2
},
"innerBlockPositions": null,
"parentBlockPosition": null
},
{
"name": "core/list",
"attributes": {
"ordered": false,
"values": "<li>List item 1</li><li>List item 2</li><li>List item 3</li><li>List item 4</li>"
},
"innerBlockPositions": null,
"parentBlockPosition": null
},
{
"name": "core/heading",
"attributes": {
"className": "has-top-margin",
"content": "Columns Block",
"level": 2
},
"innerBlockPositions": null,
"parentBlockPosition": null
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"innerBlockPositions": [
5,
7
],
"parentBlockPosition": null
},
{
"name": "core/column",
"attributes": {},
"parentBlockPosition": 4,
"innerBlockPositions": [
6
]
},
{
"name": "core/image",
"attributes": {
"id": 1701,
"className": "layout-column-1",
"url": "https://d.pr/i/fW6V3V+",
"alt": ""
},
"parentBlockPosition": 5,
"innerBlockPositions": null
},
{
"name": "core/column",
"attributes": {},
"parentBlockPosition": 4,
"innerBlockPositions": [
8
]
},
{
"name": "core/paragraph",
"attributes": {
"className": "layout-column-2",
"content": "Phosfluorescently morph intuitive relationships rather than customer directed human capital.",
"dropCap": false
},
"parentBlockPosition": 7,
"innerBlockPositions": null
},
{
"name": "core/heading",
"attributes": {
"content": "Columns inside Columns (nested inner blocks)",
"level": 2
},
"innerBlockPositions": null,
"parentBlockPosition": null
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"innerBlockPositions": [
11
],
"parentBlockPosition": null
},
{
"name": "core/column",
"attributes": {},
"parentBlockPosition": 10,
"innerBlockPositions": [
12,
13
]
},
{
"name": "core/image",
"attributes": {
"id": 1701,
"className": "layout-column-1",
"url": "https://d.pr/i/fW6V3V+",
"alt": ""
},
"parentBlockPosition": 11,
"innerBlockPositions": null
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"parentBlockPosition": 11,
"innerBlockPositions": [
14,
17
]
},
{
"name": "core/column",
"attributes": {
"width": "33.33%"
},
"parentBlockPosition": 13,
"innerBlockPositions": [
15,
16
]
},
{
"name": "core/heading",
"attributes": {
"fontSize": "large",
"content": "Life is so rich",
"level": 2
},
"parentBlockPosition": 14,
"innerBlockPositions": null
},
{
"name": "core/heading",
"attributes": {
"level": 3,
"content": "Life is so dynamic"
},
"parentBlockPosition": 14,
"innerBlockPositions": null
},
{
"name": "core/column",
"attributes": {
"width": "66.66%"
},
"parentBlockPosition": 13,
"innerBlockPositions": [
18,
19
]
},
{
"name": "core/paragraph",
"attributes": {
"content": "This rhyming poem is the spark that can reignite the fires within you. It challenges you to go out and live your life in the present moment as a \u201chero\u201d and leave your mark on this world.",
"dropCap": false
},
"parentBlockPosition": 17,
"innerBlockPositions": null
},
{
"name": "core/columns",
"attributes": {
"isStackedOnMobile": true
},
"parentBlockPosition": 17,
"innerBlockPositions": [
20,
22,
23
]
},
{
"name": "core/column",
"attributes": {},
"parentBlockPosition": 19,
"innerBlockPositions": [
21
]
},
{
"name": "core/image",
"attributes": {
"id": 361,
"sizeSlug": "large",
"linkDestination": "none",
"url": "https://gato-graphql.lndo.site/wp-content/uploads/2022/05/graphql-voyager-public-1024x622.jpg",
"alt": ""
},
"parentBlockPosition": 20,
"innerBlockPositions": null
},
{
"name": "core/column",
"attributes": {},
"parentBlockPosition": 19,
"innerBlockPositions": null
},
{
"name": "core/column",
"attributes": {},
"parentBlockPosition": 19,
"innerBlockPositions": [
24
]
},
{
"name": "core/image",
"attributes": {
"id": 362,
"sizeSlug": "large",
"linkDestination": "none",
"url": "https://gato-graphql.lndo.site/wp-content/uploads/2022/05/namespaced-interactive-schema-1024x598.webp",
"alt": ""
},
"parentBlockPosition": 23,
"innerBlockPositions": null
}
]
}
}
}Let op: het attribuut innerBlocks is verdwenen, omdat de blokken niet meer genest zijn. In plaats daarvan bevat de respons twee andere attributen (waarmee je de blokhiërarchie kunt reconstrueren):
parentBlockPosition: de positie van het bovenliggende blok in de geretourneerde array, ofnullals het een blok op het hoogste niveau isinnerBlockPositions: een array met de posities van de inner blocks van het blok in de geretourneerde array
De platgemaakte blokdata-items filteren
Nu de blokhiërarchie is platgemaakt, geeft filteren op core/heading alle blokken van dat type terug (ook als een ervan oorspronkelijk genest was onder een blok dat is uitgesloten).
Deze query:
{
post(by: { id: 1 }) {
id
blockFlattenedDataItems(
filterBy: {
include: [
"core/heading"
]
}
)
}
}...geeft het volgende terug:
{
"data": {
"post": {
"blockFlattenedDataItems": [
{
"name": "core/heading",
"attributes": {
"content": "List Block",
"level": 2
}
},
{
"name": "core/heading",
"attributes": {
"className": "has-top-margin",
"content": "Columns Block",
"level": 2
}
},
{
"name": "core/heading",
"attributes": {
"content": "Columns inside Columns (nested inner blocks)",
"level": 2
}
},
{
"name": "core/heading",
"attributes": {
"fontSize": "large",
"content": "Life is so rich",
"level": 2
}
},
{
"name": "core/heading",
"attributes": {
"level": 3,
"content": "Life is so dynamic"
}
}
]
}
}
}Let op: de twee extra attributen, parentBlockPosition en innerBlockPositions, worden verwijderd bij het filteren, omdat ze dan geen betekenis meer hebben.