question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InMemoryCache: Data with null arguments not merged with data without arguments in

See original GitHub issue

Intended outcome:

  • write to cache for items { ...} with 1 item
  • write to cache for items(foo: $foo) { .. } with 2 items, with $foo being null or undefined
  • read form cache for items { ... }
  • Expect result is 2

Actual outcome:

Result is 1

How to reproduce the issue:

https://codesandbox.io/s/vibrant-sun-fkcn5?file=/src/index.jsx:129-142

import { ApolloClient, InMemoryCache, gql } from "@apollo/client";

const client = new ApolloClient({
  cache: new InMemoryCache()
});

client.writeQuery({
  query: gql`
    query {
      items {
        id
      }
    }
  `,
  data: {
    items: [
      {
        id: 1,
        __typename: "Item"
      }
    ]
  }
});

client.writeQuery({
  query: gql`
    query($foo: Int) {
      items(foo: $foo) {
        id
      }
    }
  `,
  data: {
    items: [
      {
        id: 1,
        __typename: "Item"
      },
      {
        id: 2,
        __typename: "Item"
      }
    ]
  }
});

const { items } = client.readQuery({
  query: gql`
    query {
      items {
        id
      }
    }
  `
});

console.log(items.length);

In the app I’m working on. I can see from Apollo devtools that these two entries are lists using different keys.

Screen Shot 2021-09-28 at 11 05 14 AM

Versions

    "@apollo/client": "^3.4.13",
    "graphql": "^15.6.0",

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
layersssscommented, Oct 6, 2022

@louisholley sadly not. Kinda like a workaround I’ve changed my code to always include an argument (in my case limit: 20 for paging). Then combined with “null-arg” being set to undefined.

In short:

const { items } = client.readQuery({
  query: gql`
    query($searchTerm: String) {
      items(limit: 20, searchTerm: $searchTerm) {
        id
      }
    }
  `, { searchTerm: undefined }
});

reads the same cache as

const { items } = client.readQuery({
  query: gql`
    query {
      items(limit: 20) {
        id
      }
    }
  `
});
0reactions
louisholleycommented, Oct 5, 2022

@layerssss did you ever find a solution to this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

class InMemoryCache - Apollo GraphQL Docs
Writes data to the cache in the shape of a provided GraphQL query. ... merge functions, allowing incoming data to replace existing data,...
Read more >
Data not merging, Apollo 3 pagination with field policies
Take a look at merging arrays of non-normalised objects in the Apollo Client documentation. You need to set a field policy for the...
Read more >
Understanding Caching in Apollo Client 3 by Laura Beatris.
Learn GraphQL with Apollo Odyssey, our hands-on training and tutorial course platform - https://odyssey.apollographql.com/Learn from our ...
Read more >
Apollo Cache in a Nutshell - E.Y. - Medium
An array of key arguments that help the cache avoid storing unnecessary duplicate data. The most important is the read & merge function....
Read more >
Configuring the Cache – Angular - GraphQL Code Generator
To learn how to interact with cached data, see Reading and writing data to the ... ...other arguments... cache: new InMemoryCache(options) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found