Apollo read multiple values from cache

I’m looking for a way to read the value from cache for Apollo. On the page of Apollo, it only show the way to retrieve one as follow:

  • With readQuery
const READ_TODO = gql`
      query ReadTodo($id: ID!) {
        todo(id: $id) {
          id
          text
          completed
        }
      }
    `;
    
    // Fetch the cached to-do item with ID 5
    const { todo } = client.readQuery({
      query: READ_TODO,
      variables: { // Provide any required variables here.  Variables of mismatched types will return `null`.
        id: 5,
      },
    });
  • or with readFragment
const todo = client.readFragment({
  id: 'Todo:5', // The value of the to-do item's cache ID
  fragment: gql`
    fragment MyTodo on Todo {
      id
      text
      completed
    }
  `,
});

However, I can’t select more than 1 todo at a time in both of them. I will have an array of IDs I’m not sure how many values will be in it. What is an excellent approach to this problem?

Hi @mankinchi,
did you figure this out? I am looking for the same.
Tanks!

Yeah. I’m so stupid at some point :rofl: . readFragment on its own is just a sync function, so you can just run a loop/foreach through an array and change the id to retrieve multiple items.