Set the query for the resolver, for use as reference in filters, etc
Filter the query. For core data, the query is typically an instance of:
WP_Query
WP_Comment_Query
WP_User_Query
WP_Term_Query
…
But in some cases, the actual mechanism for querying data should be overridden. For example, perhaps you’re using ElasticSearch or Solr (hypothetical) and want to offload the query to that instead of a native WP_Query class. You could override this with a query to that datasource instead.
Check if the connection should execute. If conditions are met that should prevent the execution, we can bail from resolving early, before the query is executed.
Filter the pageInfo that is returned to the connection.
This filter allows for additional fields to be filtered into the pageInfo of a connection, such as “totalCount”, etc, because the filter has enough context of the query, args, request, etc to be able to calcuate and return that information.
You would also need to do the same for the other WordPress query classes. WP_Query uses no_found_rows => false to tell SQL to count the total, but WP_Term_Query and
Then filter the pageInfo to return the total for the query, something to this tune:
This uses the results of the query to return the totalCount. Again, this would need to be more advanced to account for queries that return the data differently. WP_Query returns the data as found_posts where other query classes might return differently.
Now, we can query connections like so, getting the total count:
Filter the maximum number of posts per page that should be queried. The default is 100 to prevent queries from being exceedingly resource intensive, however individual systems can override this for their specific needs. This filter is intentionally applied AFTER the query_args filter.
$max_amount (int): Maximum number of items to return for the connection. Too high of a number can cause performance bottlenecks on the server. It’s recommended to keep this lower and encourage clients to paginate requests to get more items.
$source (mixed): Source passed down from the resolve tree
$args (array): Array of arguments input in the field as part of the GraphQL query
$context (AppContext): Object containing app context that gets passed down the resolve tree
$info (ResolveInfo): Info about fields passed down the resolve tree
Examples
Filter the max query amount for a specific connection based on field name
This example shows how to filter the max query amount for a specific field name in the Schema. In this case, it filters the connection from the field named productCategories to have a max amount of 150, instead of the default 100.
/*
* Increase perPage for product categories. This is needed to build out the sidebar accordion.
*/
add_filter( 'graphql_connection_max_query_amount', function ( int $max_amount, $source, array $args, $context, $info ) {
// Bail if the fieldName isn't avail
if ( empty( $info->fieldName ) ) {
return $max_amount;
}
// Bail if we're not dealing with our target fieldName
if ( 'productCategories' !== $info->fieldName ) {
return $max_amount;
}
return 150;
}, 10, 5 );
$query_args (array): The query args to be used with the executable query to get data. This should take in the GraphQL args and return args for use in fetching the data.
$resolver (AbstractConnectionResolver): Instance of the connection resolver