Created
April 14, 2021 20:56
-
-
Save sastan/7e8215862cafa562f4f00b260d3cc76c to your computer and use it in GitHub Desktop.
sveltekit + urql (minimal version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script context="module"> | |
import { get, readable } from 'svelte/store' | |
import { createClient, operationStore } from '@urql/svelte' | |
import { browser, dev } from '$app/env' | |
/** | |
* @type {import('@sveltejs/kit').Load} | |
*/ | |
export async function load({ fetch, context }) { | |
const client = await createClient({ | |
// Pass in the fetch from sveltekit to have access to serialized requests during hydration | |
fetch, | |
dev: browser && dev, | |
}) | |
return { | |
context: { | |
...context, | |
client, | |
// Works just like query from @urql/svelte | |
query: async (query, variables, context) => { | |
const store = operationStore(query, variables, context) | |
const result = await client.query(store.query, store.variables, store.context).toPromise() | |
Object.assign(get(store), result) | |
return store | |
}, | |
}, | |
props: { client } | |
} | |
} | |
</script> | |
<script> | |
import { setClient } from '@urql/svelte' | |
/** | |
* @type {import('@urql/svelte').Client} | |
*/ | |
export let client | |
setClient(client) | |
</script> | |
<slot /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script context="module"> | |
export async function load({ context }) { | |
return { | |
props: { | |
todos: await context.query( | |
// String or TypedDocument or ... | |
`query ....`, | |
), | |
}, | |
} | |
} | |
</script> | |
<script> | |
import { query } from '@urql/svelte' | |
/** | |
* @type {import('@urql/svelte').OperationStore} | |
*/ | |
export let todos | |
// Setup subscription for the lifetime of the component | |
query(todos) | |
</script> |
I know why. I'll post an example repo shortly.
It's working very well. Thx a lot.
I will dig into svelte.config.cjs
& package.json
to see what what wrong before.
Thank you again.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx for the
gist
Unfortunately it's not running out of the box. (I'm new to urql, but I think it's the lib to use!!! :))
Few points:
url: 'my graphql endpoint'
increateClient
functiondev
is not a validClientOptions
type, so I commented it.onMount
, but it's still the same thing.Any idea?