Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(graphql): remove count query if paginationInfo is not requested #6068

Merged
merged 1 commit into from
Jan 17, 2024

Conversation

xavierleune
Copy link
Contributor

Q A
Branch? current stable version branch for bug fixes
License MIT

This pull request is a draft to fix a bug discovered on the current graphql implementation.

If the following request is sent:

{
  nameMetricFirstnameDepartmentOccurrences {
    collection {
      geoEntityDepartment {
        label
      }
    }
  }
}

The current implementation executes the count query on the database, to populate paginationInfo in the result until this information is dropped by the method ReferenceExecutor::collectAndExecuteSubFields.
I would expect the count query to be executed only if the paginationInfo is requested, like with the following request:

{
  nameMetricFirstnameDepartmentOccurrences {
    paginationInfo {
      itemsPerPage
      lastPage
      totalCount
    }
    collection {
      geoEntityDepartment {
        label
      }
    }
  }
}

I'm not quite sure that this is the best way to implement that fix, I open this pull request to start the discussion. There is probably some other implementations I should look at (like cursor based pagination, perhaps the NormalizeProcessor) and add some tests. In the meantime what do you think of this approach ?

Thanks,

@xavierleune
Copy link
Contributor Author

I think this would fix api-platform/api-platform#2310 api-platform/api-platform#1996 and #4764 (after full implementation)

@soyuka
Copy link
Member

soyuka commented Dec 29, 2023

I like this approach, can you add a test?

@xavierleune xavierleune force-pushed the fix/remove-count-query branch from 6690814 to 03791fe Compare January 3, 2024 16:33
@xavierleune xavierleune force-pushed the fix/remove-count-query branch from 03791fe to f0f873a Compare January 3, 2024 16:43
@xavierleune
Copy link
Contributor Author

@soyuka sure, I think this should do the trick, WDYT ?

@xavierleune
Copy link
Contributor Author

Thanks for your feedback @alanpoulain
As I've now a better understanding of the mechanics here, I think I can go further and offer a better implementation for the partial pagination and solve the bug described in this issue.

I think to control partial pagination in "paged" pagination in graphql, it would make sense to offer a hasNextPage in the paginationInfo. It would be a boolean to indicate if there is more results to the current query (so the client can query the next page).

I think this should be done only with a new interface HasNextPagePaginatorInterface describing only one method: public function hasNextPage(): bool.

So we will have the following paginationInfo available for the client:

paginationInfo {
    totalCount
    lastPage
    itemsPerPage
    hasNextPage
}

Each item may trigger one query to response, only if declared in the client query.

So in the case described in the current issue, no additionnal query would be triggerred:

{
  nameMetricFirstnameDepartmentOccurrences {
    collection {
      geoEntityDepartment {
        label
      }
    }
  }
}

If paginationInfo.totalCount or paginationInfo.lastPage is declared, the count query would be issued. If hasNextPage is declared (and the paginator implements HasNextPagePaginatorInterface) a query to fetch the row +1 would be triggered. A better approach here could be to always fetch itemsPerPage + 1, count the number of rows (and drop the last row if that was an overfetch), but I'm not sure if that's easy or not.

@alanpoulain
Copy link
Member

Sure why not, maybe this interface could be used for REST as well. IMO it should be done in another PR though.

@xavierleune xavierleune force-pushed the fix/remove-count-query branch 2 times, most recently from 3f0e17f to 211a0b5 Compare January 8, 2024 15:02
@xavierleune
Copy link
Contributor Author

Sure why not, maybe this interface could be used for REST as well. IMO it should be done in another PR though.

I'll go with another PR for that part ^^

On the main topic of this pull request, beside the falling test (because of php-parser 5.0.0), I think we're good to go. @alanpoulain may you take a look please ?

@xavierleune xavierleune force-pushed the fix/remove-count-query branch from 211a0b5 to fbdc798 Compare January 10, 2024 10:43
@xavierleune xavierleune force-pushed the fix/remove-count-query branch 2 times, most recently from 53a8989 to 379cf2e Compare January 12, 2024 14:07
@xavierleune
Copy link
Contributor Author

Hi @soyuka @alanpoulain do you have a new feedback on this pull request ? Maybe something I should improve before merging it ? Thanks !

@soyuka
Copy link
Member

soyuka commented Jan 16, 2024

can you rebase against 3.2 so we get proper coverage?

@xavierleune xavierleune force-pushed the fix/remove-count-query branch from 379cf2e to 5144e62 Compare January 17, 2024 08:02
@xavierleune
Copy link
Contributor Author

@soyuka It seems that coverage is ok after rebase 👍

@soyuka soyuka merged commit ef4b261 into api-platform:3.2 Jan 17, 2024
55 of 58 checks passed
@soyuka
Copy link
Member

soyuka commented Jan 17, 2024

Thanks @xavierleune !

@xavierleune xavierleune deleted the fix/remove-count-query branch January 24, 2024 16:48
@xavierleune
Copy link
Contributor Author

@alanpoulain following this pull request, I opened the PR #6120 about the hasNextPagePaginatorInterface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants