-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Query mutation options in core #10973
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
Open
benjavicente
wants to merge
3
commits into
TanStack:main
Choose a base branch
from
benjavicente:query-mutation-options-in-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/query-core': minor | ||
| --- | ||
|
|
||
| Add framework-agnostic `queryOptions` and `mutationOptions` helpers. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/query-core/src/__tests__/mutationOptions.test-d.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { describe, expectTypeOf, it } from 'vitest' | ||
| import { mutationOptions } from '../mutationOptions' | ||
| import type { CoreMutationOptions } from '../index' | ||
|
|
||
| describe('mutationOptions', () => { | ||
| it('should infer mutation data and variables', () => { | ||
| const options = mutationOptions({ | ||
| mutationFn: (variables: { id: string }) => Promise.resolve(variables.id), | ||
| onSuccess: (data, variables) => { | ||
| expectTypeOf(data).toEqualTypeOf<string>() | ||
| expectTypeOf(variables).toEqualTypeOf<{ id: string }>() | ||
| }, | ||
| }) | ||
|
|
||
| expectTypeOf(options.mutationFn) | ||
| .parameter(0) | ||
| .toEqualTypeOf<{ id: string }>() | ||
| type MutationFn = NonNullable<typeof options.mutationFn> | ||
| expectTypeOf<Awaited<ReturnType<MutationFn>>>().toEqualTypeOf<string>() | ||
| expectTypeOf(options).not.toHaveProperty('mutationKey') | ||
| }) | ||
|
|
||
| it('should preserve a required mutationKey', () => { | ||
| const options = mutationOptions({ | ||
| mutationKey: ['key'], | ||
| mutationFn: (variables: { id: string }) => Promise.resolve(variables.id), | ||
| }) | ||
|
|
||
| expectTypeOf(options.mutationKey).toEqualTypeOf<ReadonlyArray<unknown>>() | ||
| }) | ||
|
|
||
| it('should export the reusable mutation options type', () => { | ||
| expectTypeOf< | ||
| CoreMutationOptions<string, Error, { id: string }> | ||
| >().toHaveProperty('mutationFn') | ||
| }) | ||
|
|
||
| it('should not allow _defaulted', () => { | ||
| expectTypeOf(mutationOptions).parameter(0).not.toHaveProperty('_defaulted') | ||
|
|
||
| mutationOptions({ | ||
| mutationFn: () => Promise.resolve(5), | ||
| // @ts-expect-error _defaulted is an internal option | ||
| _defaulted: true, | ||
| }) | ||
| }) | ||
| }) |
102 changes: 102 additions & 0 deletions
102
packages/query-core/src/__tests__/queryOptions.test-d.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { describe, expectTypeOf, it } from 'vitest' | ||
| import { dataTagErrorSymbol, dataTagSymbol } from '../types' | ||
| import { skipToken } from '../utils' | ||
| import { QueryClient } from '../queryClient' | ||
| import { queryOptions } from '../queryOptions' | ||
|
|
||
| describe('queryOptions', () => { | ||
| it('should infer query data from queryFn', () => { | ||
| const options = queryOptions({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| }) | ||
| type QueryFn = Exclude<typeof options.queryFn, typeof skipToken | undefined> | ||
|
|
||
| expectTypeOf<Awaited<ReturnType<QueryFn>>>().toEqualTypeOf<number>() | ||
| expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<number>() | ||
| expectTypeOf(options.queryKey[dataTagErrorSymbol]).toEqualTypeOf<Error>() | ||
| }) | ||
|
|
||
| it('should tag queryKey with the query data type', () => { | ||
| const options = queryOptions({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| }) | ||
| const queryClient = new QueryClient() | ||
| const data = queryClient.getQueryData(options.queryKey) | ||
|
|
||
| expectTypeOf(data).toEqualTypeOf<number | undefined>() | ||
| }) | ||
|
|
||
| it('should type check values passed to setQueryData from the tagged queryKey', () => { | ||
| const options = queryOptions({ | ||
| queryKey: ['todo'], | ||
| queryFn: () => Promise.resolve({ id: '1', title: 'Do Laundry' }), | ||
| }) | ||
| const queryClient = new QueryClient() | ||
|
|
||
| queryClient.setQueryData(options.queryKey, { | ||
| id: '1', | ||
| title: 'Wash dishes', | ||
| }) | ||
|
|
||
| queryClient.setQueryData(options.queryKey, (prev) => { | ||
| expectTypeOf(prev).toEqualTypeOf< | ||
| { id: string; title: string } | undefined | ||
| >() | ||
| return prev | ||
| }) | ||
|
|
||
| // @ts-expect-error title should be a string | ||
| queryClient.setQueryData(options.queryKey, { id: '1', title: 1 }) | ||
|
|
||
| // @ts-expect-error title is required | ||
| queryClient.setQueryData(options.queryKey, { id: '1' }) | ||
| }) | ||
|
|
||
| it('should allow initialData without a queryFn', () => { | ||
| const options = queryOptions({ | ||
| queryKey: ['key'], | ||
| initialData: 1, | ||
| }) | ||
| const queryClient = new QueryClient() | ||
| const data = queryClient.getQueryData(options.queryKey) | ||
|
|
||
| expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<number>() | ||
| expectTypeOf(data).toEqualTypeOf<number | undefined>() | ||
| }) | ||
|
|
||
| it('should infer data when initialData is undefined', () => { | ||
| const options = queryOptions({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| initialData: undefined, | ||
| }) | ||
|
|
||
| expectTypeOf(options.initialData).toEqualTypeOf<undefined>() | ||
| expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<number>() | ||
| expectTypeOf(options.queryKey[dataTagErrorSymbol]).toEqualTypeOf<Error>() | ||
| }) | ||
|
|
||
| it('should infer a callable queryFn without skipToken', () => { | ||
| const options = queryOptions({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| enabled: false, | ||
| }) | ||
| type QueryFn = Exclude<typeof options.queryFn, undefined> | ||
|
|
||
| expectTypeOf<QueryFn>().not.toEqualTypeOf<typeof skipToken>() | ||
| expectTypeOf<Awaited<ReturnType<QueryFn>>>().toEqualTypeOf<number>() | ||
| expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<number>() | ||
| }) | ||
|
|
||
| it('should allow skipToken', () => { | ||
| const options = queryOptions({ | ||
| queryKey: ['key'], | ||
| queryFn: skipToken, | ||
| }) | ||
|
|
||
| expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<unknown>() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import type { | ||
| DefaultError, | ||
| MutationObserverOptions, | ||
| OmitKeyof, | ||
| WithRequired, | ||
| } from './types' | ||
|
|
||
| export type CoreMutationOptions< | ||
| TData = unknown, | ||
| TError = DefaultError, | ||
| TVariables = void, | ||
| TOnMutateResult = unknown, | ||
| > = OmitKeyof< | ||
| MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>, | ||
| '_defaulted' | ||
| > | ||
|
|
||
| export function mutationOptions< | ||
| TData = unknown, | ||
| TError = DefaultError, | ||
| TVariables = void, | ||
| TOnMutateResult = unknown, | ||
| >( | ||
| options: WithRequired< | ||
| CoreMutationOptions<TData, TError, TVariables, TOnMutateResult>, | ||
| 'mutationKey' | ||
| >, | ||
| ): WithRequired< | ||
| CoreMutationOptions<TData, TError, TVariables, TOnMutateResult>, | ||
| 'mutationKey' | ||
| > | ||
|
|
||
| export function mutationOptions< | ||
| TData = unknown, | ||
| TError = DefaultError, | ||
| TVariables = void, | ||
| TOnMutateResult = unknown, | ||
| >( | ||
| options: Omit< | ||
| CoreMutationOptions<TData, TError, TVariables, TOnMutateResult>, | ||
| 'mutationKey' | ||
| >, | ||
| ): Omit< | ||
| CoreMutationOptions<TData, TError, TVariables, TOnMutateResult>, | ||
| 'mutationKey' | ||
| > | ||
|
|
||
| export function mutationOptions< | ||
| TData = unknown, | ||
| TError = DefaultError, | ||
| TVariables = void, | ||
| TOnMutateResult = unknown, | ||
| >( | ||
| options: CoreMutationOptions<TData, TError, TVariables, TOnMutateResult>, | ||
| ): CoreMutationOptions<TData, TError, TVariables, TOnMutateResult> { | ||
| return options | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since adapters re-export the core and then export their own things, they won’t have access to the
queryOptionsfrom the core, right? is that on purpose?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I don't think someone wanting to define shared query options for multiple frameworks would import from a framework specific package.