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.

query returns object from cache with no fields except __typename even when selected

See original GitHub issue

EDIT: the description is from before @dmoree identified the problem.

As a workaround I am able to see all the fields in my data when I call the hook with fetchPolicy: 'no-cache'

With all the other policies, the bug occurs. This is my query:

fragment TopicDetailsForResourceCard on ITopicModel {
  id
  currentTitle
  is_archived
  created_at
  currentTopicVersion {
    id
    title
    duration
    summary
  }
  author {
    id
    email
    image_url
    username
    profile_img_url
  }

  lastEnrolment {
    id
    completion_date
    progress
    target_completion_date
  }

  coverImage {
    id
    cdnUrl
    imageVariants {
      cdnUrl
    }
  }
}

query workspaceTopicsAndThemes($workspaceId: PositiveInt!) {
  workspace(id: $workspaceId) {
    id
    description
    name
    coverImage {
      id
      cdnUrl
      imageVariants {
        cdnUrl
      }
    }
    topics(filters: [{ name: noTheme }, { name: withCurrentVersion }]) {
      ...TopicDetailsForResourceCard
    }
    themes {
      id
      title
      topics(filters: [{ name: withCurrentVersion }]) {
        ...TopicDetailsForResourceCard
      }
    }
  }
}

Intended outcome: query result is the same for every cache policy This is how I would like them to look: image

Actual outcome: query returns items that only have __typename attribute. This is how the data for topics look: image

How to reproduce the issue: sorry, it only happens in our codebase, I wasn’t able to repro on codesandbox yet.

Versions

  System:
    OS: Linux 5.4 Ubuntu 20.04 LTS (Focal Fossa)
  Binaries:
    Node: 12.14.1 - ~/.nvm/versions/node/v12.14.1/bin/node
    Yarn: 1.22.4 - ~/.yarn/bin/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v12.14.1/bin/npm
  Browsers:
    Chrome: 84.0.4147.89
    Firefox: 78.0.2
  npmPackages:
    @apollo/client: ^3.0.2 => 3.0.2 
    @apollo/link-batch-http: ^2.0.0-beta.3 => 2.0.0-beta.3 
    @apollo/link-context: ^2.0.0-beta.3 => 2.0.0-beta.3 
    @apollo/link-error: ^2.0.0-beta.3 => 2.0.0-beta.3 
    @apollo/link-schema: ^2.0.0-beta.3 => 2.0.0-beta.3 
    @apollo/react-components: ^3.1.5 => 3.1.5 
    @apollo/react-ssr: ^3.1.5 => 3.1.5 
    @apollo/react-testing: ^3.1.4 => 3.1.4 
    apollo-cache-inmemory: ^1.6.6 => 1.6.6 
    apollo-client: ^2.6.10 => 2.6.10 
    apollo-server: ^2.15.1 => 2.15.1 
    apollo-server-express: ^2.15.1 => 2.15.1 
    react-apollo: ^3.1.5 => 3.1.5 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
dmoreecommented, Aug 5, 2020

@capaj For @apollo/client I believe they are moving away from fragmentMatcher and towards explicitly declaring possible types through the possibleTypes field in InMemoryCache. Here is the documentation regarding possible types of interfaces and unions: https://www.apollographql.com/docs/react/data/fragments/#using-fragments-with-unions-and-interfaces

I am also using @graphql-codegen. Here is the documentation regarding the use in apolloClientVersion: 3 (i.e. @apollo/client): https://graphql-code-generator.com/docs/plugins/fragment-matcher#usage-with-apollo-client-3

Removing the fragmentMatcher field and explicitly adding the possibleTypes field using the example in @graphql-codegen documentation may be the solution to your problem.

2reactions
napikwaancommented, Aug 28, 2020

I was having this exact problem and adding the possibleTypes field to my InMemoryCache configuration did indeed remedy the issue.

*edited: grammar

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customizing the behavior of cached fields - Apollo GraphQL
The first parameter is the field's currently cached value (if one exists). You can use this to help calculate the value to return....
Read more >
How to remove the `__typename` field from the graphql ...
js:28 DEPRECATION WARNING: using fragments without __typename is unsupported behavior and will be removed in future versions of Apollo client.
Read more >
Normalized Caching | urql Documentation
The normalized cache can walk the query document. Each field that has no selection set (like title in the above example) must be...
Read more >
Configuring the Cache – Angular - GraphQL Code Generator
If true , the cache automatically adds __typename fields to all outgoing ... The InMemoryCache normalizes query response objects before it ...
Read more >
Global Object Identification - GraphQL
This returns objects which conform to a "Node" interface. The id field can be extracted out of the response safely, and can be...
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