Archives: Filters

  • graphql_connection_query

    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.

    apply_filters( 'graphql_connection_query', mixed $query, AbstractConnectionResolver $resolver );

    Params

    • $query (mixed): Instance of the Query for the resolver
    • $resolver (AbstractConnectionResolver): Instance of the connection resolver
  • graphql_connection_should_execute

    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 whether the connection should execute.

    apply_filters( 'graphql_connection_should_execute', bool $should_execute, AbstractConnectionResolver $resolver );

    Params

    • $should_execute (bool): Whether the connection should execute
    • $resolver (AbstractConnectionResolver): Instance of the connection resolver
  • 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', 'totalCount', [
    'type' => 'Int',
    'description' => __( 'The total count of items in the connection.', 'your-textdomain' ),
    'resolve' => static function( $source, $args, $context, $info ) {
    return $source['totalCount'];
    },
    ] );

    } );

    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_post_object_connection_query_args', function( array $query_args, $source, $args, $context, $info ) {

    $field_selection = $info->getFieldSelection( 2 );

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

    $query_args['no_found_rows'] = false;

    return $query_args;

    }, 10, 5 );

    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_page_info', function( $page_info, $resolver ) {
    $page_info['totalCount'] = $resolver->get_query()->found_posts ?? null;
    return $page_info;
    }, 10, 2 );

    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:

  • graphql_connection_edge

    Create the edge, pass it through a filter.

    apply_filters( 'graphql_connection_edge', array $edge, AbstractConnectionResolver $resolver);

    Params

    • $edge (array): The edge within the connection
    • $resolver (AbstractConnectionResolver): Instance of the connection resolver class
  • graphql_connection_amount_requested

    This filter allows to modify the requested connection page size

    apply_filters( 'graphql_connection_amount_requested', int $amount, AbstractConnectionResolver $resolver );

    Params

    • $amount (int): The requested amount
    • $resolver (AbstractConnectionResolver): Instance of the connection resolver class
  • graphql_connection_max_query_amount

    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.

    apply_filters( 'graphql_connection_max_query_amount', Int $max_amount, mixed $source, array $args, AppContext $context, ResolveInfo $info );

    Params

    • $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 );
  • graphql_connection_query_args

    Get the Query Args. This accepts the input args and maps it to how it should be used in the WP_Query.

    apply_filters( 'graphql_connection_query_args', array $query_args, AbstractConnectionResolver $resolver );

    Params

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

    Generate & Filter the schema.

    apply_filters( 'graphql_type_registry', array $type_registry, WPGraphQL\AppContext $app_context );

    Params

    • $type_registry (array): The TypeRegistry for the API
    • $app_context (WPGraphQL\AppContext): The AppContext object containing all of the information about the context we know at this point
  • graphql_debug_enabled

    Filter to determine if GraphQL Debug is enabled

    apply_filters( 'graphql_debug_enabled', bool $enabled );

    Params

    • $enabled (bool): Whether GraphQL Debug is enabled or not
  • graphql_schema

    Filter the Schema

    apply_filters( 'graphql_schema', WPSchema $schema, SchemaRegistry $schema_registry );

    Params

    • $schema (WPSchema): The generated Schema
    • $schema_registry (SchemaRegistry): The Schema Registry Instance