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

'useQuery' never returns data for entries written to the cache with 'writeFragment' #431

Open
limekiln opened this issue Dec 20, 2019 · 0 comments

Comments

@limekiln
Copy link

limekiln commented Dec 20, 2019

Summary:
Because of performance problems with the InMemoryCache, I tried to migrate our app to the hermes cache. Since cache initialization was done via writeData before I changed it to use writeFragment.

So the init function would look something like:

const initCache = cache => {
  let fragmentString = gql`
    fragment test on MyType {
      ...myTypeFragment
    }
    ${fragments.myTypeFragment}
`;
let fragment = {
  id: "myType",
  fragment: fragmentString,
  fragmentName: "test"
  data: {
    __typename: "MyType",
    ...
  }
};
cache.writeFragment(fragment);

The used fragment is part of a collection of fragments and has the following structure:

const fragments = {
  myType: gql`
    fragment myTypeFragment on myType {
      __typename,
      ...
  `,
  ...
}

Using the Apollo dev tools, I can see the following entries getting created in the cache:

"ROOT_QUERY": {
  "data": {
    "myType": null
  }
}
"myType": {
  "data": {
    "__typename": "MyType",
    ...
  }
}

The myType entry looks fine, but the ROOT_QUERY entry is always null. There does not seem to be any reference between them either.

Expected Behavior:

Using

const data = useQuery(GET_MY_TYPE, { onCompleted: data => console.log(Data) });

with

const GET_MY_TYPE = gql`
  query getMyType {
    myType @client {
      ...myTypeFragment
    }
  }
  ${fragments.myTypeFragment}
`;

I expected to see a logging message containing the data from the myType entry in the cache. Executing this query within GraphiQL should also produce this result

Actual Behavior:

Instead, onCompleted is never called. Executing the query within GraphiQL returns

{
  "data": {
    "myType": null
  }
}

System Information

  • OS:
    • Linux 5.0 Ubuntu 19.04 (Disco Dingo)
  • Binaries:
    • Node: 13.5.0 - /usr/local/bin/node
    • npm: 6.13.4 - /usr/local/bin/npm
  • Browsers:
    • Firefox: 71.0
  • npmPackages:
    • @apollo/react-hooks: 3.1.3 => 3.1.3
    • apollo-cache-hermes: ^0.8.10 => 0.8.10
    • apollo-cache-inmemory: ^1.6.5 => 1.6.5
    • apollo-client: ^2.6.8 => 2.6.8
    • apollo-link: ^1.2.13 => 1.2.13
    • apollo-link-error: ^1.1.12 => 1.1.12
    • apollo-link-http: ^1.5.16 => 1.5.16
    • react-apollo: 3.1.3 => 3.1.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant