graphql_connection_page_info

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.

apply_filters( 'graphql_connection_page_info', array $page_info, AbstractConnectionResolver $resolver );

Params

  • $page_info (array): Array containing page info
  • $resolver (AbstractConnectionResolver): Instance of the connection resolver

Example

You would want to register a “total” field to the PageInfo type, like so:

add_action( 'graphql_register_types', function() {

	register_graphql_field( 'PageInfo', 'total', [
		'type' => 'Int',
		'description' => __( 'The total number of records found for the connection', 'wp-graphql' ),
	]);

} );

Then, you would want to filter the connection query args to tell the underlying WP_Query to ask to count the total posts.

add_filter( 'graphql_connection_query_args', function( $query_args, \WPGraphQL\Data\Connection\AbstractConnectionResolver $connection, $input_args ) {

	$field_selection = $connection->getInfo()->getFieldSelection( 2 );

	if ( ! isset( $field_selection['pageInfo']['total'] ) ) {
		return $query_args;
	}

	if ( $connection->get_query() instanceof \WP_Query ) {
		$query_args['no_found_rows'] = false;
	}

	return  $query_args;

}, 10, 3 );

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:

add_filter( 'graphql_connection_query_args', function( $query_args, \WPGraphQL\Data\Connection\AbstractConnectionResolver $connection, $input_args ) {

	if ( ! $connection->get_query() instanceof \WP_Query ) {
		return $query_args;
	}

	$field_selection = $connection->getInfo()->getFieldSelection( 2 );

	if ( isset( $field_selection['pageInfo']['total'] ) ) {
		$query_args['no_found_rows'] = false;
	}

	return  $query_args;

}, 10, 3 );

This will tell WP_Query to ask SQL to calculate the total rows by passing the no_found_rows => false argument.

Now, we can query connections like so, getting the total count: