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

163
Vistas
Apollo fails to merge cache on refetch call

I'm trying to use the merge function for each refetch (filtering a list) but the existing value always comes undefined.

Type Policy of query that return and filter a list:

Query: {
        fields: {
            accounts: {
                keyArgs: false,
                merge(
                    existing,
                    incoming,
                    {args: {page = 1, pageSize = 20}, readField}
                ) {
                    const offset = (page - 1) * pageSize;
        
                    if (!existing?.items) {
                        return incoming;
                    }
        
                    const mergedItems = [...existing.items];
                    const existingIdSet = new Set(
                        mergedItems.map((item) =>
                            readField('externalReferenceCode', item)
                        )
                    );
        
                    const incomingItems = [...incoming.items].filter(
                        (item) =>
                            !existingIdSet.has(readField('externalReferenceCode', item))
                    );
        
                    for (let i = 0; i < incomingItems.length; ++i) {
                        mergedItems[offset + i] = incomingItems[i];
                    }
        
                    return {
                        ...existing,
                        items: mergedItems,
                    };
                },
                read(existing, {args: {page = 1, pageSize = 20}}) {
                    const offset = (page - 1) * pageSize;
        
                    if (!existing) {
                        return;
                    }
        
                    return {
                        ...existing,
                        items: existing.items?.slice(offset, offset + pageSize),
                    };
                },
            },
        },
    },

I'm doing something wrong? Checking by read function, the values pass as expected, but when call a refetch, comes empty.

For merge, the first it's the existing value. And in read, the value printed it was the existing value.

typePolicy.js:34 merge undefined {__typename: 'AccountPage', items: Array(20), totalCount: 235}
typePolicy.js:108 read {__typename: 'AccountPage', items: Array(20), totalCount: 235}
typePolicy.js:34 merge undefined {__typename: 'AccountPage', items: Array(2), totalCount: 2}
typePolicy.js:108 read {__typename: 'AccountPage', items: Array(2), totalCount: 2}

And this is the query:

const GET_ACCOUNTS = gql`
    query getAccounts($filter: String, $pageSize: Int = 20, $page: Int = 1) {
        accounts(filter: $filter, pageSize: $pageSize, page: $page) {
            items {
                id
                externalReferenceCode
                name
            }
            totalCount
        }
    }
`;
about 4 years ago · Juan Pablo Isaza
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