pristas-peter / wp-graphql-gutenberg

Query gutenberg blocks with wp-graphql
https://wp-graphql-gutenberg.netlify.app
GNU General Public License v3.0
299 stars 62 forks source link

Adds generic way to get block attributes #64

Open cbou opened 4 years ago

cbou commented 4 years ago

According to the doc, you always needs to need the type of a block in order to get the attributes of this block. This could lead to a ton of codes in order to get all attributes.

# ...
# blocks field represents array of Block
blocks {
    # fields from the interface
    clientId
    isDynamic
    ... on CoreParagraphBlock {
        attributes {
            ... on CoreParagraphBlockAttributes {
                content
            }
        }
    }
}
# ...

The project Quartz/wp-graphql-content-blocks has another approach:

attributes {
    name
    value
}
"attributes": [
    {
        "name": "src",
        "value": "https://example.com/fresh-prince.jpeg"
    },
    {
        "name": "alt",
        "value": "The Fresh Prince of Bel Air"
    }
]

Is it something that could be implemented in this plugin?

ojohnny commented 3 years ago

Since 251cf88 you can get all attributes for a block via the attributesJSON field (added because blocksJSON already existed, and I thought it made sense to have a similar thing for attributes). You would need to parse this JSON client-side, but I think it side-steps the problem that value in the proposed solution would either need to be able to hold all possible attribute types (schema nightmare?) or becoming serialized to a common type, such as a string.

@cbou Is attributesJSON sufficient for your use case?