Hi guys,
I'm having a hard time trying make a simple login mutation work, here's the code:
import {
initClient,
mutate
} from '@urql/svelte';
initClient({
url: 'https://graphql.fauna.com/graphql',
});
let promise;
let username = '';
let password = '';
const login = () => {
promise = mutate({
query: `
mutation (
$username: String!,
$password: String!
) {
loginUser(input: {
username: $username,
password: $password
})
}
`,
variables: {
username,
password,
}
});
}
urql version & exchanges: v0.2.3
I don't understand why it just launches the mutation once and then it throws the error Function called outside component initialization, the only way to make it work is to do something like $: result = mutation(...) but it doesn't make sense, I don't want to run the mutation after each keystroke.
So please tell me, what I am doing wrong ?
Thank you !
Currently the mutate helper in Svelte runs immediately as we鈥檙e still figuring out patterns.
However, if you call a mutation programmatically you can use getClient() and call client.mutation, like so: https://formidable.com/open-source/urql/docs/concepts/core-package/#one-off-queries-and-mutations
We鈥檙e still working on idiomatic Svelte APIs so this one鈥檚 also on our list to figure out what the best way forward is
Edit: For context, the previous API had a lazy promise. Currently I鈥檓 thinking we could just return a closure like in the React API
Ok ! Forgot to try with that, thank you very much
You guys already did a great job
So now I'm having this error when using client.mutation
visitor.mjs:225 Uncaught Error: Invalid AST Node: { query: "
\n mutation Name(
\n $username: String!,
\n $password: String!
\n ) {
\n loginUser(input: {
\n username: $username,
\n password: $password
\n })
\n }
\n ", variables: { username: "ne", password: "ne" } }.
(Changed it just a little bit, it was all on one line)
Am I doing something wrong again ?
This API is a little different as it鈥檚 on the Client directly.
It accepts the query and variables as separate arguments (first and second respectively)
Also don鈥檛 forget to call toPromise() on the Return value or it won鈥檛 execute :)
Omg, of course ! Thanks again ! You guys are great
I hope I won鈥檛 forget, but I鈥檒l come back to you once we鈥檝e got an idea on how to improve this Svelte API 馃憤
That鈥檚 very kind of you !
This is really a TOP priority for Svelte users IMO (thanks again for your amazing work, guys!).
Example:
<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}
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
I can create a REPL on CodeSandbox if you need, @kitten, no problem.
Can you re-open this until we fix it?
I opened an issue here to better digging into: https://github.com/sveltejs/svelte/issues/4934.
Most helpful comment
I hope I won鈥檛 forget, but I鈥檒l come back to you once we鈥檝e got an idea on how to improve this Svelte API 馃憤