Search code examples
javascriptgraphqlapolloapollo-client

ApolloClient readFragment returns empty object


Accordingly to the docs this is all it takes to query something from apollo cache but it isn't working and the console.log() is showing an empty object. The first image show that I have the entity cached.

  • change optimistic to false doesn't change the result
  • change the id to "141" change the return from empty object to 'null'
  • import gql from "graphql-tag";
  • "apollo-client": "^2.6.8",
  • "graphql-tag": "^2.10.0",
onClick: p => {
  const readFragment = apolloClient.readFragment({
    id: 141,
    fragment: gql`
      fragment myMarca on Marca {
        id
      }
    `,
    optimistic: true,
  });
  console.log(readFragment);
}

enter image description here

enter image description here


Solution

  • The problem was that I was passing id: 141. The right way is Marca:141. Found it from this answer inside Apollo Spectrum Community.