Replies: 1 comment
-
I'm working with Svelte Query trying to do a similar thing. To avoid having to While this does appear to "work", it's awful DX, with lots of boilerplate |
Beta Was this translation helpful? Give feedback.
-
I'm working with Svelte Query trying to do a similar thing. To avoid having to While this does appear to "work", it's awful DX, with lots of boilerplate |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm migrating an app from a Sapper codebase that uses Apollo, a GraphQL client.
Most of my GraphQL queries were being fetched in
preload()
using credentialedfetch()
, for nice SSR. Apollo has some nice features like caching and batching requests, but these features could only be used if a client was shared between allpreload()
functions of a route that needed to do some GraphQL1.In order to keep track of per-request Apollo client instances and be able to share them across
preload()
functions, I was relying on thesession
parameter, through a unique id string assigned to it in a middleware2.The problem is that this parameter doesn't exist anymore.
Instantiating an Apollo client in each
load()
function will prevent using the batching requests feature entirely. I don't mind the caching feature in the server-side since there's too little to gain from it there, but I still want these features to be available in the client-side.I tried to generate an id for each request in
src/hooks.server.js
and root+layout.server.js
and forward it to the+layout.js
'sload()
through thedata
parameter, but this parameter is not passed down to furtherload()
functions in the route.+layout.js
'sload()
function could return the Apollo instance id, or even an Apollo instance directly3, but then the otherload()
functions would need toawait parent()
first in order to get the id or the instance, which would prevent them from running concurrently and using the batching requests feature.Is there a way to share data created on the server between
load()
functions without usingawait parent()
?Footnotes
A single global client could never be used because its cache would be shared between requests. And it seems it's not possible to use a client without a cache, so a client pretty much needs to be scoped per-request. ↩
An id was used because the session data needed to be serializable. Apollo client instances were then assigned to a module-scoped object using this id. In the end of each request, the client corresponding to the id in the session was deleted to avoid memory leaks. ↩
The returned value doesn't need to be serializable anymore. ↩
Beta Was this translation helpful? Give feedback.
All reactions