Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
layerssss opened this issue Sep 27, 2021 · 5 comments

Comments

@layerssss
Copy link

layerssss commented Sep 27, 2021

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",
@layerssss
Copy link
Author

layerssss commented Oct 18, 2021

Hi, could someone look at this to see if it's something unexpected? The use case leading to this is I have a collection field items accepting some filter args e.g items(searchTerm: String).

Somewhere on UI the query

query($searchTerm: String) {
  folder {
    id
    items(searchTerm: $searchTerm) {
      id
    }
  }
}

... runs with { searchTerm: null }, returing the un-filtered / whole collection.

Then a mutation

mutation {
  addItem {
    folder {
      id
      items {
        id
      }
    }
  }
}

... is expected to notify UI of added item. But since items{searchTerm:null} and items were saved as different entries in cache, UI is not notified.

@layerssss
Copy link
Author

Please, could someone points to me to the right direction for this. It seems that I can't get around this... apart from using two different queries ... one with args, one without args(in order to not have {} in the cache key). But that totally diminishes the point of having optional argument in GraphQL.

@louisholley
Copy link

@layerssss did you ever find a solution to this?

@layerssss
Copy link
Author

@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
      }
    }
  `
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
@layerssss @bignimbus @louisholley and others