-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
Configurable Cache-Control #535
Comments
On the Supabase platform pg_graphql/GraphQL is exposed using a PostgREST RPC function named PostgREST has a mechanism for setting arbitrary response headers. You can read more about it here If you need this functionality, one option would be to
Something like: CREATE OR REPLACE FUNCTION graphql_public.graphql(
"operationName" text DEFAULT NULL::text,
query text DEFAULT NULL::text,
variables jsonb DEFAULT NULL::jsonb,
extensions jsonb DEFAULT NULL::jsonb)
RETURNS jsonb
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
perform set_config(
'response.headers',
'[{"Cache-Control": "public"}, {"Cache-Control": "max-age=259200"}]',
true
);
return graphql.resolve(
query := query,
variables := coalesce(variables, '{}'),
"operationName" := "operationName",
extensions := extensions
);
$BODY$; I appreciate that it won't be as declarative and comprehensive as your proposal but hopefully this can give you a place to drop the logic for the your most critical hot pathes you'd then send your GraphQL queries through your own endpoint at
|
Thanks for your input! We're using pg_graphql via Neon and run our own REST API as an intermediary. I have limited experience with PostgREST, but your proposed solution seems to overlook the different req/res variations unless, as you mentioned, we implement it ourselves - which I'm trying to avoid. I believe all users of the extension would benefit from a more granular cache-control mechanism. Instead of parsing the req/res in middleware or in a pg function to identify sensitive data, the extension could check comments to determine specific cache-control requirements for each field. I’m not sure how extensive your logic is for detecting and setting these headers in Supabase, but it seems error-prone, as each header must be evaluated and set individually. If the extension returned a computed header value, it could be used to set |
While I'm not against this functionality in general, there is no globally accepted standard for returning cache info for GraphQL + the majority of users are operating through PostgREST where there is an escape hatch for setting cache control headers. For those reasons, this is unlikely to get picked up in the timeframe you need it. If you're already running your own intermediary API I'd recommend looking into OTS options for graphql middleware caching |
Summary
I'd like pg_graphql to return information on how the returned data should be cached, with inspiration from how it's been done in Async GraphQL.
Rationale
It would be highly beneficial if you could configure cache-control with the extension so that the consuming server/client doesn't have to implement such logic. This allows for centralized cache control management and ensures consistency across different clients/servers.
Design
This is how I imagine it:
global cache_control
that acts as the default, similar to how you setinflect_names
.table cache_control
.field/computed field cache_control
.function cache_control
.Examples
Global default
Table override
Field override
Computed field & functions override
Req example
Res example
Since the client requested the field password, the default value is overwritten and private, max-age=0 takes precedence. I chose to use the default browser header formatting with a single key-value over a full JSON. This requires a little more work in the extension to parse each string and calculate the final output, but I think it will provide a better experience from the server's perspective as it can just take the value and use that in the response to clients further down the line.
Drawbacks
There will be more logic/code inside the extension.
Alternatives
If this is not provided by the extension itself, then the servers/clients need to parse and evaluate the request/response to decide on how the data can be cached. I've not found a good solution for doing so other than writing my own logic, which doesn't live up to the potential of the fine-grained control described above.
Unresolved Questions
Async Graphql
only support public/private and max-age. Should pg_graphql support the same or more?Thank you for publishing & maintaining this extension!
The text was updated successfully, but these errors were encountered: