drupal-graphql / graphql

GraphQL integration for Drupal 9/10
286 stars 202 forks source link

[4.x] APQ does not work with page cache #1315

Closed dbosen closed 5 months ago

dbosen commented 1 year ago

Problem/Motivation

When automatic persisted queries are enabled and the page cache module is enabled as well, automatic persisted queries do not work without sending the query with every request.

Automatic persisted queries first try to query with the query hash only. This will lead to a PersistentQueryNotFound response, when that specific query was requested for the first time. This is perfectly normal, and the client will now that he has to add the actual query in the next request, this will map the query with the hash and subsequent request should work without the hash only.

But when page cache is enabled, the PersistentQueryNotFound response is cached and does not get invalidated. So , the next time the client does the normal query it will continue to return the PersistentQueryNotFound and do the full query again, which is defeating the purpose of persisted queries.

Steps to reproduce

Now you should be able to do simple query like {__typename} to your endpoint.

A proper client should seems to work, because he will always do the second query with the full query after the PersistentQueryNotFound response. If you do it manually in postman or just your Web browser, you will see, that you will always get get the PersistentQueryNotFound when trying to execute the persistent querie.

Proposed resolution

Either we add a cache flushing mechanism, when the persistant query is stored into the system, or do not have to cache the PersistentQueryNotFound response at all. I think the latter is simpler and more robust. The PersistentQueryNotFound is incorrent milliseconds after it has been created, so it would not be necessary to cache it anyway.