1. Home
  2. Docs
  3. API Reference
  4. Register GraphQL Object Type

Register GraphQL Object Type

register_graphql_object_type( $type_name, $config );

WPGraphQL comes with a lot of pre-defined Types, but sometimes you need to register custom Types for your Schema. One example might be for complex nested meta.

For example sake, let’s say you had a Real Estate WordPress site and you needed to expose and Address on each post of the Home post_type.

An address is a complex Type, which has fields, such as street, city, state, and zip.

You may have this stored in your postmeta like so:

$address = [
  'street' => '123 4th st.',
  'city' => 'Denver,
  'state' => 'CO',
  'zip' => '80213'
];

update_post_meta( $post->ID, 'address', $address );

Register the Address Type

add_action( 'graphql_register_types', function() {
  register_graphql_object_type( 'Address', [
    'fields' => [
      'street' => [ 'type' => 'String' ],
      'city'   => [ 'type' => 'String' ],
      'state'  => [ 'type' => 'String' ],
      'zip'    => [ 'type' => 'Integer' ],
    ],
  ] );
});

Note:

NIn this case, the shape of the meta matches the shape of the Type we’ve registered, but you have control to make the shape of your Schema be whatever you want. It doesn’t have to match the shape of the data underneath 1-to-1

At this point, the Type is registered in the Schema, but it’s not used anywhere. Before we can query the Address, we need a field that resolves to the Address type.

Register Address Field to the Home Type

Here we’re assuming that we already have a Home type in our Schema to register the address field to.

add_action( 'graphql_register_types', function() {
  register_graphql_field( 'Home', 'address', [
    'type' => 'Address',
    'resolve' => function( $post, $args, $context, $info ) {
       $address = get_post_meta( $post->ID, 'address', true );
       return ! empty( $address ) ? $address : null;
    },
  ]);
});

The first $arg for register_graphql_field is Type, so we use Home as the Type to register the field to. The second $arg is the name of the field. Here we use address as the name. Next, we pass an array to configure the field. We define the type to be Address, and we define a resolve function to return the $address post_meta. 

At this point, you would be able to query the following:

query GET_HOMES_WITH_ADDRESS {
  homes {
    nodes {
      id
      title
      address {
        street
        city
        state
        zip
      }
    }
  }
}

Config Options

register_graphql_object_type( $type_name, array $config );

OptionTypeNotes
descriptionstringPlain-text description of this type for clients (e.g. used by GraphiQL for auto-generated documentation)
fieldsarrayRequired. An array describing object fields or callable returning such an array. See Fields section below for expected structure of each array entry. See also the section on Circular types for an explanation of when to use callable for this option.
interfacesarrayList of interfaces implemented by this type or callable returning such a list. See Interface Types for details. See also the section on Circular typesfor an explanation of when to use callable for this option.
isTypeOfcallablefunction($value, $context, ResolveInfo $info)
Expected to return true if $value qualifies for this type (see section about Abstract Type Resolution for explanation).
resolveFieldcallablefunction($value, $args, $context, ResolveInfo $info)
Given the $value of this type, it is expected to return value for a field defined in $info->fieldName. A good place to define a type-specific strategy for field resolution. See section on Data Fetching for details.
Tags
Was this article helpful to you? Yes 1 No

How can we help?