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

useLazyQuery won't update execute function when gql changes #10198

Closed
oychao opened this issue Oct 17, 2022 · 3 comments · Fixed by #10698
Closed

useLazyQuery won't update execute function when gql changes #10198

oychao opened this issue Oct 17, 2022 · 3 comments · Fixed by #10698

Comments

@oychao
Copy link

oychao commented Oct 17, 2022

// ...
import {
  gql,
  useLazyQuery,
} from '@apollo/client';

export function useMyBusinessRequest () {
  const [dynamicGqlTemplate, setDynamicGqlTemplate] = useState('some gql');
  const [request, model] = useLazyQuery(gql(dynamicGqlTemplate));

  return { setDynamicGqlTemplate, request, model, };
}

request won't get updated when I call setDynamicGqlTemplate.

It seems request is decorated with useCallback, is this design deliberate?

Intended outcome:

request get updated when dynamic gql template changes.

Actual outcome:

Here is the implementation of useLazyQuery.

  const execute = useCallback<
    LazyQueryResultTuple<TData, TVariables>[0]
  >(executeOptions => {
    execOptionsRef.current = executeOptions ? {
      ...executeOptions,
      fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy,
    } : {
      fetchPolicy: initialFetchPolicy,
    };

    const promise = internalState
      .asyncUpdate() // Like internalState.forceUpdate, but returns a Promise.
      .then(queryResult => Object.assign(queryResult, eagerMethods));

    // Because the return value of `useLazyQuery` is usually floated, we need
    // to catch the promise to prevent unhandled rejections.
    promise.catch(() => {});

    return promise;
  }, []);

see useLazyQuery.ts#74

How to reproduce the issue:

see above.

Versions

3.7.0

@oychao
Copy link
Author

oychao commented Oct 17, 2022

I detected the problem, internalState in execute is stale.

Is apollo/client going to update this implementation in future releases?

@oychao oychao changed the title useLazyQuery won't update request when gql changes useLazyQuery won't update execute function when gql changes Oct 18, 2022
@bignimbus bignimbus added 🔍 investigate Investigate further 🏓 awaiting-team-response requires input from the apollo team labels Oct 21, 2022
@jerelmiller jerelmiller self-assigned this Mar 31, 2023
@jerelmiller jerelmiller added 🐞 bug ⚛️ React-related and removed 🏓 awaiting-team-response requires input from the apollo team 🔍 investigate Investigate further labels Mar 31, 2023
@jerelmiller
Copy link
Member

Hey @oychao 👋

We just released v3.7.11 which now allows you to use dynamic queries. The execute function will now use the most recently passed query document to the hook.

@github-actions
Copy link
Contributor

github-actions bot commented May 1, 2023

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.