Skip to content

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

Merged
merged 15 commits into from
Nov 20, 2021

Conversation

giacomorebonato
Copy link
Contributor

@giacomorebonato giacomorebonato commented Nov 17, 2021

Refetch a query when a dependant mutation executes

I read the following issues:

"Add ability to update the cache after a mutation #52"

Refetch queries with mutations subscription #74

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 contains userId === 1 in its variables.

useQuery(TEST_QUERY, {
  refetchAfterMutations: [
    {
      mutation: UPDATE_USER_MUTATION,
      filter: ({ userId }) => userId === 1
    }
  ]
})

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

  • I have checked the contributing document
  • I have added or updated any relevant documentation
  • I have added or updated any relevant tests

- refetch query when mutation happens
- mutation variables check
@giacomorebonato giacomorebonato changed the title feat: refetch query on mutation feat: refetch query when subscribed mutation executes Nov 17, 2021
@giacomorebonato
Copy link
Contributor Author

giacomorebonato commented Nov 17, 2021

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).
I'll address that too in this PR.

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

Screenshot 2021-11-17 at 15 23 19

@giacomorebonato giacomorebonato marked this pull request as ready for review November 17, 2021 11:11
@giacomorebonato giacomorebonato marked this pull request as draft November 17, 2021 15:59
@giacomorebonato giacomorebonato force-pushed the refetch_query_on_mutation branch 2 times, most recently from 588eb6f to 24eab10 Compare November 18, 2021 08:15
@giacomorebonato giacomorebonato marked this pull request as ready for review November 18, 2021 08:22
@allforabit
Copy link

allforabit commented Nov 18, 2021

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?

Copy link
Member

@simoneb simoneb left a 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:

  1. try it out on a project before publishing a new version to make sure everything keeps working. @allforabit can do that
  2. publish a new version as a prerelease then test it out more thoroughly. I can do this
  3. once we're happy, release a final version

@@ -183,10 +187,16 @@ export interface UseClientRequestOptions<ResponseData = any, Variables = object>
client?: GraphQLClient
}

type RefetchAferMutationsData = {
mutation: string
filter?: (variables: any) => boolean
Copy link
Member

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
- updated docs
- updated examples
- updated minor version
- multiple types for refetchAfterMutations
- updated examples
- case for using else if
README.md Outdated
Comment on lines 246 to 250
- `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
Copy link
Member

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

Comment on lines 199 to 204
refetchAfterMutations?:
| string
| string[]
| RefetchAferMutationsData
| RefetchAferMutationsData[]
}
Copy link
Member

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?

@@ -1,6 +1,6 @@
{
"name": "graphql-hooks",
"version": "5.4.1",
"version": "5.5.0",
Copy link
Member

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Manually update the cache after a mutation or a subscription Add ability to update the cache after a mutation
3 participants