apollographql / apollo-feature-requests

🧑‍🚀 Apollo Client Feature Requests | (no 🐛 please).
Other
130 stars 7 forks source link

Accessing Query Variables inside dataIdFromObject in InMemoryCache ? #152

Open renganatha10 opened 6 years ago

renganatha10 commented 6 years ago

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

pl12133 commented 6 years ago

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.)

kristian-puccio commented 5 years ago

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'

lensbart commented 5 years ago

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!

PinkyJie commented 4 years ago

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.

gmcnaughton commented 4 years ago

~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.

mogelbrod commented 4 years ago

@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.