sapper
sapper copied to clipboard
Application initialization - making fetch available outside preload
Is your feature request related to a problem? Please describe.
Apollo client needs to be given the fetch function during setup of the client while setting up the application. The fetch method still isn't actually called until executing the client during preload
Today there's no way to do that and so people are coming up with workarounds https://github.com/timhall/svelte-apollo/issues/9:
- Using isomorphic-fetch, isomorphic-unfetch, or cross-fetch in order to provide their own
fetchfunction bypassing Sapper's provided functionality. But this won't pass headers (i.e. cookies and auth headers from the original request) and so requires a fair amount of work to make that actually work - Just using
onMountand skipping SSR altogether
You can't create the Apollo client in layout's preload where this.fetch is available because then it will only get created on the server during initial page load and when you navigate to another page it won't have been created.
Describe the solution you'd like
I'm thinking it might be good to have some kind of init method that gets called at the beginning of the application on both the client and server in order to setup things like data fetchers and takes an object containing fetch as a parameter
Describe alternatives you've considered
I will probably try to use cross-fetch and not use Sapper's fetch. It's a bit of a Rube Goldberg making it all fit together though. I haven't figured out how I'll pass along cookies and auth headers on the server from my original request to the API server
How important is this feature to you? There are probably workarounds. I've really been tearing my hair out trying to figure out how to get this wired up though.
GraphQL is extremely popular. It'd be nice if Sapper had better support for utilizing GraphQL clients.
Additional context In addition to this issue, users also need to create per-request context on the server to hold the client. It took me quite a lot of investigation to find a solution seems doable. (https://github.com/sveltejs/sapper/issues/751#issuecomment-516882258)
Any news on this?