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

Why function for applying query returns void instead of a promise? #7714

Closed
illyakurochkin opened this issue Feb 15, 2021 · 4 comments
Closed

Comments

@illyakurochkin
Copy link
Contributor

When I'm using useLazyQuery, the function for applying query returns void instead of a promise.

const [fetch, {data, loading}] = useLazyQuery();
...
await fetch(); // I can't do that because `fetch` return type is void

Why can't it return a promise, like for mutations?

@hwillson hwillson modified the milestones: Release 3.4, Release 3.5 Mar 30, 2021
@euphocat
Copy link

just to remember it's an old question asked already here: apollographql/react-apollo#3499

@ofrank123
Copy link

In the meantime something like the following could be used:

export const useAsyncQuery = <TData, TVariables = OperationVariables>(
  query: DocumentNode | TypedDocumentNode
): [
  (variables: TVariables) => Promise<ApolloQueryResult<TData>>,
  { loading: boolean }
] => {
  const [loading, setLoading] = useState(false);
  const client = useApolloClient();

  const runQuery = async (variables: TVariables) => {
    setLoading(true);
    const res = await client.query<TData, TVariables>({ query, variables });
    setLoading(false);
    return res;
  };

  return [runQuery, { loading }];
};

@hwillson
Copy link
Member

This was addressed in #8875 - thanks

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants