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 apq with page caches #1317

Merged
merged 26 commits into from
Apr 6, 2024

Conversation

dbosen
Copy link
Contributor

@dbosen dbosen commented Dec 6, 2022

This is aiming to resolve #1316 and resolve #1315.

Why one PR for both issues?

The reason why I want to fix both in one PR is because I started with writing tests for it. And both tests need to be BrowserTestBase tests, because otherwise we would not have requests, that create page cache (at least I did not find a way to write a kernel test).

Since no BrowserTestBase tests are in the module yet, 99% of the PRs would be identical, or they would have to be stacked.

Structure of PR

Most of the PR is just adding the tests, and fixing problems, that are caused by this - changing testing.yml, updating guzzle, fixing new phpstan issues caused by that, and the like.

If you just want to take a look at the actual fixes, then two commits are of interest:

Page Cache Fix

d5e9485

Dynamic Page Cache fix

2e6e282

Copy link
Contributor

@pmelab pmelab left a comment

Choose a reason for hiding this comment

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

Thank you! We just faced exactly the same problem!
I left one comment.

@klausi : do you have any other remarks on this?

Also, is nobody using GraphQL with persisted queries? Thats a pretty obvious problem.

src/EventSubscriber/ApqSubscriber.php Show resolved Hide resolved
Copy link
Contributor

@klausi klausi left a comment

Choose a reason for hiding this comment

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

Thanks a lot, looks really nice! I have some suggestions to make this change a bit lighter.

.github/workflows/testing.yml Outdated Show resolved Hide resolved
phpunit.xml.dist Outdated Show resolved Hide resolved
src/EventSubscriber/ApqSubscriber.php Show resolved Hide resolved
/**
* The base class for all functional GraphQL tests.
*/
abstract class GraphQLFunctionalTestBase extends BrowserTestBase {
Copy link
Contributor

@klausi klausi Apr 13, 2023

Choose a reason for hiding this comment

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

we have a HttpRequestTrait that we use in the kernel tests, can we use that one to test the request/response workflow? Maybe we need to enhance it a bit?

Then we would not need to invent a new functional test category here.

@dbosen
Copy link
Contributor Author

dbosen commented May 30, 2023

Thanks for the review! I am going to take a look at the requested changes.

@klausi
Copy link
Contributor

klausi commented Mar 21, 2024

Thanks, can you answer all the inline comments from above and click "resolve" if they are done in your opinion?

The biggest blocker here is adding the Functional tests, which I would like to avoid if we can.

@dbosen
Copy link
Contributor Author

dbosen commented Apr 2, 2024

I have been able to replace the functional tests. But I also had to implement a different approach to solve the dynamic page cache issue.

The page cache fix is basically the same as before, but the dynamic page cache fix was not really working, as I found out while trying to write a new test for it...

So, I basically turn off dynamic page cache for the graphql requests. I think that is totally fine. Dynamic page cache does not seem to be the right thing for APQ requests anyway.

But I think, @pmelab and @IT-Cru might want to test this new approach in their projects again, to make sure it still works for them.

@dbosen
Copy link
Contributor Author

dbosen commented Apr 3, 2024

The last commit was done by @alexpott, not by me! He found a way to correctly add the cache context for the dynamic page cache.

This looks quite good now I think!

$parameters['variables'] = '{"id": "2"}';
$content = json_encode(['query' => $titleQuery] + $parameters);
$request = Request::create($endpoint, 'POST', [], [], [], ['CONTENT_TYPE' => 'application/json'], $content);
$result = $this->container->get('http_kernel')->handle($request);
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we have to do all this repetitive request building and cannot use $this->query() from HttpRequestTrait?

Opened #1393 as this was also not correct in the previous test case.

if (is_string($query)) {
$computedQueryHash = hash('sha256', $query);
if ($queryHash !== $computedQueryHash) {
throw new Error('Provided sha does not match query');
Copy link
Contributor

Choose a reason for hiding this comment

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

I think Error is not correct here, should be UserError, right? We want to show this error to the graphql client that they sent something wrong.

Not the fault of this pull request, was already wrong before.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interestingly we are asserting in the tests that the error message is coming out in the response, so it appears to be treated already as user error for some reason ... so we can leave this as is and don't care for now.

@klausi
Copy link
Contributor

klausi commented Apr 6, 2024

Thanks a lot! I will merge this now and then simplify the test case a bit to use the ->query() method.

@klausi klausi merged commit 53eb21e into drupal-graphql:8.x-4.x Apr 6, 2024
7 checks passed
@klausi
Copy link
Contributor

klausi commented Apr 6, 2024

While converting the test cases I got super confused, why it fails on GET requests and shows a cached result. Need to find out why in #1394.

@klausi
Copy link
Contributor

klausi commented Apr 8, 2024

Of course it was my fault, not using the result value from the method call.

All good now and will merge the follow-up test improvements soon.

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

Successfully merging this pull request may close these issues.

[4.x] APQ does not work with dynamic page cache [4.x] APQ does not work with page cache
5 participants