Gatsby & WordPress - 5.3. - Increasing WPGraphQL Query Limit

by Jack Pritchard

Hey! This post's a little over two years old now, so some of what's here might be out of date. Still worth a read, but if anything really matters to you, it's worth double-checking against a newer source too.

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.

This reflects WPGraphQL 0.4.1 from 2019 — the plugin has had many major releases since, so the filter name and default behaviour here may not match current versions. Check the current WPGraphQL docs before relying on this.

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;
}