Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to return Promise.reject on plugin? #175

Closed
Laurensvdmaas opened this issue Oct 19, 2022 · 2 comments
Closed

Possible to return Promise.reject on plugin? #175

Laurensvdmaas opened this issue Oct 19, 2022 · 2 comments

Comments

@Laurensvdmaas
Copy link

Hi,

Is there a possibility to return Promise.reject inside a plugin? I've seen some global error handling with sentry but not sure how to reject the promise within the plugin.

I would like to handle errors with:

 try {
    await execute(vars);
  } catch (e) {
    // handle error.
  }

instead of:

const {error} = await execute(vars)
if(error) // handle error

Thank you!

@logaretm
Copy link
Owner

logaretm commented Oct 23, 2022

Possible but not recommended as you might cause value skipping setting the reactive values on the useMutation returns, which could be acceptable if the only way you use useMutation is via the execute function.

So the dirty way is to introduce a custom fetch plugin that throws, something similar to fetch default plugin which you can use as a baseline but make sure to throw the error. I'm not going to offer code guidance there since I don't like recommending this hack.

Another way that I recommend, is to compose useMutation with a custom one:

export function useThrowingMutation(query) {
  const mutation = useMutation(query);

  mutation.execute = function executeWithThrow(...args) {
     const { data, errors } = await execute(...args);
     if (error) {
       throw error;
    }

    return { data };
  }
}

This is simpler and should work well and you can use it for all of your mutations.

Internally this is hard to offer out of the box because villus uses a pipeline of plugins system, so if one crashes it is a big deal and causes integrity concerns to the GraphQL result.

@Laurensvdmaas
Copy link
Author

I understand, thanks for your answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants