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

useFragment accepts a generic type, but it converts it into a DeepPartialObject. what is the best usage in here to avoid using DeepPartialObject? #11701

Closed
paolojulian opened this issue Mar 19, 2024 · 5 comments

Comments

@paolojulian
Copy link

paolojulian commented Mar 19, 2024

We have just started implementing useFragment. But the problem is we need to add typeguard of use typecast for the DeepPartialObject. useQuery is not doing this.

image
@jerelmiller
Copy link
Member

Hey @paolojulian 👋

This happens because we don't know if the cache can fulfill the entirety of the fragment or not. useFragment will return any data it can, therefore it can be partial. useFragment also returns a complete property that you can use to determine if the cache could fulfill the entire fragment. The types will adjust to the non-partial type if you check for complete first.

const { complete, data } = useFragment<MyFragment>({ fragment })
//                 ^? DeepPartial<MyFragment>

if (complete) {
  data
// ^? MyFragment
}

Hope this helps!

@paolojulian
Copy link
Author

@jerelmiller
Ahhh, I wonder why I didn't notice it 😅 , i was also looking at the useFragment.d.ts but didn't catch it, thanks for this!

Copy link
Contributor

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

@jerelmiller
Copy link
Member

You're welcome! Glad I could help 🙂

Copy link
Contributor

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 Apr 19, 2024
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

2 participants