1

I'm using GraphQL code generator to create typescript types of my schema entities. When I use useQuery like below there is no problem;

const { loading, error, data } = useQuery<
    FavoritesQuery,
    FavoritesQueryVariables
  >({
    fetchPolicy: 'no-cache',
  });

However, when I try lazy query like below, typescript compiler suffers;

const [loadFavorites, { called, loading, data }] = useLazyQuery<
    FavoritesQuery,
    FavoritesQueryVariables
  >(Queries.FAVORITES_QUERY, {
    fetchPolicy: 'no-cache',
  });

Intellisense finds fetchPolicy and its value list while typing. But compiler generates below error;

Argument of type '{ fetchPolicy: string; }' is not assignable to parameter of type 'LazyQueryHookOptions<{ __typename?: "MainQuery"; } & { favorites: ({ __typename?: "FavoriteGType"; } & Pick<FavoriteGType, "id" | "name" | "category" | "description" | "commandText" | "lastUpdatedDate"> & { ...; })[]; }, FavoritesQueryVariables>'.
  Object literal may only specify known properties, and 'fetchPolicy' does not exist in type 'LazyQueryHookOptions<{ __typename?: "MainQuery"; } & { favorites: ({ __typename?: "FavoriteGType"; } & Pick<FavoriteGType, "id" | "name" | "category" | "description" | "commandText" | "lastUpdatedDate"> & { ...; })[]; }, FavoritesQueryVariables>'.  TS2345

    89 |     FavoritesQueryVariables
    90 |   >(Queries.FAVORITES_QUERY, {
  > 91 |     fetchPolicy: 'no-cache',
       |     ^
    92 |   });

How should I use fetchPolicy in useLazyQuery?

Thanks & regards

1 Answer 1

2

React Apollo 3 uses Typescript's Omit functionality, which was added in Typescript 3.5. I have a feeling you're using an older version of Typescript; if you update to TS >= 3.5, this problem should go away.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.