Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda