Skip to content

Make QueryKey consistently an Array #2919

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

Closed
6 tasks
TkDodo opened this issue Nov 11, 2021 · 9 comments
Closed
6 tasks

Make QueryKey consistently an Array #2919

TkDodo opened this issue Nov 11, 2021 · 9 comments
Assignees
Labels
Milestone

Comments

@TkDodo
Copy link
Collaborator

TkDodo commented Nov 11, 2021

When we pass the queryKey to the QueryFunctionContext, we make sure that it is an Array. This is great for global query functions, because you can just work with an Array<unknown> on type level.

However, in other situations where we provide the whole Query, accessing query.queryKey will still be string|Array<unknown>. For example, when using the predicate filter:

  queryClient.invalidateQueries({
    predicate: (query) => query.queryKey...
  })

here, it is not guaranteed that query.queryKey is an Array, so it's harder to work with

Proposed solution

only allow Arrays for QueryKeys (and for MutationKeys as well)

a codemod would be very good for this. we could let it run over all our examples, and we'd also need to update everything in the docs.


things to take care about:

  • useQuery
  • useInfiniteQuery
  • useMutation
  • queryCache.find
  • mutationCache.find
  • all methods on queryClient
@TkDodo TkDodo added the v4 label Nov 11, 2021
@TkDodo TkDodo added this to the v4 milestone Nov 11, 2021
@arnaudbzn
Copy link
Contributor

I think this change is great but we have a type "symmetry" DX issue:

  • queryKey: string | Array<unknown> in useQuery()
  • query.queryKey: Array<unknown> in the predicate usage example.

A solution could be to add an alias: query.key: Array<unknown>

So Query exposes the queryKey with two properties:

  • query.queryKey: string|Array<unknown> as in v3
  • query.key: Array<unknown>

It also means no breaking changes.

@TkDodo
Copy link
Collaborator Author

TkDodo commented Nov 11, 2021

We already do it for the queryFn. There, the queryKey passed to the queryFunctionContext is always of type array. This is needed so that you can have a global query function and don't need to do if (Array.isArray) type checks all the time.

other callbacks that can retrieve an arbitrary query don't get it that way, which is why this issue was suggested. Maybe the best thing would be to take away the string altogether? I guess the main reason it exists is so that you can do useQuery(`/todos?id=${id}`) and then your defaultQueryFn picks it up (where it then is again an Array and you have to get it via queryKey[0])

@arnaudbzn
Copy link
Contributor

We already do it for the queryFn.

Right!

Maybe the best thing would be to take away the string altogether?

The v4 could be a great opportunity for this change!
That will improve the DX with consistent types and recommended practice.

@TkDodo
Copy link
Collaborator Author

TkDodo commented Nov 11, 2021

@tannerlinsley would you also agree to that?

@tannerlinsley
Copy link
Collaborator

Sorry, I'm not sure what I'm agreeing to exactly

@TkDodo
Copy link
Collaborator Author

TkDodo commented Nov 12, 2021

@tannerlinsley the initial proposal was to have the QueryKey be consistently an Array internally, so that all callbacks receive the query.queryKey as an Array, but to still allow to pass a string via the api, like useQuery('todos').

The new proposal would take this one step further and always need an array for the queryKey when you call useQuery, so it would a) be consistent and b) we wouldn't need to do any checking internally.

The reasoning is that it might come across as confusing if you call useQuery with a string as key, but then get it passed as an Array somewhere

@TkDodo
Copy link
Collaborator Author

TkDodo commented Nov 17, 2021

Tanner just said we can always use Arrays 🚀

@TkDodo
Copy link
Collaborator Author

TkDodo commented Nov 17, 2021

we could also add a codemod to this :)

@TkDodo TkDodo self-assigned this Nov 18, 2021
@TkDodo
Copy link
Collaborator Author

TkDodo commented Nov 18, 2021

@balazsmatepetro FYI

@TkDodo TkDodo closed this as completed Nov 19, 2021
charlypoly added a commit to dotansimha/graphql-code-generator that referenced this issue Jan 27, 2022
* fix: ensure mutationKey is an array

react query will require all keys be an array.  see: TanStack/query#2919

* chore: yarn changeset

* chore: yarn generate:examples

* chore: yarn changeset

* test(plugins/react-query): fix tests + update snapshots

Co-authored-by: Charly POLY <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants