-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add recursive graphql format in blocksDataV2 #72
Add recursive graphql format in blocksDataV2 #72
Conversation
…d full query and reconstruction example
@Zamfi99 Tagging you here as this will affect the changes you made in #66 and likely want to use. The new variable behavior has been moved to See the issue description for the "why" on the modification, but in general it should make the block format shorter, easier to query, and reconstruct. Hopefully this will be easy to integrate into your existing pipeline for consuming post data in GraphQL, but if not the |
@alecgeatches thank you. |
While I agree that specifically requesting attribute data ( What are you suggesting as an alternative? Thank you! |
Although it adheres to GraphQL patterns for querying properties, I believe implementing it too granularly is unnecessary. As you mentioned, all properties are required for the attributes to be useful. Therefore, I propose returning the attributes as an object, where the keys represent the attribute names and the values correspond to the attribute values. Request
Response{
"data": {
"post": {
"blocksDataV2": {
"blocks": [
{
"name": "core/columns",
"id": "QmxvY2tEYXRhVjI6NDY6MQ==",
"parentId": null,
"attributes": {
"isStackedOnMobile": true
}
},
{
"name": "core/column",
"id": "QmxvY2tEYXRhVjI6NDY6Mg==",
"parentId": "QmxvY2tEYXRhVjI6NDY6MQ==",
"attributes": {}
},
{
"name": "core/paragraph",
"id": "QmxvY2tEYXRhVjI6NDY6Mw==",
"parentId": "QmxvY2tEYXRhVjI6NDY6Mg==",
"attributes": {
"content": "Left column",
"dropCap": false
}
},
{
"name": "core/column",
"id": "QmxvY2tEYXRhVjI6NDY6NA==",
"parentId": "QmxvY2tEYXRhVjI6NDY6MQ==",
"attributes": {}
},
{
"name": "core/paragraph",
"id": "QmxvY2tEYXRhVjI6NDY6NQ==",
"parentId": "QmxvY2tEYXRhVjI6NDY6NA==",
"attributes": {
"content": "Right column",
"dropCap": false
}
}
]
}
}
}
} |
Thank you for the detailed example! We considered this originally, but we had the issue that to display a know set of values like "attributes": {
"isStackedOnMobile": true
} we also need to generate a type for that attribute object, e.g. This is how WPGraphQL-Gutenberg does things. It's very powerful, but querying has to be pretty specific per-block:
This is how we made the original decision to use name/value pairs. It can be a clumsy-looking syntax, but we have a single query that works for all block attributes. It takes an extra step to reconstitute those attributes into a key-value object, but removes a lot of overhead during querying. |
Merging this in! Per our discussion above, if we do find a better way to expose attributes we can also add a new property later like |
Description
Recently, data type representations were changed in #66 which modified some odd parsing results when consuming GraphQL:
This behavior has been fixed in the development branch (
planned-release/1.3.0
), but it causes a breaking change to GraphQL attribute data. If other customers rely on the old behavior (e.g.false == ""
), we don't want to unexpectedly change data reprentation. Because of this, we decided to split these changes into a newblocksDataV2
GraphQL variable to avoid breaking any dependencies on the previous behavior.Recursive block structure
This PR also changes the structure of block data, preferring a flattened, recursive structure by default. To show the difference, here was a prior query to get all blocks (and their inner blocks) with full attributes:
There's some repetition -
innerBlocks
is basically the same query asblocks
, but it must be specified separately with aparentId
.attributes
' properties must also be repeated in both sections. Here is the new query:This query will return every block, including inner blocks, as a flattened structure. Each block has a queryable
parentId
and both root and inner blocks are returned in the same structure. Root blocks will have anull
parentId
, and non-root blocks will have a specifiedparentId
. Theflatten
option (defaulttrue
) was also removed, and everyblocksDataV2
query will return flattened data.For an example of the output in the new format, see Example: Simple nested blocks:
core/list
andcore/quote
in the updated README or the block reconstruction example in this branch.Block reconstruction
Another benefit of the new recursive structure is that block reconstruction code has been simplified. Old block reconstruction code was more complex as it needed to handle outer and inner block processing separately due to their different data structures. The new reconstruction code example is shorter and simpler.
Other changes
The changes that were made in #66 to
blocksData
and related tests have been moved toblocksDataV2
and new test files instead. TheblocksData
(v1) property has been modified to include a deprecated notice in the description, but otherwise will remain unchanged for any plugin users that rely on the prior structure or data representation.Steps to Test
Automated tests:
wp-env start
composer install
composer run test
Manual tests:
Install this PR's branch (
add/recursive-graphql-format
) on a WordPress site.Also ensure the WPGraphQL plugin is installed on the same site.
Create a post with nested data, e.g.
In the WordPress admin, go to GraphQL -> GraphQL IDE.
Run this query, replacing
<post-id>
with the relevant post ID from step 4:View the result. The data should contain each block in a flattened list, with
parentIds
corresponding the nested structure of the post: