HuolalaTech/react-query-kit

createQueryClient function

Closed this issue · 6 comments

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.

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],
    },
  },
})

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

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.

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.

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

doc updated.