-
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
[^3.0.0] data is undefined while loading #7038
Comments
Was just about to log something very similar. Our case only happens when rapidly refetching though. I can log a separate issue if needed. These are breaking changes for us - we can't deploy if fetching new data wipes the old data while new data is fetching (contrary to 2.x behavior). Can't find documentation for these apparently breaking changes. Repro steps:
Expected Result (2.x):
Current Result (3.x):
Thanks! |
@mrtnbroder This was an intentional change introduced in AC3 by #6566 to fix #6039, and listed as a [BREAKING] change in
It's fair to say this hasn't been a popular change, but the only way I can see to restore reasonable behavior (so that #6039 remains fixed) would be to provide The old behavior was ambiguous, because |
@benjamn thanks for clarification. I was browsing a bit more around here and eventually found out that this was an intentional change. Any idea on when this |
Came here for a solution, I can see there is only a proposal for this right now. Here is my workaround: //...
const [getData, query] = useLazyQuery() // Uses a GraphQL query that has an $id argument/variable
const [data, setData] = React.useState()
React.useEffect(() => {
if (query.data) {
setData(query.data)
}
}, [data])
const handleChange = (id) => {
getData({
variables: {id}
})
}
//... UPDATE: useLazyQuery(query, {
staleWhileRevalidate: true,
// or
swr: true
})
|
For anyone using
import { useRef } from "react"
import type { DocumentNode, QueryHookOptions } from "@apollo/client"
import { useQuery as useQueryT } from "@apollo/client"
export * from "@apollo/client/react"
// code from: // https://github.com/apollographql/apollo-client/issues/6603
// OVERWRITE DEFAULT useQuery from @apollo/client/react on version 3.x until
// https://github.com/apollographql/apollo-client/issues/7038 will be resolved!
export function useQuery<TData, TVariables>(
query: DocumentNode,
options?: QueryHookOptions<TData, TVariables>,
) {
let cachedData = useRef<TData | undefined>(undefined)
const queryResult = useQueryT<TData, TVariables>(query, options)
if (
queryResult.loading !== true &&
queryResult.data !== undefined &&
// Check for empty object due to https://github.com/apollographql/apollo-client/issues/6876
Object.keys(queryResult.data).length > 0
) {
cachedData.current = queryResult.data
}
return { ...queryResult, data: cachedData.current }
} and in the I also agree with @balazsorban44, a flag would be easier and stop us from writing a lot of duplicated code: const data = result.data ?? result.previousData |
Here's our current solution, seems to be working well. The point is just to ensure that once export function useQueryWithTypes<TData, TVariables>(
query: TypedDocumentNode<TData, TVariables>,
options?: QueryHookOptions<TData, TVariables>,
) {
const result = useQuery(query, options);
const [data, setData] = useState<TData | undefined>(undefined);
useEffect(() => {
if (result.data !== undefined) {
setData(result.data);
}
}, [result.data]);
return { ...result, data };
} |
I suggest further discussions on this are made in #6603 as it seems to be a duplicate. This way the maintainers can concentrate on it in a single thread/issue. There are over 500 open issues on this repo, I guess it is hard enough to maintain that. We could potentially make this issue harder to resolve by arguing about the same thing in different places. |
Yes. Closing this one as a dupe of #6603 |
As I commented over in #6603, we finally have an implementation of We recommend the idiom |
I'm a bit late to the discussion but I just wanted to mention this change broke the UI for multiple of my apps and I didn't notice the issue for a while… If it was changed on purpose, it should certainly have been highlighted in huge red type in the Apollo Client release notes or migration guide! |
Not sure if this was fixed but as of v3.6.9 when loading, react query hooks are still returning undefined instead of the old data. Has the intended behavior changed or should I open a bug report? |
The intended behavior changed, if you want the previous data you can use |
Intended outcome:
Let's take the example from your website:
when the
breed
variable changes, it should setloading = true
and return the pastdata
until it resolves.Actual outcome:
when the
breed
variable changes, it setsloading = true
and returnsundefined
fordata
until it resolves.How to reproduce the issue:
See the example above. This used to work in Apollo 2.x. I think it has to do with the cache, but there is nothing mentioned on the docs. This is indeed an app-breaking bug.
Versions
The text was updated successfully, but these errors were encountered: