Feature: useFragment hook
See original GitHub issueThis 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.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:28
- Comments:7 (3 by maintainers)
Top Results From Across the Web
useFragment - Relay
API reference for useFragment, a React hook used to read fragment data from the Relay store using a fragment reference.
Read more >Hooks (experimental) - Apollo GraphQL Docs
This hook returns an always-up-to-date view of whatever data the cache currently contains for a given fragment. useFragment_experimental never triggers network ...
Read more >How to use the relay-hooks.useFragment function in ... - Snyk
To help you get started, we've selected a few relay-hooks.useFragment examples, based on popular ways it is used in public projects.
Read more >useFragment · Morrys Repositories
The hook is automatically subscribed to updates to the fragment data: if the data for this particular User is updated anywhere in the...
Read more >Welcome to Relay Hooks - DEV Community
... and nothing else while in relay-hooks returns the same object of the render function of the QueryRenderer HOC; useFragment, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@smikula I’m still working on a more official proposal, but what do you think of the following sketch?
As this sketch suggests,
useFragment
is a read-only reactive/live binding into the cache, providing an always-up-to-date view of whateverdata
the cache currently contains for a givenfragment
.This architecture means Apollo Client can broadcast extremely specific fragment results to individual components that use
useFragment
, simply by dumping the results of all incoming queries into the cache. Relay works this way, and I’m very much coming around to it. Perhaps more importantly, Apollo Client has always been capable of this, so I don’t want to leave these capabilities unused.It’s possible to implement something like this already (see https://github.com/apollographql/apollo-client/issues/7072#issuecomment-810611537 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
useQuery
is certainly an option, but then the component gets subscribed to all changes in the query data, when it probably only cares about some small fragment of the query’s data. I think a new kind of query hook will be necessary, or perhaps an additional sub-section within theuseFragment
options.Let me know if/how that matches your hopes/expectations!
This work was completed (https://github.com/apollographql/apollo-client/pull/8782) and will be released in Apollo Client 3.7.