Skip to content

Commit

Permalink
feat: isolate wagmi's React Query queryClient instance. (#963)
Browse files Browse the repository at this point in the history
* feat: Isolate wagmi query context from react-query default context

* move context addition into custom query hooks

* alphabetize exports

* change imports to be from utils instead of utils/query

* export useMutation and useQueryClient hooks from react package

* add changeset

* tests: update snapshots

* refactor: rename imports

* chore: update changeset

Co-authored-by: moxey.eth <jakemoxey@gmail.com>
  • Loading branch information
aj-may and jxom authored Sep 27, 2022
1 parent 2b5a9e5 commit 8ea29e2
Show file tree
Hide file tree
Showing 23 changed files with 137 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-peaches-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wagmi': patch
---

Isolate wagmi's React Query `queryClient` instance.
11 changes: 9 additions & 2 deletions packages/react/src/context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryClientProvider } from '@tanstack/react-query'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { Provider, WebSocketProvider } from '@wagmi/core'
import * as React from 'react'

Expand All @@ -8,6 +8,10 @@ export const Context = React.createContext<
Client<Provider, WebSocketProvider> | undefined
>(undefined)

export const queryClientContext = React.createContext<QueryClient | undefined>(
undefined,
)

export type WagmiConfigProps<
TProvider extends Provider = Provider,
TWebSocketProvider extends WebSocketProvider = WebSocketProvider,
Expand All @@ -24,7 +28,10 @@ export function WagmiConfig<
}: React.PropsWithChildren<WagmiConfigProps<TProvider, TWebSocketProvider>>) {
return (
<Context.Provider value={client as unknown as Client}>
<QueryClientProvider client={client.queryClient}>
<QueryClientProvider
client={client.queryClient}
context={queryClientContext}
>
{children}
</QueryClientProvider>
</Context.Provider>
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/accounts/useConnect.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMutation } from '@tanstack/react-query'
import { ConnectArgs, ConnectResult, connect } from '@wagmi/core'
import * as React from 'react'

import { useClient } from '../../context'
import { MutationConfig } from '../../types'
import { useMutation } from '../utils'

export type UseConnectArgs = Partial<ConnectArgs>

Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/hooks/accounts/useDisconnect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMutation } from '@tanstack/react-query'
import { disconnect } from '@wagmi/core'

import { useMutation } from '../utils'

export type UseDisconnectConfig = {
/** Function to invoke when an error is thrown while connecting. */
onError?: (error: Error, context: unknown) => void | Promise<unknown>
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/accounts/useSignMessage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useMutation } from '@tanstack/react-query'
import { SignMessageArgs, SignMessageResult, signMessage } from '@wagmi/core'
import * as React from 'react'

import { MutationConfig } from '../../types'
import { useMutation } from '../utils'

export type UseSignMessageArgs = Partial<SignMessageArgs>

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/accounts/useSignTypedData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMutation } from '@tanstack/react-query'
import {
SignTypedDataArgs,
SignTypedDataResult,
Expand All @@ -7,6 +6,7 @@ import {
import * as React from 'react'

import { MutationConfig } from '../../types'
import { useMutation } from '../utils'

export type UseSignTypedDataArgs = Partial<SignTypedDataArgs>

Expand Down
3 changes: 1 addition & 2 deletions packages/react/src/hooks/accounts/useSigner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useQueryClient } from '@tanstack/react-query'
import {
FetchSignerArgs,
FetchSignerResult,
Expand All @@ -9,7 +8,7 @@ import {
import * as React from 'react'

import { QueryConfig, QueryFunctionArgs } from '../../types'
import { useChainId, useQuery } from '../utils'
import { useChainId, useQuery, useQueryClient } from '../utils'

export type UseSignerConfig = Omit<
QueryConfig<FetchSignerResult, Error>,
Expand Down
3 changes: 1 addition & 2 deletions packages/react/src/hooks/accounts/useSwitchNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMutation } from '@tanstack/react-query'
import {
SwitchNetworkArgs,
SwitchNetworkResult,
Expand All @@ -8,7 +7,7 @@ import * as React from 'react'

import { useClient } from '../../context'
import { MutationConfig } from '../../types'
import { useForceUpdate } from '../utils'
import { useForceUpdate, useMutation } from '../utils'

export type UseSwitchNetworkArgs = Partial<SwitchNetworkArgs>

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/contracts/useContractWrite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMutation } from '@tanstack/react-query'
import {
WriteContractArgs,
WriteContractPreparedArgs,
Expand All @@ -8,6 +7,7 @@ import {
import * as React from 'react'

import { MutationConfig } from '../../types'
import { useMutation } from '../utils'

export type UseContractWriteArgs = Omit<WriteContractArgs, 'request' | 'type'> &
(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMutation } from '@tanstack/react-query'
import {
DeprecatedWriteContractConfig,
DeprecatedWriteContractResult,
Expand All @@ -7,6 +6,7 @@ import {
import * as React from 'react'

import { MutationConfig } from '../../types'
import { useMutation } from '../utils'

export type UseDeprecatedContractWriteArgs = DeprecatedWriteContractConfig
export type UseDeprecatedContractWriteMutationArgs = Pick<
Expand Down
9 changes: 8 additions & 1 deletion packages/react/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ export {
useWaitForTransaction,
} from './transactions'

export { useChainId, useBaseQuery, useQuery, useInfiniteQuery } from './utils'
export {
useChainId,
useBaseQuery,
useQuery,
useInfiniteQuery,
useMutation,
useQueryClient,
} from './utils'
3 changes: 1 addition & 2 deletions packages/react/src/hooks/network-status/useBlockNumber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useQueryClient } from '@tanstack/react-query'
import {
FetchBlockNumberArgs,
FetchBlockNumberResult,
Expand All @@ -9,7 +8,7 @@ import * as React from 'react'

import { QueryConfig, QueryFunctionArgs } from '../../types'
import { useProvider, useWebSocketProvider } from '../providers'
import { useChainId, useQuery } from '../utils'
import { useChainId, useQuery, useQueryClient } from '../utils'

type UseBlockNumberArgs = Partial<FetchBlockNumberArgs> & {
/** Function fires when a new block is created */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMutation } from '@tanstack/react-query'
import {
DeprecatedSendTransactionArgs,
DeprecatedSendTransactionResult,
Expand All @@ -7,6 +6,7 @@ import {
import * as React from 'react'

import { MutationConfig } from '../../types'
import { useMutation } from '../utils'

export type UseDeprecatedSendTransactionArgs =
Partial<DeprecatedSendTransactionArgs>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMutation } from '@tanstack/react-query'
import {
SendTransactionArgs,
SendTransactionPreparedRequest,
Expand All @@ -9,6 +8,7 @@ import {
import * as React from 'react'

import { MutationConfig } from '../../types'
import { useMutation } from '../utils'

export type UseSendTransactionArgs = Omit<
SendTransactionArgs,
Expand Down
8 changes: 7 additions & 1 deletion packages/react/src/hooks/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export { useBaseQuery, useQuery, useInfiniteQuery } from './query'
export {
useBaseQuery,
useQuery,
useInfiniteQuery,
useMutation,
useQueryClient,
} from './query'
export { useChainId } from './useChainId'
export { useForceUpdate } from './useForceUpdate'
export { useInvalidateOnBlock } from './useInvalidateOnBlock'
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/hooks/utils/query/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { useBaseQuery } from './useBaseQuery'
export { useInfiniteQuery } from './useInfiniteQuery'
export { useMutation } from './useMutation'
export { useQuery } from './useQuery'
export { useQueryClient } from './useQueryClient'
3 changes: 2 additions & 1 deletion packages/react/src/hooks/utils/query/useInfiniteQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
UseInfiniteQueryOptions,
} from '@tanstack/react-query'

import { queryClientContext as context } from '../../../context'
import { useBaseQuery } from './useBaseQuery'
import { parseQueryArgs, trackResult } from './utils'

Expand Down Expand Up @@ -129,7 +130,7 @@ export function useInfiniteQuery<
): UseInfiniteQueryResult<TData, TError> {
const parsedOptions = parseQueryArgs(arg1, arg2, arg3)
const baseQuery = useBaseQuery(
parsedOptions,
{ context, ...parsedOptions },
InfiniteQueryObserver as typeof QueryObserver,
)

Expand Down
78 changes: 78 additions & 0 deletions packages/react/src/hooks/utils/query/useMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
MutationFunction,
MutationKey,
UseMutationOptions,
UseMutationResult,
parseMutationArgs,
useMutation as useMutation_,
} from '@tanstack/react-query'

import { queryClientContext as context } from '../../../context'

export function useMutation<
TData = unknown,
TError = unknown,
TVariables = void,
TContext = unknown,
>(
options: UseMutationOptions<TData, TError, TVariables, TContext>,
): UseMutationResult<TData, TError, TVariables, TContext>

export function useMutation<
TData = unknown,
TError = unknown,
TVariables = void,
TContext = unknown,
>(
mutationFn: MutationFunction<TData, TVariables>,
options?: Omit<
UseMutationOptions<TData, TError, TVariables, TContext>,
'mutationFn'
>,
): UseMutationResult<TData, TError, TVariables, TContext>

export function useMutation<
TData = unknown,
TError = unknown,
TVariables = void,
TContext = unknown,
>(
mutationKey: MutationKey,
options?: Omit<
UseMutationOptions<TData, TError, TVariables, TContext>,
'mutationKey'
>,
): UseMutationResult<TData, TError, TVariables, TContext>

export function useMutation<
TData = unknown,
TError = unknown,
TVariables = void,
TContext = unknown,
>(
mutationKey: MutationKey,
mutationFn?: MutationFunction<TData, TVariables>,
options?: Omit<
UseMutationOptions<TData, TError, TVariables, TContext>,
'mutationKey' | 'mutationFn'
>,
): UseMutationResult<TData, TError, TVariables, TContext>

export function useMutation<
TData = unknown,
TError = unknown,
TVariables = void,
TContext = unknown,
>(
arg1:
| MutationKey
| MutationFunction<TData, TVariables>
| UseMutationOptions<TData, TError, TVariables, TContext>,
arg2?:
| MutationFunction<TData, TVariables>
| UseMutationOptions<TData, TError, TVariables, TContext>,
arg3?: UseMutationOptions<TData, TError, TVariables, TContext>,
): UseMutationResult<TData, TError, TVariables, TContext> {
const options = parseMutationArgs(arg1, arg2, arg3)
return useMutation_({ context, ...options })
}
4 changes: 3 additions & 1 deletion packages/react/src/hooks/utils/query/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
UseQueryOptions,
} from '@tanstack/react-query'

import { queryClientContext as context } from '../../../context'

import { useBaseQuery } from './useBaseQuery'
import { parseQueryArgs, trackResult } from './utils'

Expand Down Expand Up @@ -113,7 +115,7 @@ export function useQuery<
arg3?: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
): UseQueryResult<TData, TError> {
const parsedOptions = parseQueryArgs(arg1, arg2, arg3)
const baseQuery = useBaseQuery(parsedOptions, QueryObserver)
const baseQuery = useBaseQuery({ context, ...parsedOptions }, QueryObserver)

const result = {
data: baseQuery.data,
Expand Down
5 changes: 5 additions & 0 deletions packages/react/src/hooks/utils/query/useQueryClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { useQueryClient as useQueryClient_ } from '@tanstack/react-query'

import { queryClientContext as context } from '../../../context'

export const useQueryClient = () => useQueryClient_({ context })
3 changes: 2 additions & 1 deletion packages/react/src/hooks/utils/useInvalidateOnBlock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { QueryKey, useQueryClient } from '@tanstack/react-query'
import { QueryKey } from '@tanstack/react-query'

import { useBlockNumber } from '../network-status'
import { useQueryClient } from './query'

export function useInvalidateOnBlock({
chainId,
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ it('should expose correct exports', () => {
"useEnsResolver",
"useFeeData",
"useInfiniteQuery",
"useMutation",
"useNetwork",
"useProvider",
"useQuery",
"useQueryClient",
"useSendTransaction",
"usePrepareContractWrite",
"usePrepareSendTransaction",
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export {
useEnsResolver,
useFeeData,
useInfiniteQuery,
useMutation,
useNetwork,
useProvider,
useQuery,
useQueryClient,
useSendTransaction,
usePrepareContractWrite,
usePrepareSendTransaction,
Expand Down

1 comment on commit 8ea29e2

@vercel
Copy link

@vercel vercel bot commented on 8ea29e2 Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.