Skip to content

Overriding/subclassing the default object connection types #2678

@maurocolella

Description

@maurocolella

What problem does this address?

Hi.

I have a situation where we are using paginated queries, but need to apply complex access control logic.

This section from PostObjectConnectionResolver illustrates the issue:

  /**
   * {@inheritDoc}
   */
  public function get_ids_from_query()
  {
    $ids = !empty($this->query->posts) ? $this->query->posts : [];

    // FIXME: this is where we should filter entries according to security rules
    foreach($ids as $id) {

    }

    // If we're going backwards, we need to reverse the array.
    if (!empty($this->args['last'])) {
      $ids = array_reverse($ids);
    }

    return $ids;
  }

Existing filters let us either filter the WP_Query upstream (but our logic can't be expressed as a meta query), or the returned ids after cursors have been applied to them.

Currently, the AbstractConnectionResolver applies cursors immediately to the output of the above function.

What is your proposed solution?

A filter after ids have been retrieved, but before cursors are applied to them.

What alternatives have you considered?

I have tried to override the PostObjectConnectionResolver:

register_graphql_connection(
  [
    'fromType'       => 'RootQuery',
    'toType'         => ucfirst($post_type->graphql_single_name),
    'queryClass'     => 'WP_Query',
    'fromFieldName'  => $post_type->graphql_plural_name,
    'connectionArgs' => PostObjects::get_connection_args(),
    'resolve'        => function ($root, $args, $context, $info) use ($post_type) {
      $resolver = new \WPGraphQL\Data\Connection\CustomPostObjectConnectionResolver($root, $args, $context, $info, $post_type);

      return $resolver->get_connection();
    },
  ]
);

In:

graphql_register_types

But obviously, this results in a complaint about duplicate types.

Is something like this possible currently?

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions