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.

Cache with {addTypename:false} causes query to return empty data when fragments are used

See original GitHub issue

I have a simple query described in a .gql file:

fragment thingBody on Thing {
    id
    name
    description
}

query things {
    things {
        ...thingBody
    }
}

I have a client with this configuration:

const client = new ApolloClient ({
  new InMemoryCache({ addTypename: false}),
  new HttpLink({ uri: myURI}),
});

Intended outcome:

This query should return this data, which is identical to what’s returned when using the no-cache policy:

{
    things: [
      {
        id: '001',
        name: 'Thing1',
        description: 'This is a standard thing',
      },
      {
        id: '002',
        name: 'Thing2',
        description: 'This is a standard thing',
      },
      {
        id: '003',
        name: 'Thing3',
        description: 'This is a standard thing',
      },
    ]
}

Actual outcome:

{ things: [ {}, {}, {} ] }

How to reproduce the issue:

Configure a cache and client as above and use any query with a fragment.

Versions

Binaries: Node: 14.6.0 - /usr/local/bin/node npm: 6.14.7 - /usr/local/bin/npm npmPackages: @apollo/client: 3.1.4 => 3.1.4

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
l-abelscommented, Sep 14, 2020

If I use the no-cache option or the old apollo-fetch module, the query “just works”. I was a little surprised that adding a cache could cause incorrect query results, I assumed it could be implemented in such a way that it did no harm. Thanks for the info, adapting to having __typename injected into everything won’t be a major issue for me.

0reactions
Ellosaurcommented, Mar 25, 2022

FYI for anyone finding the same issue disabling the cache is the only solution I’ve found. See this answer and dont forget to add back in { addTypename: false } to the “diasbled” InMemoryCache in order to avoid the error Introspection is not allowed. from the server.

Important: this seems to break our use of a paged query as below. I’ll update if I solve that issue as well const { data, error, loading, fetchMore } = useQuery(gql(query), { variables: { term, skipCount: 0 } });

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring the Apollo Client cache - Apollo GraphQL Docs
If true , the cache returns an identical ( === ) response object for every execution of the same query, as long as...
Read more >
How to remove the `__typename` field from the graphql ...
I tried changing the addTypeName: false in the Apollo client in GraphQL ... fragmentMatcher.js:26 You're using fragments in your queries, ...
Read more >
Configuring the Cache – Angular - GraphQL Code Generator
This article describes cache setup and configuration. To learn how to interact with cached data, see Reading and writing data to the cache....
Read more >
How to fix Apollo's MockedProvider returning empty objects for ...
return ( <MockedProvider addTypename={false} mocks={MOCKS}> {children} ... For your mocked query response, add __typename to each fragment.
Read more >
doc/development/fe_guide/graphql.md - GitLab
Fragments are a way to make your complex GraphQL queries more ... When the Apollo Client is used in Vuex and fetched data...
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