Support for query directives
:wave: Hey Apollo team.
I'm not sure if this is a bug, feature request, or by design - would love some guidance.
Variables in query directives don't seem to trigger updates correctly. I have the following query which includes a custom @inContext directive in the actual query:
query getProducts($resultsPerPage: Int, $country: CountryCode) @inContext(country: $country){
products(first:$resultsPerPage) {
...
}
}
If $country changes, cache returns old data + nothing is fetched (unless the policy is set to something like cache-and-network). If$resultsPerPage changes, everything works as expected.
Versions
System:
OS: macOS 11.4
Binaries:
Node: 15.11.0 - /usr/local/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 7.6.0 - /usr/local/bin/npm
Browsers:
Chrome: 92.0.4515.131
Edge: 92.0.902.67
Firefox: 89.0
Safari: 14.1.1
npmPackages:
@apollo/client: ^3.4.5 => 3.4.5
You might already be aware of this, but I'll mention it anyway: if the directive appeared on a field like Query.products rather than the root query AST node, you could differentiate Query.products field values using a keyArgs function:
new InMemoryCache({
typePolicies: {
Query: {
fields: {
products: {
keyArgs(args, { field }) {
// Return any string you want, taking args and field.directives into account.
// This string becomes the suffix of the ROOT_QUERY["products" + suffix]
// key used to store different values for the Query.products field.
}
}
}
}
}
})
This doesn't work for directives attached to the query, of course, because there's no field leading to the ROOT_QUERY object. It's the starting point for all queries, and you can't currently have different versions of it based on directives.
If that explanation makes sense, is that what you would want? Different instances of the root query object for use with different queries? It's an interesting idea to me, but there might be other ways to achieve what you want. Happy to discuss further!
Thanks for the response @benjamn!
If that explanation makes sense, is that what you would want? Different instances of the root query object for use with different queries? It's an interesting idea to me, but there might be other ways to achieve what you want. Happy to discuss further!
It sounds like it. Here's an example of the directive. You could imagine a 'Country' dropdown which changes the country argument. As a workaround right now I can change the fetch policy or reset cache - am I missing an obvious solution?