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

Error handling within MockedProvider #4

Closed
NickTomlin opened this issue Jun 21, 2019 · 7 comments · Fixed by #7
Closed

Error handling within MockedProvider #4

NickTomlin opened this issue Jun 21, 2019 · 7 comments · Fixed by #7

Comments

@NickTomlin
Copy link
Contributor

NickTomlin commented Jun 21, 2019

The current createApolloErrorProvider approach has a separate "throw an error for all requests". This is useful for inspecting the behavior of errors on initial load, but breaks down for me when I have a view that may render some data with a query (like a list of items) and execute another query/mutation on that data.

I'd prefer to be able to control the error handling within my custom resolvers. I've taken a crack at it here. Here is a snippet of usage:

<MockedProvider
  customResolvers={{
    Query: () => ({
      todo: (_obj: any, args: any) => {
        console.log(args.id)
        throw new Error('Boom');
      },
      todos: () => [
        {
          text: 'Success',
        },
      ],
    }),
  }}
>

This would allow a todos query to execute, and then throw an error when todo(id: ID) is executed. Optionally, a resolver can inspect its arguments and conditionally throw an error. This would allow for a more natural way of handling complex error scenarios.

@benawad
Copy link
Owner

benawad commented Jul 20, 2019

I like that approach, I'd be happy to merge a pr for that.

@sarfata
Copy link
Contributor

sarfata commented Aug 2, 2019

@NickTomlin I could use this too. Any reason you have not made a PR yet?

If you do not have time, I could take your fork, check that it builds with recent changes and make the PR. PR ready to merge :)

@NickTomlin
Copy link
Contributor Author

@sarfata thanks for merging! My wife and I recently welcomed a child so I haven't had any time to focus on this 😄

One issue that I did encounter (which was why I hadn't merged my initial work) is that errors thrown in mutation resolvers do not work as expected. By default, they caught by the error handler and also thrown by Apollo. Here's a reproduction (npm t should result in a failing test). More details in apollographql/react-apollo#2614

The "fix" is to either specify an onError policy on the Mutation (which is ugly, because you shouldn't have to do this just for tests) or to set a mutation error policy of all on the client:

const client = new ApolloClient({
  defaultOptions: {
    mutate: {
      errorPolicy: 'all'
    }
  },
});

If you add the defaultOptions snippet to createApolloMockedProvider the test in my reproduction branch should work. It feels a little weird to default the error policy for all the mocked providers. If there's not a better fix we could allow passing a defaultOptions object to createApolloMockedProvider which could override that behavior.

@sarfata
Copy link
Contributor

sarfata commented Aug 19, 2019

Hey @NickTomlin Congrats on the baby! Sorry for the delay here. Just wanted to acknowledge and say that I will get around to doing that and pushing a new PR. Thanks for pointing this out.

@netweng
Copy link
Contributor

netweng commented Mar 30, 2020

i have the problem when throw error in mutation, can this be fixed or i need to fork the repo and add the defaultOptions for errorPolicy.

@benawad
Copy link
Owner

benawad commented Mar 30, 2020

@netweng happy to merge a pr

@lorensr
Copy link
Contributor

lorensr commented Nov 16, 2020

Here's a problem I'm having with this feature: #31

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

Successfully merging a pull request may close this issue.

5 participants