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

Apollo client, Discriminant loading in QueryResult type #442

Open
jsellam opened this issue Dec 4, 2024 · 3 comments
Open

Apollo client, Discriminant loading in QueryResult type #442

jsellam opened this issue Dec 4, 2024 · 3 comments

Comments

@jsellam
Copy link

jsellam commented Dec 4, 2024

Hi everyone,

In react, when I use Apollo client for a query I got a QueryResult type with loading, data, and error.

in my code I have :

const result = useQuery(...)

if(result.loading){
 return <LoadingComponent />
}

if(result.error) {
 return <ErrorComponent />
}

if(result.data?.user){
 return <UserComponent user={result.data.user} />
}

I need to add a condition on if(result.data?.user) only for typescript. If loaded is set to true, and error to false, data should be present no ?
Actually there is no discriminant in the QueryResult definition. Do you think it could be a good new feature ?

@jsellam
Copy link
Author

jsellam commented Dec 4, 2024

I have just seen that is the same feature request opened in 2019 #194

@phryneas
Copy link
Member

phryneas commented Dec 4, 2024

There are various situations where loading might be false, no error is present and you still have no data - e.g. if you are currently skipping the query.
Unfortunately, it's not that simple and almost impossible to represent in types - it would be a big explosion of types, and you'd always have to check networkStatus, too, at least - a boolean alone will never be enough here.

That said, we will try to add some improvements in that direction in the future, but it will also get a lot better once you start using the suspenseful hooks like useSuspenseQuery instead of useQuery, as there are just no loading and error states to manage there.

@jsellam
Copy link
Author

jsellam commented Dec 4, 2024

Thank you for you feeback @phryneas.
I understand the situation better now.

Actually I use assertIsDefined(result.data.user) before the final render. Suspence queries was in preview when I started my current project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants