Bloki (Gutenberg)
Oto jak pobierać dane bloków Gutenberg.
Schemat GraphQL posiada następujące pola dodane do wszystkich typów CustomPost (takich jak Post i Page):
blocksblockDataItemsblockFlattenedDataItems
Te pola nie są włączone, jeśli aktywna jest wtyczka Classic Editor.
blocks
Pole CustomPost.blocks: [BlockUnion!] pobiera listę wszystkich bloków zawartych w custom poście.
blocks zwraca listę typów Block, które zostały zmapowane w schemacie GraphQL. Wszystkie te typy Block są częścią typu BlockUnion i implementują interfejs Block.
Wtyczka implementuje jeden typ Block, GenericBlock, który jest już wystarczający do pobierania danych dowolnego bloku (za pomocą pola attributes: JSONObject).
To query:
{
post(by: { id: 1 }) {
blocks {
...on Block {
name
attributes
innerBlocks {
...on Block {
name
attributes
innerBlocks {
...on Block {
name
attributes
}
}
}
}
}
}
}
}
}...zwróci tę odpowiedź:
{
"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 "hero" 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
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
}
}Schemat GraphQL dla typów Block wygląda następująco:
interface Block {
name: String!
attributes: JSONObject
innerBlocks: [BlockUnion!]
contentSource: HTML!
}
type GenericBlock implements Block {
name: String!
attributes: JSONObject
innerBlocks: [BlockUnion!]
contentSource: HTML!
}
union BlockUnion = GenericBlockPola Block
Interfejs Block (a zatem również typ GeneralBlock) zawiera następujące pola:
namepobiera nazwę bloku:"core/paragraph","core/heading","core/image", itd.attributespobiera obiekt JSON zawierający wszystkie atrybuty bloku.innerBlockspobiera[BlockUnion!], dzięki czemu możemy odpytywać i nawigować po hierarchii bloków zawierających bloki wewnętrzne oraz pobierać dane dla wszystkich z nich, na tylu poziomach w głąb, ile istnieje w naszej treści.contentSourcepobiera źródłowy kod HTML (Gutenberg) bloku, łącznie z ogranicznikami komentarzy zawierającymi atrybuty. Jednak to pole nie pobiera dokładnie tych samych danych, jakie są przechowywane w bazie danych (zob. #2346), dlatego należy korzystać z tego pola ostrożnie.
Bezpośrednie pobieranie GeneralBlock (zamiast BlockUnion)
Ponieważ obecnie istnieje tylko jeden typ Block mapujący bloki — GeneralBlock — sensowne jest, aby CustomPost.blocks (a także Block.innerBlocks) pobierało ten typ bezpośrednio, zamiast typu BlockUnion.
Można to zrobić na stronie Ustawień w zakładce Blocks, zaznaczając opcję Use single type instead of union type?:

Wówczas query GraphQL zostaje uproszczone:
{
post(by: { id: 1 }) {
blocks {
name
attributes
innerBlocks {
name
attributes
}
}
}
}Należy pamiętać, że zachowanie typu odpowiedzi jako BlockUnion jest korzystne dla kompatybilności wstecznej: jeśli kiedykolwiek zdecydujemy się dodać typy specyficzne dla bloków do schematu (zob. sekcję poniżej), nie będzie żadnych niekompatybilnych zmian.
Mapowanie typów specyficznych dla bloków
Typ JSONObject (pobierany przez Block.attributes) nie jest ściśle typowany: jego właściwości mogą mieć dowolny typ i krotność (String, Int, [Boolean!], itd.), dlatego musimy znać te informacje dla każdego bloku i radzić sobie z każdym przypadkiem po stronie klienta.
Jeśli potrzebujemy ścisłego typowania, musimy rozszerzyć schemat GraphQL za pomocą kodu PHP, dodając typy specyficzne dla bloków, które mapują konkretne atrybuty bloku jako pola i włączyć je do BlockUnion.
Na przykład możemy dodać typ CoreParagraphBlock, który mapuje blok core/paragraph, z polem content typu String.
Zapoznaj się z dokumentacją w GatoGraphQL/GatoGraphQL, aby dowiedzieć się, jak rozszerzać schemat GraphQL (currently a work in progress).
Filtrowanie bloków
Pole CustomPost.blocks zawiera argument filterBy z dwiema właściwościami: include i exclude. Możemy ich używać do filtrowania pobieranych bloków według nazwy bloku:
{
post(by: { id: 1 }) {
id
blocks(
filterBy: {
include: [
"core/heading",
"core/gallery"
]
}
) {
name
attributes
}
}
}To zwróci:
{
"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
}
]
}
}
}Należy zwrócić uwagę, że nie wszystkie bloki typu core/heading zostały uwzględnione: te, które są zagnieżdżone pod core/column, zostały wykluczone, ponieważ nie ma możliwości ich osiągnięcia (skoro bloki core/columns i core/column są same w sobie wykluczone).
Niedogodności pola blocks
Pole blocks stwarza niedogodność polegającą na tym, że aby pobrać wszystkie dane bloków zawarte w custom poście, w tym dane bloków wewnętrznych, ich własnych bloków wewnętrznych i tak dalej, musimy wiedzieć, ile poziomów zagnieżdżonych bloków istnieje w treści, i odzwierciedlić tę informację w query GraphQL.
Jeśli nie wiemy, musimy skomponować query z wystarczającą liczbą poziomów, aby mieć pewność, że wszystkie dane zostaną pobrane.
Na przykład poniższe query pobiera do 7 poziomów zagnieżdżenia bloków wewnętrznych:
{
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
Aby uniknąć niedogodności związanej z tym, jak pole blocks pobiera wszystkie dane (w tym dane bloków wewnętrznych, ich bloków wewnętrznych itd.), istnieje pole CustomPost.blockDataItems.
To pole, zamiast zwracać [BlockUnion], zwraca [JSONObject!]:
type CustomPost {
blockDataItems: [JSONObject!]
}Innymi słowy, zamiast podążać typowym podejściem GraphQL polegającym na nawigowaniu między powiązanymi encjami, każda encja Block na najwyższym poziomie już zawiera wszystkie dane bloku dla siebie i wszystkich swoich potomków, w ramach jednego wyniku JSONObject.
Obiekt JSON zawiera właściwości bloku (w pozycjach name i attributes) oraz jego bloków wewnętrznych (w pozycji innerBlocks), rekurencyjnie.
Na przykład następujące query:
{
post(by: { id: 1 }) {
blockDataItems
}
}...zwróci:
{
"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 "hero" 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": ""
}
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
}
}Filtrowanie elementów danych bloków
Podobnie jak w przypadku blocks, blockDataItems również umożliwia filtrowanie pobieranych bloków za pomocą argumentu filterBy.
To query:
{
post(by: { id: 1 }) {
id
blockDataItems(
filterBy: {
include: [
"core/heading"
]
}
)
}
}...zwróci:
{
"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
}
]
}
}
}Należy zwrócić uwagę, że podobnie jak w przypadku blocks, nie wszystkie bloki typu core/heading zostały uwzględnione: te, które są zagnieżdżone pod core/column, zostały wykluczone, ponieważ nie ma możliwości ich osiągnięcia (skoro bloki core/columns i core/column są same w sobie wykluczone).
blockFlattenedDataItems
Oba pola, blocks i blockDataItems, pozwalają filtrować pobierane bloki (za pomocą argumentu filterBy). W obu przypadkach, jeśli blok spełnia warunek włączenia, ale jest zagnieżdżony w bloku, który go nie spełnia, zostanie wykluczony.
Zdarzają się jednak sytuacje, gdy musimy pobrać wszystkie bloki określonego typu z custom posta, niezależnie od tego, gdzie te bloki znajdują się w hierarchii. Na przykład możemy chcieć uwzględnić wszystkie bloki typu core/image, aby pobrać wszystkie obrazy zawarte w poście na blogu.
Aby zaspokoić tę potrzebę, istnieje pole CustomPost.blockFlattenedDataItems. W odróżnieniu od pól blocks i blockDataItems, spłaszcza ono hierarchię bloków do jednego poziomu.
To query:
{
post(by: { id: 1 }) {
blockFlattenedDataItems
}
}...zwróci:
{
"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 "hero" 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
}
]
}
}
}Należy zwrócić uwagę, że atrybut innerBlocks zniknął, ponieważ bloki nie są już zagnieżdżone. W jego miejscu odpowiedź zawiera dwa inne atrybuty (które pozwalają odtworzyć hierarchię bloków):
parentBlockPosition: pozycja bloku nadrzędnego w zwróconym tablicy, lubnulljeśli jest to blok najwyższego poziomuinnerBlockPositions: tablica z pozycjami bloków wewnętrznych bloku w zwróconym tablicy
Filtrowanie spłaszczonych elementów danych bloków
Teraz gdy hierarchia bloków została spłaszczona, filtrowanie według core/heading zwróci wszystkie te bloki (nawet jeśli któryś z nich był pierwotnie zagnieżdżony pod blokiem, który został wykluczony).
To query:
{
post(by: { id: 1 }) {
id
blockFlattenedDataItems(
filterBy: {
include: [
"core/heading"
]
}
)
}
}...zwróci:
{
"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"
}
}
]
}
}
}Należy zwrócić uwagę, że dwa dodatkowe atrybuty, parentBlockPosition i innerBlockPositions, są usuwane podczas filtrowania, gdyż nie mają już sensu.