-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
skip option fails when assigned a variable that flips from false to true #6670
Comments
I traced all the way into the fetchQueryByPolicy function introduced in #6221. I believe it has something to do with the skip option not being passed to the apollo-client/src/core/QueryManager.ts Lines 999 to 1005 in 2de0e23
I tried locally modifying this but even when passing |
@benjamn I'd like to learn more about the inner-workings of ApolloClient, and would be happy to try and fix this but I'm not sure what I'm looking for. I know the options are differing here, with apollo-client/src/react/data/QueryData.ts Lines 241 to 255 in 7993a77
which calls to update the "current observable" on the query data class. I'm guessing that for some reason the thing that makes fetch/no-fetch decisions is not aware of this update. |
I'm having this same problem. |
Is this a duplicate of apollographql/react-apollo#3492 (comment)? Also related: #6342 |
Maybe? I wanted to create an issue with very simple/specific repro. |
Okay so
|
i'm surprised the major release (out of beta) was done with this still broken (since it was a known thing not a bug that popped after release).. it's a pretty widely used feature and I was a little shocked after I migrated to find the console error and this thread .. would of been nice if it was a known thing to make mention of it in the release/migration guide .. maybe its just me though? |
Funnily enough we migrated back to beta-43. It's more stable as of the moment of writing |
@3nvi is there any other things I should keep an eye on based on your comment sounds like there might be other unmentioned issues? .. we've been QA'ing the migration to 3.0.2 but a heads up if you know of anything specific would be great! |
related to #6572 |
If anyone needs a work around, this is what I came up with. import { OperationVariables } from '@apollo/react-common';
import { DocumentNode } from 'graphql';
import { useEffect } from 'react';
import { useLazyQuery, LazyQueryHookOptions } from '@apollo/react-hooks';
const useSkippableQuery = <TData = any, TVariables = OperationVariables>(
query: DocumentNode,
options: LazyQueryHookOptions<TData, TVariables> & { skip: boolean }
) => {
const { skip, ...rest } = options;
const [lazyQuery, result] = useLazyQuery<TData, TVariables>(query, rest);
useEffect(() => {
if (!skip) lazyQuery();
}, [skip]);
/* On the first render cycle, lazy query always returns loading false because the useEffect has
* not yet invoked the query. This ensures that the loading will be true on the first cycle
* by checking the skip prop directly and checking the query has been called */
const loading = result.loading || (!skip && !result.called);
const data = !skip ? result.data : undefined;
return { ...result, data, loading };
};
export default useSkippableQuery; You can then use this pretty much like |
@s-taylor nice hook! I was going down the route of switching us to lazy query with effects/skip flags but I got hung up on the initial loading state. I'll give this hook a shot :) Thanks for the suggestion! Edit: @s-taylor, oops, forgot that const data = !skip ? result.data : undefined;
return { ...result, data, loading }; Edit 2: We need to pass variables to the invoker since the variable can change between renders, but for some reason, passing the variables to the effect cause an infinite loop. :/ const { skip, variables, ...rest } = options;
const [runQuery, result] = useLazyQuery(query, rest);
useEffect(() => {
if (!skip) {
runQuery({
variables
});
}
}, [runQuery, skip, variables]); |
as a workaround, you might want to try this #6572 (comment) You don't need to use useEffect nor useLazyQuery and avoid having 2 render cycles TLDR const skip = !company;
const variables = !skip
? {
companyId: company.id,
}
: {};
const { data, loading } = useQuery({
variables,
skip,
}); |
@tafelito does not work for us - query is still invoked when |
@sirugh nice thanks! I will update the snippet. One flaw I've found with my approach though, is that once skip has been set to |
We're finalizing a fix for this and other |
When skip is set to true but other updated options have been passed into `useQuery` (like updated variables), `useQuery` will still make a network request, even though it will skip the handling of the response. This commit makes sure `useQuery` doesn't make unnecessary `skip` network requests. Fixes #6670 Fixes #6190 Fixes #6572
When skip is set to true but other updated options have been passed into `useQuery` (like updated variables), `useQuery` will still make a network request, even though it will skip the handling of the response. This commit makes sure `useQuery` doesn't make unnecessary `skip` network requests. Fixes #6670 Fixes #6190 Fixes #6572
When skip is set to true but other updated options have been passed into `useQuery` (like updated variables), `useQuery` will still make a network request, even though it will skip the handling of the response. This commit makes sure `useQuery` doesn't make unnecessary `skip` network requests. Fixes #6670 Fixes #6190 Fixes #6572
@sirugh would you be able to verify if |
@hwillson I'll give it a check after my morning meetings. Can you give me about two hours? If you publish pre's to npm you could also check the sandbox quickly, I believe. Edit: Looks like you guys do, and looks like it did fix the issue in the sandbox. As discussed on the PR, data is being returned now when |
When skip is set to true but other updated options have been passed into `useQuery` (like updated variables), `useQuery` will still make a network request, even though it will skip the handling of the response. This commit makes sure `useQuery` doesn't make unnecessary `skip` network requests. Fixes #6670 Fixes #6190 Fixes #6572
When skip is set to true but other updated options have been passed into `useQuery` (like updated variables), `useQuery` will still make a network request, even though it will skip the handling of the response. This commit makes sure `useQuery` doesn't make unnecessary `skip` network requests. Fixes #6670 Fixes #6190 Fixes #6572
Intended outcome:
skip: true
results in no network request.The
error
property in the result of the useQuery hook reflects the current running query.Actual outcome:
skip: true
results in a network request.The
error
property displays for the previous result.How to reproduce the issue:
400
from the graphql endpoint.Versions
Related Issues
#6190
#6572
#6507
The text was updated successfully, but these errors were encountered: