question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: useFragment hook

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:28
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

30reactions
benjamncommented, Jun 8, 2021

@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, 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.

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 the useFragment options.

Let me know if/how that matches your hopes/expectations!

12reactions
hwillsoncommented, Aug 30, 2022

This work was completed (https://github.com/apollographql/apollo-client/pull/8782) and will be released in Apollo Client 3.7.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found