Skip to content

code mod for breaking syntax changes #2983

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
TkDodo opened this issue Nov 19, 2021 · 7 comments
Closed

code mod for breaking syntax changes #2983

TkDodo opened this issue Nov 19, 2021 · 7 comments

Comments

@TkDodo
Copy link
Collaborator

TkDodo commented Nov 19, 2021

useQuery, useInfiniteQuery, queryClient.fetchQuery, queryClient.prefetchQuery, queryClient.fetchInfiniteQuery, queryClient.prefetchInfiniteQuery, useInfiniteQuery
input:
  useQuery('todos', fetchTodos)
output:
  useQuery(['todos'], fetchTodos)

input:
  useQuery(['todos'], fetchTodos, { staleTime: 1000 })
output:
  useQuery(['todos'], fetchTodos, { staleTime: 1000 })

input:
  useQuery(['todos'], { queryFn: fetchTodos, staleTime: 1000 })
output:
  useQuery(['todos'], { queryFn: fetchTodos, staleTime: 1000 })

input:
  useQuery('todos', { queryKey: ['notTodos'], queryFn: fetchTodos, staleTime: 1000 })
output:
  useQuery(['todos'], { queryKey: ['notTodos'], queryFn: fetchTodos, staleTime: 1000 })

input:
  useQuery({ queryKey: 'todos', staleTime: 1000 })
output:
  useQuery({ queryKey: ['todos'], staleTime: 1000 })
  
input:
  const key = 'todos'
  useQuery(key, { queryFn: fetchTodos, staleTime: 1000 })
output:
  useQuery({ queryKey: [key], queryFn: fetchTodos, staleTime: 1000 })

input:
  const queryKey = 'todos'
  useQuery({ queryKey, queryFn: fetchTodos, staleTime: 1000 })
output:
  useQuery({ queryKey: [queryKey], queryFn: fetchTodos, staleTime: 1000 })
  
input:
  const key = ['todos']
  useQuery(key, { queryFn: fetchTodos, staleTime: 1000 })
output:
  useQuery(key, { queryFn: fetchTodos, staleTime: 1000 })

input:
  const queryKey = ['todos']
  useQuery({ queryKey, queryFn: fetchTodos, staleTime: 1000 })
output:
  useQuery({ queryKey, queryFn: fetchTodos, staleTime: 1000 })

input:
  const queryKey = createKey()
  useQuery({ queryKey, queryFn: fetchTodos, staleTime: 1000 })
output:
  useQuery({ queryKey, queryFn: fetchTodos, staleTime: 1000 })

input:
  import { useQuery as useReactQuery } from 'react-query'
  const useQuery = (key, fn, options) => useReactQuery(key, fn, { ...options, staleTime: 1000 })
  useQuery('todos', fetchTodos, {})
output:
  import { useQuery as useReactQuery } from 'react-query'
  const useQuery = (key, fn, options) => useReactQuery(key, fn, { ...options, staleTime: 1000 })
  useQuery(['todos'], fetchTodos, {})

input:
  import * as RQ from 'react-query'
  const useQuery = (key, fn, options) => RQ.useQuery(key, fn, { ...options, staleTime: 1000 })
  useQuery('todos', fetchTodos, {})
output:
  import * as RQ from 'react-query'
  const useQuery = (key, fn, options) => RQ.useQuery(key, fn, { ...options, staleTime: 1000 })
  useQuery(['todos'], fetchTodos, {})
queryClient.invalidateQueries, queryClient.refetchQueries, queryClient.resetQueries, queryClient.removeQueries, queryClient.cancelQueries
input:
  const queryClient = useQueryClient()
  queryClient.invalidateQueries('todos')
output:
  const queryClient = useQueryClient()
  queryClient.invalidateQueries(['todos'])

input:
  const queryClient = useQueryClient()
  queryClient.invalidateQueries('todos', { exact: true })
output:
  const queryClient = useQueryClient()
  queryClient.invalidateQueries(['todos'], { exact: true })

input:
  const queryClient = useQueryClient()
  queryClient.invalidateQueries(['todos'], { exact: true }, options)
output:
  const queryClient = useQueryClient()
  queryClient.invalidateQueries(['todos'], { exact: true }, options)

input:
  useQueryClient().invalidateQueries({ queryKey: 'todos', exact: true })
output:
  useQueryClient().invalidateQueries({ queryKey: ['todos'], exact: true })

input:
  useQueryClient().invalidateQueries({ queryKey: ['todos'], exact: true })
output:
  useQueryClient().invalidateQueries({ queryKey: ['todos'], exact: true })

input:
  const queryKey = 'todos'
  useQueryClient().invalidateQueries({ queryKey, exact: true })
output:
  useQueryClient().invalidateQueries({ queryKey: [queryKey], exact: true })

input:
  const queryKey = ['todos']
  useQueryClient().invalidateQueries({ queryKey, exact: true })
output:
  useQueryClient().invalidateQueries({ queryKey, exact: true })
queryClient.getQueryData, queryClient.getQueryState, queryCache.find, queryClient.getQueriesData, queryClient.isFetching, useIsFetching, useIsMutating, queryCache.findAll, queryClient.setQueriesData
input:
  const queryClient = useQueryClient()
  queryClient.getQueryData('key', { exact: true })
output:
  const queryClient = useQueryClient()
  queryClient.getQueryData(['key'], { exact: true })

input:
  const queryClient = useQueryClient()
  queryClient.getQueryData(['key'], { exact: true })
output:
  const queryClient = useQueryClient()
  queryClient.getQueryData(['key'], { exact: true })
queryClient.setQueryData, queryClient.setMutationDefaults, queryClient.getMutationDefaults, queryClient.setQueryDefaults, queryClient.getQueryDefaults
- fn(key, updater, options)
+ fn([key], updater, options)
useMutation

same as useQuery, except that queryKey is mutationKey

useQueries

An easy one because useQueries only gets an array passed, which should become an object. Nothing within query1 / query2 / query3 needs to change.

- useQueries([query1, query2, query3])
+ useQueries({ queries: [query1, query2, query3])

after that, inside the queries object, we need to change queryKey to be an array, like for useQuery with the object syntax.

@TkDodo TkDodo added the v4 label Nov 19, 2021
@TkDodo TkDodo added this to the v4 milestone Nov 19, 2021
@TkDodo
Copy link
Collaborator Author

TkDodo commented Nov 19, 2021

@balazsmatepetro I've added some examples for useQuery. I will add more for the other methods a bit later. I hope I didn't make any syntax errors 😅

@balazsmatepetro
Copy link
Contributor

balazsmatepetro commented Nov 19, 2021

Thank you so much, it helps a lot! :)

@TkDodo
Copy link
Collaborator Author

TkDodo commented Jan 3, 2022

@balazsmatepetro how are things on the code mod? Do you need any help?

@balazsmatepetro
Copy link
Contributor

@balazsmatepetro how are things on the code mod? Do you need any help?

Hi! I'm planning to open a PR at the end of this week. :)

@balazsmatepetro
Copy link
Contributor

balazsmatepetro commented Jan 18, 2022

Task list

Write codemods for:

  • useQuery hook
  • useQueries hook
  • useInfiniteQuery hook
  • useIsFetching hook
  • useIsMutating hook
  • useMutation hook
  • QueryCache methods
    • find
    • findAll
  • QueryClient methods
    • Object syntax aware
      • cancelQueries
      • fetchInfiniteQuery
      • fetchQuery
      • invalidateQueries
      • prefetchInfiniteQuery
      • prefetchQuery
      • refetchQueries
      • removeQueries
      • resetQueries
    • Not object syntax aware
      • cancelQueries
      • getMutationDefaults
      • getQueriesData
      • getQueryData
      • getQueryDefaults
      • getQueryState
      • invalidateQueries
      • isFetching
      • refetchQueries
      • removeQueries
      • resetQueries
      • setMutationDefaults
      • setQueriesData
      • setQueryData
      • setQueryDefaults

Additional tasks:

  • Add support for mutationKey in case of useMutation hook.
  • Add support for template literals in case of query key.
  • Try to combine ALL codemods into a single one.
  • Move files under v4 directory.
  • Reduce the number of repetitive test fixtures.

Things to address after our last sync with @TkDodo:

  • Remove use_query directory, it's fine to have everything under the codemods/v4 directory.
  • Add codemods to the bundle.
  • Add usage instructions to the codemods.
  • Do not remove typeArguments from the nodes (mostly generic types).
  • Add file paths to the console warnings.
  • Fix invalid usage of invalidateQueries method in playground example.
  • Leave code untouched when the queryKey is already an array.

@TkDodo TkDodo closed this as completed Feb 18, 2022
@tannerlinsley
Copy link
Collaborator

🎉 This issue has been resolved in version 4.0.0-alpha.13 🎉

The release is available on:

Your semantic-release bot 📦🚀

@tannerlinsley
Copy link
Collaborator

🎉 This issue has been resolved in version 4.0.0-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

No branches or pull requests

3 participants