Gatsby & WordPress - 5.3. - Increasing WPGraphQL Query Limit

by Jack Pritchard

Hey! Just so you know, this article is over 2 years old. Some of the information in it might be outdated, so take it with a grain of salt. I'm not saying it's not worth a read, but don't take everything in it as gospel. If you're curious about something, it never hurts to double-check with a more up-to-date source!

In this video, we will increase the maximum queryable nodes returned by a single GraphQL query in WPGraphQL from 100 items to 300+ items.

The current version of WPGraphQL (0.4.1) limits the number of posts you can query from the `/graphql'endpoint to 100 items.

Even with filter parameters used within a GraphQL query (e.g. first:300), our JSON response will be limited to 100 items.

To get around this issue, we can modify our WordPress theme's functions.php file to increase the GraphQL query limit.

How to do this in a traditional WordPress theme functions.php

#
add_filter( 'graphql_connection_max_query_amount', function( $amount, $source, $args, $context, $info ) {
if ( current_user_can( 'manage_options' ) ) {
$amount = 1000;
}
return $amount;
}, 10, 5 );

https://github.com/wp-graphql/wp-graphql/issues/261

How to do this in a timber WordPress theme functions.php

#
/** Add timber support. */
public function __construct() {
...Other WordPress hooks
add_filter('graphql_connection_max_query_amount', array($this, 'wpgraphql_tweaks'), 10, 5);
parent::__construct();
}

public function wpgraphql_tweaks($amount, $source, $args, $context, $info) {
$amount = 1000;
return $amount;
}