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

In version 3.5.8 or later, useLazyQuery cannot get errors information when using promise #9477

Closed
eretica opened this issue Mar 2, 2022 · 3 comments

Comments

@eretica
Copy link

eretica commented Mar 2, 2022

In version 3.5.7, errors were passed in the then clause of Promise, but since version 3.5.8, errors are no longer passed in the then clause.

Intended outcome:

That we can get information about errors even in patterns that use promises.

Actual outcome:

useLazyQuery promise does not contain errors information

How to reproduce the issue:

import { gql, InMemoryCache } from "@apollo/client";
import { GraphQLErrors } from "@apollo/client/errors";
import { useLazyQuery } from "@apollo/client/react/hooks";
import { MockedProvider } from "@apollo/client/testing";
import { renderHook } from "@testing-library/react-hooks";

describe("apollo", () => {
  it("has errors", async () => {
    const mockQuery = gql`
      query dummyQuery($page: Int!) {
        repos(page: $page) {
          list {
            id
          }
        }
      }
    `;

    const { result } = renderHook(() => useLazyQuery(mockQuery), {
      wrapper: ({ children, ...rest }) => (
        <MockedProvider
          cache={new InMemoryCache()}
          defaultOptions={{
            watchQuery: {
              fetchPolicy: "no-cache",
              errorPolicy: "all"
            },
            query: {
              fetchPolicy: "no-cache",
              errorPolicy: "all"
            },
            mutate: {
              fetchPolicy: "no-cache",
              errorPolicy: "all"
            }
          }}
          {...rest}
        >
          <div>{children}</div>
        </MockedProvider>
      ),
      initialProps: {
        mocks: [
          {
            request: {
              query: mockQuery,
              variables: { page: 1 }
            },
            result: {
              data: { list: null },
              errors: [{ extensions: { code: "im error" } }] as unknown as GraphQLErrors
            }
          }
        ]
      }
    });

    const [fetchQuery] = result.current;

    await fetchQuery({ variables: { page: 1 } }).then(data => {
      expect(data.data).toStrictEqual({ list: null });
      expect(data.errors).toStrictEqual([{ extensions: { code: "im error" } }]);

      // version 3.5.7 is having there is error and errors response
      /**
       * {
       * graphQLErrors: [{ extensions: { code: "im error" } }],
       * clientErrors: [],
       * networkError: null,
       * message: 'undefined',
       * extraInfo: undefined,
       * }
       **/
      data.error;

      // [{ extensions: { code: "im error" } }]
      data.errors;
    });
  });
});

Versions

  System:
    OS: macOS 11.4
  Binaries:
    Node: 16.11.0 - ~/.asdf/installs/nodejs/16.11.0/bin/node
    Yarn: 1.19.2 - /usr/local/bin/yarn
    npm: 8.0.0 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Chrome: 98.0.4758.109
    Firefox: 88.0.1
    Safari: 14.1.1
  npmPackages:
    @apollo/client: 3.5.10 => 3.5.10
    storybook-addon-apollo-client: ~4.0.0 => 4.0.11
@eretica eretica changed the title In version 3.7.8 or later, useLazyQuery cannot get errors information when using promise In version 3.5.8 or later, useLazyQuery cannot get errors information when using promise Mar 2, 2022
@ZayZAy313
Copy link

import { gql, InMemoryCache } from "@apollo/client";
import { GraphQLErrors } from "@apollo/client/errors";
import { useLazyQuery } from "@apollo/client/react/hooks";
import { MockedProvider } from "@apollo/client/testing";
import { renderHook } from "@testing-library/react-hooks";

describe("apollo", () => {
it("has errors", async () => {
const mockQuery = gqlquery dummyQuery($page: Int!) { repos(page: $page) { list { id } } };

const { result } = renderHook(() => useLazyQuery(mockQuery), {
  wrapper: ({ children, ...rest }) => (
    <MockedProvider
      cache={new InMemoryCache()}
      defaultOptions={{
        watchQuery: {
          fetchPolicy: "no-cache",
          errorPolicy: "all"
        },
        query: {
          fetchPolicy: "no-cache",
          errorPolicy: "all"
        },
        mutate: {
          fetchPolicy: "no-cache",
          errorPolicy: "all"
        }
      }}
      {...rest}
    >
      <div>{children}</div>
    </MockedProvider>
  ),
  initialProps: {
    mocks: [
      {
        request: {
          query: mockQuery,
          variables: { page: 1 }
        },
        result: {
          data: { list: null },
          errors: [{ extensions: { code: "im error" } }] as unknown as GraphQLErrors
        }
      }
    ]
  }
});

const [fetchQuery] = result.current;

await fetchQuery({ variables: { page: 1 } }).then(data => {
  expect(data.data).toStrictEqual({ list: null });
  expect(data.errors).toStrictEqual([{ extensions: { code: "im error" } }]);

  // version 3.5.7 is having there is error and errors response
  /**
   * {
   * graphQLErrors: [{ extensions: { code: "im error" } }],
   * clientErrors: [],
   * networkError: null,
   * message: 'undefined',
   * extraInfo: undefined,
   * }
   **/
  data.error;

  // [{ extensions: { code: "im error" } }]
  data.errors;
});

});
});

Versions

System:
OS: macOS 11.4
Binaries:
Node: 16.11.0 - ~/.asdf/installs/nodejs/16.11.0/bin/node
Yarn: 1.19.2 - /usr/local/bin/yarn
npm: 8.0.0 - ~/.asdf/plugins/nodejs/shims/npm
Browsers:
Chrome: 98.0.4758.109
Firefox: 88.0.1
Safari: 14.1.1
npmPackages:
@apollo/client: 3.5.10 => 3.5.10
storybook-addon-apollo-client: ~4.0.0 => 4.0.11

@eretica
Copy link
Author

eretica commented Apr 15, 2022

@brainkim hi

Previously, useLazyQuery's promise returned error information.
Currently, useLazyQuery's promise does not return error information.

Is this a specification?

This behavior has been fixed in this PR #9328

thanks

@eretica
Copy link
Author

eretica commented Apr 28, 2022

resolved by version 3.6.0

thanks!

@eretica eretica closed this as completed Apr 28, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants