-
Notifications
You must be signed in to change notification settings - Fork 93
feat: refetch query when subscribed mutation executes #686
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
Conversation
- refetch query when mutation happens - mutation variables check
I believe deploy-preview step is failing because package-lock is still generated with Node 14 while the server is using Node 16 (I had the same issue locally). Updates I re-generated all lock files with Node 16, but I need someone's feedback about this, since the job is called "ci / build (14.x)", I am not sure if I am doing the right thing then. Netlify deploy-preview step is using Node 16 |
588eb6f
to
24eab10
Compare
I've left a couple of comments. I want to try it out on a project but just wanted to ensure I'm using it correctly as the type signature seems to be wrong. I'm assuming it should be an array of mutation objects. I'm wondering if it needs to be an array of objects and could be optionally an array of strings with just the mutations? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't get a chance review thoroughly but upon a quick look this looks sensible and overall very good.
Considering the large change I would suggest to:
- try it out on a project before publishing a new version to make sure everything keeps working. @allforabit can do that
- publish a new version as a prerelease then test it out more thoroughly. I can do this
- once we're happy, release a final version
- fix typings
- updated docs - updated examples
packages/graphql-hooks/index.d.ts
Outdated
@@ -183,10 +187,16 @@ export interface UseClientRequestOptions<ResponseData = any, Variables = object> | |||
client?: GraphQLClient | |||
} | |||
|
|||
type RefetchAferMutationsData = { | |||
mutation: string | |||
filter?: (variables: any) => boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
variables is going to be an object I guess?
- refetch query when mutation happens - mutation variables check
- fix typings
- updated docs - updated examples
- updated minor version - multiple types for refetchAfterMutations - updated examples
…ql-hooks into refetch_query_on_mutation
- case for using else if
README.md
Outdated
- `refetchAfterMutations`: list of mutations that will trigger the query refetch. It can be either: | ||
- String | String[] - The string containing the mutation's definition or an array of them | ||
- Object | Object[] with the following properties or an array of them | ||
- `mutation`: String - The mutation string | ||
- `filter`: Function (optional) - It receives mutation variables as parameter and blocks refetch if it returns false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would consider describing it in a different way:
- avoid mentioning "list of mutations", because in fact it could be only one
- the type definition should actually be
string | object | (string | object)[]
. Then, once you define the type you describe- if it's a string, it's the mutation string
- if it's an object then it has properties mutation and filter
- if it's an array, the elements can be of either type above
rephrase in a way that makes sense to the reader :)
packages/graphql-hooks/index.d.ts
Outdated
refetchAfterMutations?: | ||
| string | ||
| string[] | ||
| RefetchAferMutationsData | ||
| RefetchAferMutationsData[] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't we define the RefetchAfterMutationsData as a union type which represents all the possible options instead of doing it here?
packages/graphql-hooks/package.json
Outdated
@@ -1,6 +1,6 @@ | |||
{ | |||
"name": "graphql-hooks", | |||
"version": "5.4.1", | |||
"version": "5.5.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't bump version numbers in PRs, they're bumped when we release
* Checks values of refetchAfterMutations public option and maps them to an object | ||
* @typedef {import('../index').RefetchAferMutationsData} RefetchAferMutationsData | ||
* | ||
* @param {string | string[] | RefetchAferMutationsData | RefetchAferMutationsData[]} refetchAfterMutations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the comment above would simplify this, too
Refetch a query when a dependant mutation executes
I read the following issues:
They contain a proposal by @bmullan91 and jgoux that I liked, the n.3 about refetchAfterMutations.
Meaning that when using
useQuery
we can subscribe to a list of mutations that make the entire query refetch.It also allows a
filter
property with a function that allows refetch only when it evaluates true.Query below executes again when
UPDATE_USER_MUTATION
executes and containsuserId === 1
in its variables.The above is achieved by adding an EventEmitter (new events dependency) property to the GraphQLClient class.
While the issue also mentions the manual manipulation of the cache when a mutation happens to avoid roundtrips, I believe that this implementation stays relevant because it's easier to use and flexible.
It's my first contribution to the project and I am afraid that I am missing out something important: looking forward to hearing your thoughts :)
Related issues
For reviewers
The first commit in this work contains the implementation which touches 8 files.
The next commits is me updating the project to Node 16, to fix Netlify build that relies already on it.
If this work wasn't needed I'll take them out.
Checklist