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


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


  • $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


File: access-functions.php


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).

Query the connection

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

Connection Query Results

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

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:

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:

And we get results limited to the last 7 days!