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

cache.modify() not a function #6208

Closed
philvc opened this issue Apr 28, 2020 · 13 comments
Closed

cache.modify() not a function #6208

philvc opened this issue Apr 28, 2020 · 13 comments

Comments

@philvc
Copy link

philvc commented Apr 28, 2020

Hello,

I have a problem with cache.modify() when trying to update the cache in a resolver with the v.3.0 beta @apollo/client version.

The chrome browser tells me that modify() is not a function:
cache.identify is working.
image

Here is my code :
`export const resolvers: any = {

Mutation: {
updateApplicationProofUrl: (__:any, {input}:any, {cache, getCacheKey}:any) => {
const {jobId, signedGetUrl} = input
const id = cache.identify({
__typename: 'Job',
id: jobId
})
console.log('id', id)
cache.modify(id, {applicationProofUrl: signedGetUrl})
}
},
}`
Note : cache.identify() is working.

My cache is configured with this
// Graphql default state const cache = new InMemoryCache(); const link = new HttpLink({ uri: "http://localhost:5001" }) // GraphqlClient const client = new ApolloClient<NormalizedCacheObject>({ link, cache, typeDefs, resolvers, connectToDevTools: true })

Do you have an idea of the problem ?

It's my first ever issue posting on any library so if it misses some info, don't hesitate to tell me.

Thank you for your help !

@hwillson
Copy link
Member

@philvc cache.modify was recently removed. See #6289 for the full details.

@larsblumberg
Copy link

Please note that https://www.apollographql.com/docs/react/data/mutations/ still refers to cache.modify in its examples

@krskoondra
Copy link

Here is still reference to cache.modify: https://www.apollographql.com/docs/react/caching/cache-interaction/#cachemodify

@benjamn
Copy link
Member

benjamn commented Jul 24, 2020

For the record, we reinstated cache.modify in #6350, after outcry over #6289.

@sgup
Copy link

sgup commented Nov 14, 2020

I'm getting this error on the latest @apollo/client: 3.2.5

@sflahave
Copy link

I'm seeing it in 3.3.0-rc2 also:

index.js:1 Error: modify is not a function
    at new ApolloError (index.js:26)
    at Object.next (QueryManager.js:110)
    at notifySubscription (Observable.js:135)
    at onNotify (Observable.js:179)
    at SubscriptionObserver.next (Observable.js:235)
    at iteration.js:4
    at Array.forEach (<anonymous>)
    at iterateObserversSafely (iteration.js:4)
    at Object.next (Concast.js:24)
    at notifySubscription (Observable.js:135)
    at onNotify (Observable.js:179)
    at SubscriptionObserver.next (Observable.js:235)
    at Object.next (index.js:29)
    at notifySubscription (Observable.js:135)
    at onNotify (Observable.js:179)
    at SubscriptionObserver.next (Observable.js:235)
    at createHttpLink.js:100

@vonkanehoffen
Copy link

Aye I'm seeing this in 3.2.7. I'm reasonably new to Apollo and while lots of the docs are great, I' just so confused about how cache updates are meant to be done without refetchQueries. 🤯

@tdiluzio
Copy link

Seeing the error as well in "@apollo/client": "^3.3.7"

@JasonFehr
Copy link

I was having this problem because I had accidentally installed apollo-client instead of @apollo/client .

@stephenhandley
Copy link

stephenhandley commented Mar 26, 2021

@benjamn still seeing this in 3.3.13

index.js:1 Error: modify is not a function
    at new ApolloError (index.js:26)
    at Object.next (QueryManager.js:110)
    at notifySubscription (Observable.js:135)
    at onNotify (Observable.js:179)
    at SubscriptionObserver.next (Observable.js:235)
    at iteration.js:4
    at Array.forEach (<anonymous>)
    at iterateObserversSafely (iteration.js:4)
    at Object.next (Concast.js:25)
    at notifySubscription (Observable.js:135)
    at onNotify (Observable.js:179)
    at SubscriptionObserver.next (Observable.js:235)
    at notifySubscription (Observable.js:135)
    at onNotify (Observable.js:179)
    at SubscriptionObserver.next (Observable.js:235)
    at createHttpLink.js:100

Looks to be something in here

cache[methodName] = function () {
destructiveMethodCounts.set(
cache,
// The %1e15 allows the count to wrap around to 0 safely every
// quadrillion evictions, so there's no risk of overflow. To be
// clear, this is more of a pedantic principle than something
// that matters in any conceivable practical scenario.
(destructiveMethodCounts.get(cache)! + 1) % 1e15,
);
return original.apply(this, arguments);
};
}

as this is some logging I'm doing prior to that happening

update: writing to cache 
{cache: InMemoryCache, cache_id: "User:XXX", fields: {…}, modifytype: "function", modify: ƒ}
cache: InMemoryCache {watches: Set(3), typenameDocumentCache: Map(8), txCount: 0, getFragmentDoc: ƒ, makeVar: ƒ, …}
cache_id: "User:XXX"
fields: {id: "XXX", ...}
modify: ƒ ()
arguments: (...)
caller: (...)
length: 0
name: ""
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: QueryInfo.js:8
[[Scopes]]: Scopes[3]
modifytype: "function"
__proto__: Object

@jfulse
Copy link

jfulse commented Jun 10, 2021

Also see it in 3.3.20

@jalovatt
Copy link

jalovatt commented Jul 5, 2021

My current project is having the same problem (3.3.20), but I think it's only showing up for update callbacks specified when we fire the mutation.

That is, this is fine:

const [mutate] = useMutation(UPDATE_SOMETHING, {
  variables: { ... },
  update: (cache) => {
    cache.modify({
      ...
    });
  },
});

but this fails:

const [mutate] = useMutation(UPDATE_SOMETHING, {
  variables: { ...  },
};

mutate({
  update: (cache) => {
    cache.modify({
      ...
    });
  },
});

@weilu
Copy link

weilu commented Jul 7, 2021

In case it may help anyone, this is what fixed it for me:

# git diff
-import { InMemoryCache } from 'apollo-cache-inmemory';
+import { InMemoryCache } from '@apollo/client';

@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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests