Skip to content
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

feat: meta to Mutation #2906

Merged
merged 6 commits into from Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/core/mutation.ts
@@ -1,4 +1,4 @@
import type { MutationOptions, MutationStatus } from './types'
import type { MutationOptions, MutationStatus, MutationMeta } from './types'
import type { MutationCache } from './mutationCache'
import type { MutationObserver } from './mutationObserver'
import { getLogger } from './logger'
Expand All @@ -14,6 +14,7 @@ interface MutationConfig<TData, TError, TVariables, TContext> {
options: MutationOptions<TData, TError, TVariables, TContext>
defaultOptions?: MutationOptions<TData, TError, TVariables, TContext>
state?: MutationState<TData, TError, TVariables, TContext>
meta?: MutationMeta
}

export interface MutationState<
Expand Down Expand Up @@ -84,6 +85,7 @@ export class Mutation<
state: MutationState<TData, TError, TVariables, TContext>
options: MutationOptions<TData, TError, TVariables, TContext>
mutationId: number
meta: MutationMeta | undefined

private observers: MutationObserver<TData, TError, TVariables, TContext>[]
private mutationCache: MutationCache
Expand All @@ -98,6 +100,7 @@ export class Mutation<
this.mutationCache = config.mutationCache
this.observers = []
this.state = config.state || getDefaultState()
this.meta = config.meta
}

setState(state: MutationState<TData, TError, TVariables, TContext>): void {
Expand Down
1 change: 1 addition & 0 deletions src/core/mutationCache.ts
Expand Up @@ -52,6 +52,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
defaultOptions: options.mutationKey
? client.getMutationDefaults(options.mutationKey)
: undefined,
meta: options.meta,
})

this.add(mutation)
Expand Down
3 changes: 3 additions & 0 deletions src/core/types.ts
Expand Up @@ -501,6 +501,8 @@ export type MutationKey = string | readonly unknown[]

export type MutationStatus = 'idle' | 'loading' | 'success' | 'error'

export type MutationMeta = Record<string, unknown>

export type MutationFunction<TData = unknown, TVariables = unknown> = (
variables: TVariables
) => Promise<TData>
Expand Down Expand Up @@ -536,6 +538,7 @@ export interface MutationOptions<
retry?: RetryValue<TError>
retryDelay?: RetryDelayValue<TError>
_defaulted?: boolean
meta?: MutationMeta
}

export interface MutationObserverOptions<
Expand Down
61 changes: 61 additions & 0 deletions src/react/tests/useMutation.test.tsx
Expand Up @@ -519,4 +519,65 @@ describe('useMutation', () => {

consoleMock.mockRestore()
})

it('should pass meta to mutation', async () => {
const consoleMock = mockConsoleError()

const errorMock = jest.fn()
const successMock = jest.fn()

const queryClientAy = new QueryClient({
mutationCache: new MutationCache({
onSuccess: (_, __, ___, mutation) => {
successMock(mutation.meta?.metaSuccessMessage)
},
onError: (_, __, ___, mutation) => {
errorMock(mutation.meta?.metaErrorMessage)
},
}),
})

const metaSuccessMessage = 'mutation succeeded'
const metaErrorMessage = 'mutation failed'

function Page() {
const { mutate: succeed, isSuccess } = useMutation(async () => '', {
meta: { metaSuccessMessage },
})
const { mutate: error, isError } = useMutation(
async () => {
throw new Error('')
},
{
meta: { metaErrorMessage },
}
)

return (
<div>
<button onClick={() => succeed()}>succeed</button>
<button onClick={() => error()}>error</button>
{isSuccess && <div>successTest</div>}
{isError && <div>errorTest</div>}
</div>
)
}

const { getByText, queryByText } = renderWithClient(queryClientAy, <Page />)

fireEvent.click(getByText('succeed'))
fireEvent.click(getByText('error'))

await waitFor(() => {
expect(queryByText('successTest')).not.toBeNull()
expect(queryByText('errorTest')).not.toBeNull()
})

expect(successMock).toHaveBeenCalledTimes(1)
expect(successMock).toHaveBeenCalledWith(metaSuccessMessage)
expect(errorMock).toHaveBeenCalledTimes(1)
expect(errorMock).toHaveBeenCalledWith(metaErrorMessage)

consoleMock.mockRestore()
})
})
2 changes: 2 additions & 0 deletions src/react/types.ts
Expand Up @@ -9,6 +9,7 @@ import {
QueryKey,
MutationFunction,
MutateOptions,
MutationMeta,
} from '../core/types'

export interface UseBaseQueryOptions<
Expand Down Expand Up @@ -97,6 +98,7 @@ export interface UseMutationOptions<
retry?: RetryValue<TError>
retryDelay?: RetryDelayValue<TError>
useErrorBoundary?: boolean | ((error: TError) => boolean)
meta?: MutationMeta
}

export type UseMutateFunction<
Expand Down