question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

svelte: error when mutation is in function

See original GitHub issue

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 !

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
kittencommented, May 6, 2020

Currently the mutate helper in Svelte runs immediately as we’re 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’re still working on idiomatic Svelte APIs so this one’s 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’m thinking we could just return a closure like in the React API

2reactions
kittencommented, May 6, 2020

I hope I won’t forget, but I’ll come back to you once we’ve got an idea on how to improve this Svelte API 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

svelte: error when mutation is in function · Issue #795 - GitHub
Hi guys, I'm having a hard time trying make a simple login mutation work, here's the code: import { initClient, mutate } from...
Read more >
Mutations | Svelte Query | SvelteStack
error - If the mutation is in an isError state, the error is available via the error property. data - If the mutation...
Read more >
Svelte Query Mutations • REPL • Svelte
1. <script> ; 2. import { QueryClient, useMutation, useQuery } from '@sveltestack/svelte-query@1.1.0'; ; 3. import { mutationOptions, ...
Read more >
Mutations | Svelte Relay - GitHub Pages
To set up a mutation in Svelte Relay, you can use the getMutation() function. This function accepts a GraphQL query, and returns a...
Read more >
Svelte with Apollo GraphQl - Mutation is not getting triggered
async/await async function signInAction() { try { const result = await ... catch (error) { console.log(error); } } // then/catch function ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found