Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Application initialization - making fetch available outside preload #1243

Open
benmccann opened this issue May 28, 2020 · 1 comment
Open
Labels

Comments

@benmccann
Copy link
Member

benmccann commented May 28, 2020

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 timhall/svelte-apollo#9:

  • Using isomorphic-fetch, isomorphic-unfetch, or cross-fetch in order to provide their own fetch function 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 onMount and 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. (#751 (comment))

@Drevoed
Copy link

Drevoed commented Nov 22, 2020

Any news on this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants