Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Add generic types for updates #75

Open
lukasluecke opened this issue Sep 12, 2019 · 6 comments
Open

Add generic types for updates #75

lukasluecke opened this issue Sep 12, 2019 · 6 comments
Labels
feature 🚀 New feature or request help wanted ⛏️ Extra attention is needed

Comments

@lukasluecke
Copy link
Contributor

lukasluecke commented Sep 12, 2019

I have the following code (in updates.Mutation) to update a list count after creating an item:

createItem: (result, args, cache, info) => {
    cache.updateQuery({query: ListDocument, variables: {parentId: args.input.parent}}, data => {
        data.parent.items.totalCount = data.parent.items.totalCount + 1;
        return data;
    })
}

There's currently no validation / hints on variables or data, which should be possible by passing the respective types as generics to updateQuery (similar to how useQuery/useMutation hooks work).

I also get a typescript error on data.parent.items:

Property items does not exist on type DataField


It would also be great to have the possibility to type the whole function createItem, so we could have validation / hints on args and result as well.

Right now I get another typescript error on args.input.parent:

Property parent does not exist on type string | number | boolean | Variables | ScalarObject | Scalar[] | Variables[]

@jBugman
Copy link

jBugman commented Sep 12, 2019

Was meant to post almost exactly that.
As a workaround I currently use this pattern, @lukasluecke:

const post = data.addPost as Post;
...
return {
      ...prevData,
      post,
    } as Data;

@lukasluecke
Copy link
Contributor Author

@jBugman Thanks for the workaround, that works (and is nicer than just @ts-ignore 😅).

@JoviDeCroock
Copy link

We could add a generic here: https://github.com/FormidableLabs/urql-exchange-graphcache/blob/master/src/types.ts#L69

Not entirely sure if that could solve all use cases but I'll dig into that later.

@lukasluecke
Copy link
Contributor Author

@JoviDeCroock Looks like that could solve the issues, thank you. 🙏

@JoviDeCroock
Copy link

JoviDeCroock commented Sep 12, 2019

Hmm thinking about it further, TS won't allow it being set there. Will have to dig deeper into this. Sorry about that hadn't given it enough thought yet

@lukasluecke
Copy link
Contributor Author

No problem, it's not a blocking issue, but I think this would be a great addition 🙂

@kitten kitten added feature 🚀 New feature or request help wanted ⛏️ Extra attention is needed labels Sep 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature 🚀 New feature or request help wanted ⛏️ Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants