Closed ingeniumed closed 10 months ago
@ingeniumed I noticed some errors in a response while testing today. In a post, I have this content:
<!-- wp:paragraph -->
<p>Unnested paragraph.</p>
<!-- /wp:paragraph -->
<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Column 1</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->
<!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Column 2</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->
<!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Column 3</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->
And then I query for the blocks/innerBlocks of that post:
query NewQuery {
post(id: 419, idType: DATABASE_ID) {
blocksData {
blocks {
attributes {
name
value
}
id
name
innerBlocks {
attributes {
name
value
}
id
name
parentId
}
}
}
}
}
The result appears to hold the correct blocks, but also a bunch of Internal server error
in an errors
array:
@alecgeatches So the error itself was due to the fact that we were setting the attributes to an empty object array whenever it was empty. This answer has a good explanation for this, and recommends using ArrayObject
instead. I switched that over, and have added an extra check for the attributes to ensure our graphQL code doesn't run if its empty. That should resolve it.
One thing I did btw - I moved the analytics usage code out of the rest API and into the parser itself. This way in case someone tries to call the method directly, instead of going through the rest api they'll still be counted. The same applies for the graphQL code as well.
Happy to revert this, but figured this was a good time to rectify that.
The test I added is now failing because Relay cannot be found since wp_graphQL is not bundled with the plugin when running the tests. Gonna try and fix this so the test can stay
I have made all the changes that are necessary, and there are 2 pending questions that I commented on. So this should be good to review once more
@chriszarate Thanks a ton for your review! I believe I've addressed all of your suggestions above. Please take another look when you have a chance. Thank you!
I'll merge this into our release branch, and we can cut a release later this week via that. Thanks for the amazing feedback @chriszarate, and @smithjw1.
Description
This PR is meant add a GraphQL API to the Block Data API. It assumes that you have WP-GraphQL installed, and uses a filter to tweak the output of the
ContentParser
to generate the GraphQL response.In addition, it uses the
wp_unique_id
function to generate ids for the blocks so that the blocks can be correlated together. Theinner_blocks
are therefore able to be flattened entirely, thereby bypassing the nested depth problem in graphQL.This is in the draft status to get initial feedback, as well as to add some tests and guard rails around the GraphQL API.
Resolves #49
Steps to Test