Open renganatha10 opened 6 years ago
I believe this is currently not possible based on the invocation of dataIdFromObject
:
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.)
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'
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!
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.
~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 doesn't appear to be possible in @apollo/client@3.0.0-beta.36, 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.
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 ?
In the above code variables is
undefined