@apollo/client useLazyQuery .then() triggers even when there's an error (React/graphene-django)

Viewed 14

I have a graphene-django backend with a me query that returns an exception: You do not have permission to perform this action if the user is not logged in.

From React I'm calling the query like this:

const [getMe] = useLazyQuery(ME_QUERY); // query not important

React.useEffect(() => {
  getMe()
    .then((meRes) => {
      console.log('Hit the .then()');
      if (meRes.data && meRes.data.me) {
        setUser(meRes.data.me);
      }
    })
    .catch(() => {
      console.log('Hit the .catch()');
    });
}, [])

The response I get from the backend:

{"errors":[{"message":"You do not have permission to perform this action","locations":[{"line":2,"column":3}],"path":["me"]}],"data":{"me":null}}

I would expect that @apollo/client would recognize this as an error and my console would output Hit the .catch(), but for some reason it's not hitting the catch at all and only outputs Hit the .then().

Why would that response resolve instead of reject in @apollo/client? I only have this issue with queries, not with mutations which work as expected. For instance if I call this mutation:

const [createUser, { data, loading, error }] = useMutation(CREATE_USER_MUTATION);

createUser({
  variables: {
    email,
    password1,
    password2,
  },
}).then(() => {
  console.log('this should not be hit');
});

And I receive this response:

{"errors":[{"message":"['This password is too short. It must contain at least 8 characters.', 'This password is too common.']","locations":[{"line":2,"column":3}],"path":["createUser"]}],"data":{"createUser":null}}

@apollo/client will correctly recognize that as an error and this should not be hit is not seen in the console, even though both the query and mutation return a 200 status code.

Any help understanding why useLazyQuery incorrectly hits .then() but useMutation correctly hits .catch() would be greatly appreciated. I can provide more explanation on either the Django or React side if needed.

0 Answers
Related