I'm trying the amazing works done by https://github.com/FormidableLabs/urql/tree/master/packages/svelte-urql guys.
Everything works good until today issue.
I'm using the below code and it gives me this error:
Error: Function called outside component initialization
at get_current_component (index.mjs:615)
at getContext (index.mjs:648)
at getClient (urql-svelte.mjs:55)
at query (urql-svelte.mjs:81)
at Players.svelte:41
Code:
<script>
import { query } from '@urql/svelte'
import { myQuery } from './myQuery'
let players
let myVars
function sleep (ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
$: (async () => {
await sleep(2000) // this gives me the error; removing it make it work
players = query({
query: myQuery,
variables: { ...myVars },
requestPolicy: 'cache-and-network'
})
})()
</script>
{#if !players}
Loading players...
{:else}
Players loaded! Do the work.
{/if}
Can you suggest what's the problem?
If I use await()
in onMount()
it works! Like this:
onMount(async () => {
await sleep(2000)
loaded = true
})
Here the code for @urql/svelte
:
- query.ts: https://github.com/FormidableLabs/urql/blob/master/packages/svelte-urql/src/operations/query.ts
- context.ts: https://github.com/FormidableLabs/urql/blob/master/packages/svelte-urql/src/context.ts
Maybe the context
code?
import { setContext, getContext } from 'svelte';
import { Client, ClientOptions } from '@urql/core';
const CLIENT = '$$_URQL';
export const getClient = (): Client => getContext(CLIENT);
export const setClient = (client: Client): void => {
setContext(CLIENT, client);
};
export const initClient = (args: ClientOptions): Client => {
const client = new Client(args);
setClient(client);
return client;
};
I can create a REPL on CodeSandbox if you need, no problem.
Bug on @urql/svelte
: https://github.com/FormidableLabs/urql/issues/795.
Information about your Svelte project: - Chrome 83 - Svelte version: 3.23.0 - Rollup