-
-
Notifications
You must be signed in to change notification settings - Fork 454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Add stricter types for cache
in graphcache resolvers
#2558
Comments
I have been thinking about this, and it might entail a lot of complexity to get all these types in there correctly, tried making a little start here but I am not sure if TypeScript allows us to narrow down the types based on the arguments we are putting in, i.e. we input I think we could adopt this by expanding |
@JoviDeCroock it should be possible. I've updated your TypeScript playground here. With conditional type constraints it's also possible to either require or not allow passing the third argument. Happy to help out if necessary. 👍 |
Thanks @prichodko! We can indeed narrow down the types based on inputs using genetics. I’ve done this a few times so I’m happy to help too.
This should work. TypeScript overloads may be an option too, but I think conditional types + an arguments array that gets spread should work. |
With that method it should probably be possible to assemble a similar object defining the types with their fields and arguments in the |
Would something like this help? |
One simple piece that is missing right now: cache.invalidate({ __typename }) In this case, type ExtractHasTypename<T> = T extends { __typename: string } ? T : never
type QueryValue = Query[keyof Query]
type Typename = ExtractHasTypename<QueryValue>['__typename'] There, |
Summary
Currently,
cache.invalidate('Query', fieldName, field.args)
lets you pass any arbitrary string tofieldName
and object to the third argument. This means you have to manually cast types beforehand.Proposed Solution
urql.mp4
It would be nice if the
cache
accepted an optionalQuery
generic to give you autocomplete suggestions. For example, if you havemyBookings
inside ofQuery
, thencache.invalidate('Query',
would auto-suggestmyBookings
for thefieldName
, and perhaps it could autocomplete the args too.All of it could be easily made optional with things like
keyof Query | (string & {})
for backwards compatibility.Requirements
I haven't looked into the cache exchange's internals yet, but presumably this would require passing the
Query
generic to thecache
. Assuming the generic can get passed down, this should work withurql-graphcache
's codegen. I'm happy to look into making a PR if this sounds like something you'd approve.Thanks!
The text was updated successfully, but these errors were encountered: