Homepage
Link to WPGraphQL on Github
Link to WPGraphQL on WordPress.org

register_graphql_fields

Add one or more fields to a Type in the GraphQL Schema

Parameters

  • $fields (array): Associative array of key/value pairs where the key is the name of the field and the value is a config array for the field.
    • $name (string): The name of the field to add to the Type. “camelCase” recommended.
    • $config (array): The config for the field being registered
      • $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.

Source

File: access-functions.php

Examples

Below are some examples of using the function to extend the GraphQL Schema.

Register Root Fields

This example adds multiple fields to the root of the GraphQL Schema.

Example Query:

Screenshot of the example query and results for fieldOne and fieldTwo
Screenshot of the example query and results for fieldOne and fieldTwo

Register Post Fields

This example shows how to register fields to the “Post” type.

Example Query

Screenshot of example query for multiple test fields registered to the "Post" type
Screenshot of example query for multiple test fields registered to the “Post” type