Apollo-feature-requests: Accessing Query Variables inside dataIdFromObject in InMemoryCache ?

Created on 21 Sep 2018  路  6Comments  路  Source: apollographql/apollo-feature-requests

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

Most helpful comment

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'

All 6 comments

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

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/[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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

noaelad picture noaelad  路  3Comments

jbachhardie picture jbachhardie  路  3Comments

dko-slapdash picture dko-slapdash  路  3Comments

hwillson picture hwillson  路  8Comments

FredyC picture FredyC  路  9Comments