We are using the XDN to cache graphql requests. Our graphql server returns a 200 response for partial data(this is the default functionality for Apollo), we would really want to avoid caching partial data responses on the XDN. Do you have a recommended way to handle such cases?
One solution might be to add middleware to the Apollo server to alter cache-control headers and add private only for partial responses. Would that be possible? You can also alter the client using middleware to send operation names in the query string and write different routes to cache operations separately.
@mark.brocato can you help me understand how altering cache-control headers and add private only for partial responses would solve this issue?
I don’t think Apollo is intentionally sending a partial response.
How could this example from Layer0 Documentation - Prefetching be modified to validate that the response is complete?
const { Router, CustomCacheKey } = require('@layer0/core/router')
module.exports = new Router().get('/graphql', ({ cache, removeUpstreamResponseHeader, proxy }) => {
cache({
edge: {
maxAgeSeconds: 60 * 60 * 24,
staleWhileRevalidateSeconds: 60 * 60,
},
browser: {
maxAgeSeconds: 0,
serviceWorkerSeconds: 60 * 60 * 24,
},
})
// Some APIs, like Shopify, attempt to establish a session by setting a cookie. Layer0 will
// not cache responses with a set-cookie header, so we remove it before attempting to write
// the response to the cache
removeUpstreamResponseHeader('set-cookie')
// Proxy the request to the "graphql" backend end configured in layer0.config.js
proxy('graphql', { path: '/graphql' })
})
@howie.ross I guess Mark was just referring to this:
https://docs.layer0.co/guides/caching#section_caching_private_responses
So in theory you could implement a middleware in the apollo server which, before returning the response, checks for errors and updates the cache control response header if it is returning a partial response. This would mean that those responses wouldn´t be cached on the edge.
Yes, that’s correct. Basically exclude partial responses from caching.