Extending GraphQL
Adding Fields
New fields can be added to the GraphQL schema, on any object type. This can be used to register a field on a post type object, the global media type, or even an ACF field group. When you need to add a new field, you can find the object type name using the GraphQL IDE.
Add a field to a Type in the GraphQL Schema:
register_graphql_field(string $type_name, string $field_name, array $config);$type_name(string): The name of the GraphQL Type in the Schema to register the field to$field_name(string): The name of the field. Should be unique to the Type the field is being registered to.$config(array): Configuration for the field$type(string | array): The name of the GraphQL Type the field will return. The resolve function must return this type. For non-null fields:'type' => [ 'non_null' => 'TypeName' ]For listOf fields:'type' => [ 'list_of' => 'TypeName' ]$description(string): Description of the field. This will be used to self-document the schema and should describe to clients how the field should be used.$resolve(function): Function that will execute when the field is asked for in a query.
Adding Object Types
When adding a new field that should contain subfields, you’ll also want to register an object type.
Last updated on