|
1 | 1 | # WPGraphQL Content Blocks
|
2 | 2 |
|
| 3 | +## 4.8.0 |
| 4 | + |
| 5 | +### Minor Changes |
| 6 | + |
| 7 | +- 742f18a: # Querying Object-Type Block Attributes in WPGraphQL |
| 8 | + |
| 9 | + ## Overview |
| 10 | + |
| 11 | + With this update, you can now query object-type block attributes with each property individually, provided that the **typed structure** is defined in the class `typed_object_attributes` property or through a **WordPress filter**. |
| 12 | + |
| 13 | + ## How It Works |
| 14 | + |
| 15 | + The `typed_object_attributes` is a **filterable** array that defines the expected **typed structure** for object-type block attributes. |
| 16 | + |
| 17 | + - The **keys** in `typed_object_attributes` correspond to **object attribute names** in the block. |
| 18 | + - Each value is an **associative array**, where: |
| 19 | + - The key represents the **property name** inside the object. |
| 20 | + - The value defines the **WPGraphQL type** (e.g., `string`, `integer`, `object`, etc.). |
| 21 | + - If a block attribute has a specified **typed structure**, only the properties listed within it will be processed. |
| 22 | + |
| 23 | + ## Defining Typed Object Attributes |
| 24 | + |
| 25 | + Typed object attributes can be **defined in two ways**: |
| 26 | + |
| 27 | + ### 1. In a Child Class (`typed_object_attributes` property) |
| 28 | + |
| 29 | + Developers can extend the `Block` class and specify **typed properties** directly: |
| 30 | + |
| 31 | + ```php |
| 32 | + class CustomMovieBlock extends Block { |
| 33 | + /** |
| 34 | + * {@inheritDoc} |
| 35 | + * |
| 36 | + * @var array<string, array<string, "array"|"boolean"|"number"|"integer"|"object"|"rich-text"|"string">> |
| 37 | + */ |
| 38 | + protected array $typed_object_attributes = [ |
| 39 | + 'film' => [ |
| 40 | + 'id' => 'integer', |
| 41 | + 'title' => 'string', |
| 42 | + 'director' => 'string', |
| 43 | + 'soundtrack' => 'object', |
| 44 | + ], |
| 45 | + 'soundtrack' => [ |
| 46 | + 'title' => 'string', |
| 47 | + 'artist' => 'string' |
| 48 | + ], |
| 49 | + ]; |
| 50 | + } |
| 51 | + ``` |
| 52 | + |
| 53 | + ### 2. Via WordPress Filter |
| 54 | + |
| 55 | + You can also define **typed structures dynamically** using a WordPress filter. |
| 56 | + |
| 57 | + ```php |
| 58 | + add_filter( |
| 59 | + 'wpgraphql_content_blocks_object_typing_my-custom-plugin_movie-block', |
| 60 | + function () { |
| 61 | + return [ |
| 62 | + 'film' => [ |
| 63 | + 'id' => 'integer', |
| 64 | + 'title' => 'string', |
| 65 | + 'director' => 'string', |
| 66 | + 'soundtrack' => 'object', |
| 67 | + ], |
| 68 | + 'soundtrack' => [ |
| 69 | + 'title' => 'string', |
| 70 | + 'artist' => 'string' |
| 71 | + ], |
| 72 | + ]; |
| 73 | + } |
| 74 | + ); |
| 75 | + ``` |
| 76 | + |
| 77 | + ## Filter Naming Convention |
| 78 | + |
| 79 | + To apply custom typing via a filter, use the following format: |
| 80 | + |
| 81 | + ``` |
| 82 | + wpgraphql_content_blocks_object_typing_{block-name} |
| 83 | + ``` |
| 84 | + |
| 85 | + - Replace `/` in the block name with `-`. |
| 86 | + - Example: |
| 87 | + - **Block name**: `my-custom-plugin/movie-block` |
| 88 | + - **Filter name**: `wpgraphql_content_blocks_object_typing_my-custom-plugin_movie-block` |
| 89 | + |
| 90 | + ## Example: |
| 91 | + |
| 92 | + ### Example `block.json` Definition |
| 93 | + |
| 94 | + If the block has attributes defined as **objects**, like this: |
| 95 | + |
| 96 | + ```json |
| 97 | + "attributes": { |
| 98 | + "film": { |
| 99 | + "type": "object", |
| 100 | + "default": { |
| 101 | + "id": 1, |
| 102 | + "title": "The Matrix", |
| 103 | + "director": "Director Name" |
| 104 | + } |
| 105 | + }, |
| 106 | + "soundtrack": { |
| 107 | + "type": "object", |
| 108 | + "default": { |
| 109 | + "title": "The Matrix Revolutions...", |
| 110 | + "artist": "Artist Name" |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + ``` |
| 115 | + |
| 116 | + This means: |
| 117 | + |
| 118 | + - The `film` attribute contains `id`, `title`, `director`. |
| 119 | + - The `soundtrack` attribute contains `title` and `artist`. |
| 120 | + |
| 121 | + ## WPGraphQL Query Example |
| 122 | + |
| 123 | + Once the typed object attributes are **defined**, you can query them **individually** in WPGraphQL. |
| 124 | + |
| 125 | + ```graphql |
| 126 | + fragment Movie on MyCustomPluginMovieBlock { |
| 127 | + attributes { |
| 128 | + film { |
| 129 | + id |
| 130 | + title |
| 131 | + director |
| 132 | + soundtrack { |
| 133 | + title |
| 134 | + } |
| 135 | + } |
| 136 | + soundtrack { |
| 137 | + title |
| 138 | + artist |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + query GetAllPostsWhichSupportBlockEditor { |
| 144 | + posts { |
| 145 | + edges { |
| 146 | + node { |
| 147 | + editorBlocks { |
| 148 | + __typename |
| 149 | + name |
| 150 | + ...Movie |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + ``` |
| 157 | + |
3 | 158 | ## 4.7.0
|
4 | 159 |
|
5 | 160 | ### Minor Changes
|
|
0 commit comments