apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.38k stars 2.66k forks source link

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 closed 8 months ago

paolojulian commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

@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!

github-actions[bot] commented 8 months ago

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 commented 8 months ago

You're welcome! Glad I could help 🙂

github-actions[bot] commented 7 months ago

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.