HuolalaTech / react-query-kit

🕊️ A toolkit for ReactQuery that make ReactQuery hooks reusable and typesafe
MIT License
355 stars 11 forks source link

createQueryClient function #29

Closed ArturKhantsevich closed 1 year ago

ArturKhantsevich commented 1 year ago

Hello,

Readme file mentions createQueryClient function which should allow to register middlewares globally. The search repo result says that there is no any such functionality defined. Do you have any plans to implement this feature?

Thank you for this great package.

liaoliao666 commented 1 year ago

Docs example is a typo. There already support register middlewares globally but no typescript. You can simply add @ts-ignore to solve this problem

new QueryClient({
  defaultOptions: {
    queries: {
     // @ts-ignore
      use: [middleware],
    },
  },
})
denisborovikov commented 1 year ago

You can try this

import { createQuery as createQueryFactory } from 'react-query-kit'

function createQuery<Response, Variables, Error>(
  options: Parameters<typeof createQueryFactory<Response, Variables, Error>>[0]
) {
  return createQueryFactory<Response, Variables, Error>({
    ...options,
    use: [requestIdAttacher, fetchQueryAttacher, ...options.use],
  })
}

const usePosts = createQuery({ queryKey: ..., queryFn: ..., })  // has additional features from pre-defined middleware
denisborovikov commented 1 year ago

If you use default middleware using defaultOptions and add middleware with createQuery as far as I understand, createQuery will replace the ones from defaults, not merge them.

liaoliao666 commented 1 year ago

If you use default middleware using defaultOptions and add middleware with createQuery as far as I understand, createQuery will replace the ones from defaults, not merge them.

It will merge them, only for middleware. Just like swr.

denisborovikov commented 1 year ago

Interesting. This is an important stuff and worth putting to the doc.

liaoliao666 commented 1 year ago

doc updated.