Skip to content

fix: refactor AbstractConnectionResolver::get_nodes() to prevent children double slicing#2294

Merged
jasonbahl merged 2 commits intowp-graphql:developfrom
justlevine:fix/refactor-connection-resolver-get-nodes
Apr 1, 2022
Merged

fix: refactor AbstractConnectionResolver::get_nodes() to prevent children double slicing#2294
jasonbahl merged 2 commits intowp-graphql:developfrom
justlevine:fix/refactor-connection-resolver-get-nodes

Conversation

@justlevine
Copy link
Copy Markdown
Collaborator

What does this implement/fix? Explain your changes.

This PR refactors AbstractConnectionResolver::get_nodes() to use a separate function (AbstractionConnectonResolver::get_ids_for_nodes() for slicing the query ids by the query_amount and offset.

This serves three purposes:

  1. Improves the performance of child get_nodes() methods, since the models are only fetched for the actual nodes to return.
  2. Makes extending AbstractConnectionResolver more DRY, since for most use cases in core, get_nodes() is really only used to change how the offsets are handled.
  3. Fixes issues like [Bug] pageInfo cursors for plugins and themes are not correctly returning arrayconnection:{id} #2087 , since currently parent::get_nodes() slices by query_amount before slicing by offset, which is the entire reason get_nodes() needs to be overwritten to begin with.

Does this close any currently open issues?

#2087

Any other comments?

  • The underlying issue is that some queries require the ids to be sliced by query_amount before slicing by offest, while others need it to happen after. I see no solution to this that isn't a breaking change (if it is possible at all).
  • While this PR is not a breaking change, plugins that overwrite AbstractConnectionResolver::get_nodes() by relying on parent::get_nodes() will still be broken as the latter is still slicing by query_amount before `offset.

Where has this been tested?

Operating System: Ubuntu 20.04 (WSL + Devilbox) + PHP 8.0.15

WordPress Version: 5.9.1

if ( isset( $this->args['after'] ) ) {
$key = array_search( $this->get_offset(), array_keys( $nodes ), true );
$nodes = array_slice( $nodes, $key + 1, null, true );
public function get_ids_for_nodes() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_ids_for_nodes has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.

* @throws Exception
*/
public function get_nodes() {
public function get_ids_for_nodes() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_ids_for_nodes has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.

if ( isset( $this->args['after'] ) ) {
$key = array_search( $this->get_offset(), array_keys( $nodes ), true );
$nodes = array_slice( $nodes, $key + 1, null, true );
public function get_ids_for_nodes() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_ids_for_nodes has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.

if ( isset( $this->args['after'] ) ) {
$key = array_search( $this->get_offset(), array_keys( $nodes ), true );
$nodes = array_slice( $nodes, $key + 1, null, true );
public function get_ids_for_nodes() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_ids_for_nodes has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.

if ( isset( $this->args['after'] ) ) {
$key = array_search( $this->get_offset(), array_keys( $nodes ), true );
$nodes = array_slice( $nodes, $key + 1, null, true );
public function get_ids_for_nodes() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_ids_for_nodes has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.

@qlty-cloud-legacy
Copy link
Copy Markdown

Code Climate has analyzed commit 329268f and detected 10 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 8
Duplication 2

View more on Code Climate.

@justlevine justlevine requested a review from jasonbahl March 11, 2022 03:15
@justlevine justlevine added type: bug Issue that causes incorrect or unexpected behavior needs: reviewer response This needs the attention of a codeowner or maintainer component: connections Relating to GraphQL Connections lang: php Pull requests that update PHP code scope: performance Enhancing speed and efficiency labels Mar 11, 2022
@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.08%) to 79.181% when pulling 329268f on justlevine:fix/refactor-connection-resolver-get-nodes into 70dc88b on wp-graphql:develop.

@justlevine
Copy link
Copy Markdown
Collaborator Author

While the code changes are independent, adding tests require #2293 (to avoid dealing with merge conflicts). Marking this as blocked in the interim.

@justlevine justlevine added the status: blocked Progress halted due to dependencies or issues label Mar 12, 2022
@jasonbahl jasonbahl merged commit 58ecca1 into wp-graphql:develop Apr 1, 2022
jasonbahl added a commit to justlevine/wp-graphql that referenced this pull request Apr 1, 2022
@jasonbahl jasonbahl mentioned this pull request Apr 6, 2022
@justlevine justlevine deleted the fix/refactor-connection-resolver-get-nodes branch May 17, 2022 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: connections Relating to GraphQL Connections lang: php Pull requests that update PHP code needs: reviewer response This needs the attention of a codeowner or maintainer scope: performance Enhancing speed and efficiency status: blocked Progress halted due to dependencies or issues type: bug Issue that causes incorrect or unexpected behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] pageInfo cursors for plugins and themes are not correctly returning arrayconnection:{id}

3 participants