Skip to content

Support for useFragments to watch multiple fragments simultaneouslyΒ #452

@zstiggz

Description

@zstiggz

The useFragment hook is fantastic for subscribing to updates on a single cached fragment. But it's not possible to subscribe to a list of fragments in the cache. Consider the following scenario

fragment CoreImage on Image {
  id
  url
  metadata
}

query images($ids: [String!]!) {
  images(ids: $ids) {
    ...CoreImage
  }
}

mutation createImage(...) {
  createImage(...) {
    ...CoreImage
  }
}

My app might query images once at the start, and then perform a bunch of createImage mutations to add new image assets. Then suppose I wanted to do something like:

const images = useFragments([1,2,3,...])
const allSameSize = images.every(image => image.metadata.width === images[0].metadata.width)

With the hypothetical useFragments hook, this is easy. Without it, it's tricky to create a reactive object that contains multiple fragments.

I could of course leverage useQuery, and write the objects into the query. But that's not quite what I want, because I wouldn't be able to define an arbitrary slice of the data by supplying an array of ids. Each different slice would incur another network request, or require me to manually write the fragment into the query that I'm using as a workaround for the lack of a useFragments hook.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions