register_graphql_connection

Given a config array for a connection, this registers a connection by creating all appropriate fields and types for the connection.

register_graphql_connection( array $config );

Parameters

  • $config (array): Configuration for the connection
    • $fromType (string): The name of the Type the connection is coming from
    • $toType (string): The name of the type the connection is going to
    • $fromFieldName (string): The name of the field on the from Type that should resolve to the connection
    • $connectionTypeName (string): The name of the Connection Type. If not provided, the Type name will be derived from the fromType and toType names
    • $resolve (function): The function that is used to resolve the connection
    • $connectionArgs (array): Array of GraphQL input fields that will be added to the connection’s “where” args
    • $connectionFields (array): Array of GraphQL fields to expose as a field of the connection

Source

File: access-functions.php

Examples

The use of register_graphql_connection( $config ) should be happen within the graphql_register_types action, as that’s when GraphQL builds the Schema. Since Connections require a fromType and toType, the connection must be registered after those types have been registered. A lower priority (such as 99) will allow for other Types to be registered before the connection references them.

The below snippet will add a connection to the WPGraphQL Schema, and will use the built-in Post Object Connection Resolver, so it will return data, but it won’t be limited to “the last week” as the connection name implies.

Register the connection

In this example, we define the following:

  • fromType => ‘RootQuery’: This tells GraphQL the connection will be coming from the RootQuery, which means we can query this connection at the root of the graph. This from Type could be any existing Type, and that Type is where the connection can be entered when querying.
  • toType => ‘Post’: This tells GraphQL the connection will resolve to nodes of the Post type. This can be configured to any existing Type in the Schema, but you have to hold up your end of the contract and resolve the connection to the Type promised.
  • fromFieldName => ‘postsFromThisWeek’: This is the field name that is added to the “fromType”. In this case, the RootQuery type gets a field postsFromThisWeek added to it which resolves to the connection.
  • connectionTypeName => ‘PostsFromThisWeekConnection’: If this field is blank, the connection name would be derived from the fromType and toType. In this case, there’s already a RootQueryToPostConnection in the Schema, which we could resolve to, but it’s probably safer to resolve to our own Connection Type so we could customize in a more isolated way down the road. You will now see (by browsing GraphiQL or similar) that our Schema includes a PostsFromThisWeekConnection Type
  • resolve: This is set to a function, like all resolve fields in the Schema. This function is what is executed when the connection is queried. In our case, we return the results of WPGraphQLDataDataSource::resolve_post_objects_connection. (we’ll look at customizing the resolver below).
add_action( 'graphql_register_types', 'register_my_custom_graphql_connection', 99 );

function register_my_custom_graphql_connection() {
  $config = [
    'fromType' => 'RootQuery',
    'toType' => 'Post',
    'fromFieldName' => 'postsFromThisWeek',
    'connectionTypeName' => 'PostsFromThisWeekConnection',
    'resolve' => function( $id, $args, $context, $info ) {
      $resolver   = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $source, $args, $context, $info, $post_type );
      $connection = $resolver->get_connection();

      return $connection;
    },
  ];
  register_graphql_connection( $config );
};

Query the connection

Now that we have a connection registered to our Schema, we can query the following:

{
	postsFromThisWeek(first: 3) {
		nodes {
			id
			title
			date
			link
		}
	}
}

Connection Query Results

And get a result like so (of course, the data is relative to the content in your database):

{
	"data": {
		"postsFromThisWeek": {
			"nodes": [
				{
					"id": "cG9zdDoxMzYz",
					"title": "New Post From This Week",
					"date": "2019-04-04 19:24:22",
					"link": "https://demowpgraphql.local/new-post/"
				},
				{
					"id": "cG9zdDox",
					"title": "Hello world!",
					"date": "2019-01-28 23:50:02",
					"link": "https://demowpgraphql.local/hello-world/"
				},
				{
					"id": "cG9zdDoxMDMx",
					"title": "Tiled Gallery",
					"date": "2013-03-15 17:23:27",
					"link": "https://demowpgraphql.local/tiled-gallery/"
				}
			]
		}
	}
}

The problem with this is that we want to limit our connection to just posts from this week, because the connection is called postsFromThisWeek.

So, let’s look at overriding the connection resolver.

Custom Connection Resolvers

The resolver for the connection above instantiates a new instance of PostObjectConnectionResolver, and asks for the get_connection in response.

We want to modify the args of the underlying WP_Query to limit posts to the last 7 days.

We can do so by using the set_query_arg method on the $resolver, like so:

'resolve' => function( $id, $args, $context, $info ) {
    $resolver   = new WPGraphQLDataConnectionPostObjectConnectionResolver( $id, $args, $context, $info, 'post' );
    $resolver->set_query_arg( 'date_query', [
        'after' => '1 week ago',
    ] );
    $connection = $resolver->get_connection();
    return $connection;
}

Using set_query_arg pass an argument to the underlying WP_Query, with a custom date_query query arg, limiting posts to just the last week.

Now, we can execute the same query:

{
	postsFromThisWeek(first: 3) {
		nodes {
			id
			title
			date
			link
		}
	}
}

And we get results limited to the last 7 days!

{
	"data": {
		"postsFromThisWeek": {
			"nodes": [
				{
					"id": "cG9zdDoxMzYz",
					"title": "New Post From This Week",
					"date": "2019-04-04 19:24:22",
					"link": "https://demowpgraphql.local/new-post/"
				}
			]
		}
	}
}