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

Query with embedded fragment returns an empty object on cache miss instead of null #6368

Closed
jebonfig opened this issue May 31, 2020 · 1 comment
Assignees
Milestone

Comments

@jebonfig
Copy link

This is a new issue in beta.53 as a result of c810713

With this code:

typePolicies: {
  Query: {
    fields: {
      pokemon(existingData, { args, toReference }) {
        return existingData || toReference({ __typename: 'Pokemon', id: args?.id });
      }
    }
  },

...

const PokemonFields = ` 
  fragment PokemonFields on Pokemon {
    id
    number
    name
    classification
    types
    resistant
    weaknesses
    fleeRate
    maxCP
    maxHP
    image
  }`

// FRAGMENT BASED QUERY
  
let FRAGMENT_POKEMON_QUERY = gql`
  ${PokemonFields}
  query pokemon($id: String) {
    pokemon(id: $id) {
      ...PokemonFields
    }
  }
`;

Intended outcome:

  1. When using a read type policy and a query containing a fragment with a cache* fetchPolicy, when the cache misses, return null for the result instead of an empty object.

  2. most likely directly related to 1, cache-first needs to make a network call to fetch the data if the initial cache search missed.

Actual outcome:

If cache-only, or cache-and-network, the returned pokemon is an empty object on renders until the cache hits.

If cache-first and the cache missed, the returned pokemon is an empty object on all renders, and a network call never gets made.

If the query does not use a fragment, or the fetchPolicy is network-only, the returned pokemon will be null on all renders until the cache hits or the network call returns with data. I believe this is the expected behavior.

As a result of the above behavior, depending on the fetchPolicy and whether a fragment is embedded, the result object can now be either null, or an empty object.

How to reproduce the issue:

Repro here: https://github.com/jebonfig/react-apollo-error-pokemon

Versions

  System:
    OS: macOS High Sierra 10.13.6
  Binaries:
    Node: 11.2.0 - /usr/local/bin/node
    npm: 6.4.1 - /usr/local/bin/npm
  Browsers:
    Chrome: 83.0.4103.61
    Safari: 13.1.1
  npmPackages:
    @apollo/client: ^3.0.0-beta.53 => 3.0.0-beta.53 
@benjamn benjamn self-assigned this Jun 1, 2020
@benjamn benjamn added this to the Release 3.0 milestone Jun 1, 2020
benjamn added a commit that referenced this issue Jun 1, 2020
…cts"

This reverts commit c810713, introduced
recently in PR #6365. My optimism that #6365 was an improvement seems to
have been misplaced, since it caused the regression reported in #6368.
We're too close to the AC3 RC/release to be floating risky changes like
these without compelling justifications.
benjamn added a commit that referenced this issue Jun 1, 2020
…cts" (#6371)

This reverts commit c810713, introduced
recently in PR #6365. My optimism that #6365 was an improvement seems to
have been misplaced, since it caused the regression reported in #6368.
We're too close to the AC3 RC/release to be floating risky changes like
these without compelling justifications.
@benjamn
Copy link
Member

benjamn commented Jun 1, 2020

@jebonfig This should be fixed in the latest beta (-bets.54). Thanks for catching this regression so quickly, and sorry for any inconvenience it caused.

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

No branches or pull requests

2 participants