I'm using Svelte and URQL.
I'm using the svelte example in your packages folder here except I'm creating a format: 'esm'
in my rollup.config.js
.
My Rollup is already code-splitting my final bundle in chunks when I use import('./Component.svelte')
in my code.
What I'm having hard time doing is initializing urql asynchronously, like this:
import { initClient, dedupExchange, fetchExchange } from '@urql/svelte'
import { cacheExchange } from "@urql/exchange-graphcache";
const exchanges = [
cacheExchange({
resolvers: { /*... my resolvers*/ },
schema // I need this async, something like: await import('./schema').then(result => result.default)
}),
fetchExchange
]
export function initURQL () {
initClient({
url: 'http://localhost/graphql',
exchanges
})
}
If I use something like:
export async function initURQL () {
const schema = await import('./schema.json').default
const exchanges = [
cacheExchange({
resolvers: {},
schema
}),
fetchExchange
]
initClient({
url: 'http://localhost/graphql',
exchanges
})
}`
and call it from App.svelte
with ;(async () => await initURQL())()
it gives me error:
Uncaught (in promise) Error: Function called outside component initialization
How can I fix this?
I'm not using URQL but I've run into this error as well.
Unfortunately, you can't call
getContext
orsetContext
(which is whatinitClient
inurql/packages/svelte-urql/src/context.ts
calls) asynchronously.https://svelte.dev/docs#setContext:
https://github.com/timhall/svelte-apollo/issues/9#issuecomment-646184888 answers a very similar question and hopefully points to a suitable workaround for you: