Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
What is the best practice to use the Apollo cache identify method?

Based on the official docs Apollo's cache identify method is used to get the id of the cache item because this ID could be custom and composed from different fields. That's clear.

To do it we need to receive an item from the cache to pass this item into the cache.identify and receive this item ID.

To get a cache item we have the next possibilities, based on the docs:

  1. 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: {
     id: 5,
   },
 });
  1. 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
    }
  `,
});

These methods will return an item from the cache that we further could pass into the cache.identify to receive the ID.
But they require the ID so it must be known at the moment of the execution of any of them.

So I'm wondering what is the best practice to use the cache.identify ?

So far I know, probably, it could be the only 1 use-case.
Obtain the possible cache item id in the update callback that could be used as an option for a mutation that only updates an existing item. The backend could return a response with the modified object and it's possible to pass this object into the cache.identify to get the ID and pass it further into the appropriated cache update method. But I'm not sure if I'm right so would like to clarify it with more experienced Apollo developers.
Is this the only 1 purpose for the cache.identify method?

Thanks for any help!

about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda