apollo-feature-requests icon indicating copy to clipboard operation
apollo-feature-requests copied to clipboard

Accessing Query Variables inside dataIdFromObject in InMemoryCache ?

Open renganatha10 opened this issue 5 years ago • 6 comments

I want to access the query variables to set the key in local cache since my query doesn't have id and identical by query variable, how to access the query variables ?

const cache = new InMemoryCache({
  dataIdFromObject: (object, variables) => {
    console.log(object, variables);
    switch (object.__typename) {
      default:
        return defaultDataIdFromObject(object); // fall back to default handling
    }
  },
});

In the above code variables is undefined

renganatha10 avatar Sep 21 '18 11:09 renganatha10

I believe this is currently not possible based on the invocation of dataIdFromObject:

https://github.com/apollographql/apollo-client/blob/45540d5367673ceaa722e680b92fe5652512f511/packages/apollo-cache-inmemory/src/writeToStore.ts#L379-L380

I would also find it useful to access variables from the dataIdFromObject function, because I would like to cache a search query based on the input rather than the response.

(This is probably a feature request and may be better targeted at apollographql/apollo-feature-requests.)

pl12133 avatar Sep 25 '18 16:09 pl12133

The parent object would be handy as well for the same reason. Some of my objects don't have an id. Things like pagination results, I would like to tie them to the parent 'thing'

kristian-puccio avatar Dec 13 '18 05:12 kristian-puccio

I arrived here because I wanted to open a feature request for the parent object being passed as an additional argument. But perhaps this one can serve for it?

The absence of a parent object being passed as an argument to dataIdFromObject forces us now to include the parent id as a scalar field to the child object, to be able to tell them apart. So it would be a welcome addition!

lensbart avatar Oct 29 '19 15:10 lensbart

This is really a must have feature. Right now our work round is to return the variable in the response so we can use the variable as part of id.

PinkyJie avatar Dec 09 '19 00:12 PinkyJie

~FWIW, on apollo-client 2.6.3, we're able to access the variables for the current request via this.~ (see edit at bottom)

Within dataIdFromObject, this appears to refer to the InMemoryCache that is using it, and this.variables reflect the variables for the currently-executing request.

const cache = new InMemoryCache({
  dataIdFromObject: function(object, variables) { // note: not an arrow function!
    console.log(this.variables);
    ...
  }
})

This doesn't appear to be explicitly documented anywhere, so I'm not sure how much I'd trust using it, but it does seem to help this use-case! It would be great to get this standardized and documented. 🙏

Edit: in hindsight this turns out to not be a great idea; writeToStore calls dataIdFromObject standalone without a this, so it's not a safe fix.

gmcnaughton avatar Feb 24 '20 16:02 gmcnaughton

@gmcnaughton doesn't appear to be possible in @apollo/[email protected], all I get is undefined :( This would've otherwise allowed us to normalize types that lack a ID field themselves but are only accessible via queries that require an ID as argument.

mogelbrod avatar Feb 28 '20 15:02 mogelbrod