apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.34k stars 2.66k forks source link

Can you use `useFragment` with a type that doesn't have an id? #11875

Closed patrick91 closed 4 months ago

patrick91 commented 4 months ago

Hi, I'm trying useFragment with a query that looks like this:

{
  flyers {
    edges { ... }   
    someStats { ... }
  }
}

I'd like to split the query this way:

{
  flyers {
    ...FlyersEdgesFragment
    ...FlyersEdgesStats
  }
}

but it doesn't seem to work 😊 Is my understanding correct that useFragment "only" works with types that have an ID?

bignimbus commented 4 months ago

Hi @patrick91 👋 miss you! A way to identify the entity object in the cache is needed for useFragment. See https://www.apollographql.com/docs/react/api/react/hooks#usefragment, specifically, the from attribute. So if you can identify a fragment by its type and an id, that's probably the most straightforward way. But if there are other valid identifiers that could be used (in combination with keyFields) then that should work too. Hope that helps!

patrick91 commented 4 months ago

@bignimbus I was pretty tired when I was trying the useFragment hook, and I was expecting something more magic.

I've ended up using fragments, but passing the data down directly to the component using the fragment, since I don't have a unique identifier for the flyers field 😊

github-actions[bot] commented 4 months ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

github-actions[bot] commented 3 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using StackOverflow or our discord server.