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

Type policies broken starting 3.3.0 #7535

Closed
Banou26 opened this issue Jan 1, 2021 · 6 comments · Fixed by #8361
Closed

Type policies broken starting 3.3.0 #7535

Banou26 opened this issue Jan 1, 2021 · 6 comments · Fixed by #8361

Comments

@Banou26
Copy link
Contributor

Banou26 commented Jan 1, 2021

Intended outcome:
Trying to query a typePolicy field should work

Actual outcome:
The query is always loading

How to reproduce the issue:
Try to query a typePolicy field, e.g

export default new InMemoryCache({
  typePolicies: {
    Query: {
      fields: {
        foo: () => 'foo'
      }
    }
  }
})


  // .... in a component
  const { data, ...rest } = useQuery(gql`query Foo { foo @client }`)
  // rest.loading is always true and data is always undefined

Versions
apollo-client ^3.3.0 / ^3.3.6 / ^3.4 beta

@Banou26 Banou26 changed the title Type policies broken starting 3. Type policies broken starting 3.3.0 Jan 1, 2021
@Banou26
Copy link
Contributor Author

Banou26 commented Jan 4, 2021

@Banou26 : did you find any work arounds for this issue? until the fix come in?

@pramodshri-tgsys I downgraded my ApolloClient version to 3.2.8.
If you found any real workarounds I'd like to know too :)

@pramod-knidal
Copy link

In my case, there was an error in the code but no errors were reported. I didn't have a return statement at the end of the function code.

@benjamn
Copy link
Member

benjamn commented Jan 4, 2021

@Banou26 This would be pretty worrisome if true, but it does not seem to be reproducing for me.

The symptoms are consistent with there not being any field policy defined for the Query.foo field. Can you double-check that your actual field name is spelled the same everywhere, and that the InMemoryCache object you're configuring and exporting is the same cache that gets used by the ApolloClient behind the useQuery call?

Of course, a reproduction would be even more useful.

@Banou26
Copy link
Contributor Author

Banou26 commented Jan 12, 2021

@benjamn here is the smallest repro I managed to get.

import { InMemoryCache, ApolloClient, gql } from '@apollo/client'

const cache = new InMemoryCache({
  typePolicies: {
    Query: {
      fields: {
        foo: () => 'foo'
      }
    }
  }
})

cache.policies.addTypePolicies({
  Query: {
    // If you comment the fields property, the logs will return 'foo'
    fields: {
      settings: () => {}
    }
  }
})

const client = new ApolloClient({
  cache
})

console.log(client.query({ query: gql` { foo } ` }))

// results of commented typePolicies fields property

// Promise {<pending>}
// __proto__: Promise
// [[PromiseState]]: "fulfilled"
// [[PromiseResult]]: Object
// data: {foo: "foo"}
// loading: false
// networkStatus: 7



// results of uncommented typePolicies fields property

// Promise {<pending>}
// __proto__: Promise
// [[PromiseState]]: "fulfilled"
// [[PromiseResult]]: undefined

It seems like it's due to the use of cache.policies.addTypePolicies, it seems to "overwrite" instead of "add", if I add foo back into the addTypePolicies object, it does indeed still work fine.

This is new behavior starting 3.3.0, in 3.2.x it worked fine.

@Remownz
Copy link

Remownz commented Mar 21, 2021

I can confirm. We also use multiple cache.policies.addTypePolicies. We also have to downgrade to 3.2.x.

@Banou26
Copy link
Contributor Author

Banou26 commented Apr 24, 2021

@benjamn Any updates on this ?
Still a bug on 3.3.15 which prevents anyone using cache.policies.addTypePolicies to upgrade above 3.2.

@brainkim brainkim self-assigned this Apr 30, 2021
benjamn pushed a commit that referenced this issue Jun 8, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants