apollographql / apollo-feature-requests

🧑‍🚀 Apollo Client Feature Requests | (no 🐛 please).
Other
130 stars 7 forks source link

Support array of IDs for useFragment #358

Open slivlikh opened 1 year ago

slivlikh commented 1 year ago

I think would be great if useFragment supports array of IDs. I understand that I can still use useQuery, but it requires a cache redirect, I would like to avoid it. Also, it's more flexible, if one day the component starts receiving an array of IDs instead of a single ID, there is no need to change useFragment to useQuery.

jerelmiller commented 1 year ago

Hey @slivlikh 👋

Would you be willing to provide some code samples and/or use cases on how you'd envision using this?

I'm curious how you might structure your query if you were using useQuery to accomplish this. Would it make sense to "widen" the fragment a bit more to achieve this?

For example:

// rather than trying to get a list of items
const ItemFragment = gql`
  fragment ItemFragment on Item {
    id
  }
`

useFragment({ fragment: ItemFragment, from: [/* list of ids */] })

// you can "widen" the fragment to the parent field to get the list of items
const ParentWithItems = gql`
  fragment ParentWithItemFragment on Parent {
    items {
      id
    }
  }
`

useFragment({ fragment: ParentWithItems, from: {/* id of parent */} })

I merely present this idea to understand if your request is achievable by other means. This doesn't mean we shouldn't consider your request. I'm merely looking to find if what you're looking for is impossible by the API today.

Let us know how we can help! Thanks for opening the issue!