-
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
Feature: useFragment hook #8236
Comments
@hwillson, is there any further documentation about the |
@smikula I'm still working on a more official proposal, but what do you think of the following sketch? const {
data, // Whatever data the cache can provide right now for this fragment
complete, // True if no missing fields. Useful with returnPartialData: true
errors, // Provides any MissingFieldErrors from the cache
// Last complete result read by this useFragment, in case data is undefined
// and displaying stale data is acceptable
previousData,
} = useFragment({
// DocumentNode containing the fragment
fragment,
// Name of the fragment you want to use, in case the document contains multiple fragments
fragmentName,
// Minimal representation of the entity to read the fragment from
// Can also be a Reference or an ID string, if you have that handy
from: { __typename: "SomeType", id },
// Various options shared by cache.diff and cache.read
variables,
returnPartialData,
optimistic,
canonizeResults,
}); As this sketch suggests, This architecture means Apollo Client can broadcast extremely specific fragment results to individual components that use It's possible to implement something like this already (see #7072 (comment) for one version), but there are some remaining questions to answer around who (what code) is responsible for issuing the queries, in this new world of only/mostly fragments. Reusing Let me know if/how that matches your hopes/expectations! |
@benjamn Yes, this is exactly what I was hoping for. When can I have it? :) I'll take a look at the comment you linked — maybe something like that will work for me in the meantime. |
@benjamn, I'm still working through this. You mentioned:
Is using I'm not saying this would be the right way to do it in your v4 — you probably want an API that is more explicitly paired with |
Really like the idea of this useFragment / useBackgroundQuery combo. Reminds me of a framework I used to use where the backgoundQuery was called a subscription and the cache acted as a client-side mongo db and the fragments where basically mongo queries on this cache that updated as the cache changed. Worked well for me. |
This work was completed (#8782) and will be released in Apollo Client 3.7. |
👋 The |
This issue will track the development of a
useFragment
hook.Description
useFragment
is a read-only reactive/live binding into the cache, providing an always-up-to-date view of whatever data the cache currently contains for a given fragment.The text was updated successfully, but these errors were encountered: