-
-
Notifications
You must be signed in to change notification settings - Fork 144
fix(tanstack-query): client context should include inside query/mutation keys #793
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,17 @@ | ||
| import type { ClientContext } from '@orpc/client' | ||
| import type { OperationKey, OperationKeyOptions, OperationType } from './types' | ||
|
|
||
| export function generateOperationKey<TType extends OperationType, TInput>( | ||
| /** | ||
| * @todo move TClientContext to second position + remove default in next major version | ||
| */ | ||
| export function generateOperationKey<TType extends OperationType, TInput, TClientContext extends ClientContext = ClientContext>( | ||
| path: readonly string[], | ||
| state: OperationKeyOptions<TType, TInput> = {}, | ||
| ): OperationKey<TType, TInput> { | ||
| state: OperationKeyOptions<TType, TInput, TClientContext> = {}, | ||
| ): OperationKey<TType, TInput, TClientContext> { | ||
| return [path, { | ||
| ...state.input !== undefined ? { input: state.input } : {}, | ||
| ...state.type !== undefined ? { type: state.type } : {}, | ||
| ...state.context !== undefined ? { context: state.context } : {}, | ||
| ...state.fnOptions !== undefined ? { fnOptions: state.fnOptions } : {}, | ||
| ...state.input !== undefined ? { input: state.input } : {}, | ||
| } as any] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import type { | |
| experimental_StreamedQueryOutput, | ||
| InfiniteOptionsBase, | ||
| InfiniteOptionsIn, | ||
| MutationKeyOptions, | ||
| MutationOptions, | ||
| MutationOptionsIn, | ||
| OperationContext, | ||
|
|
@@ -39,7 +40,7 @@ export interface ProcedureUtils<TClientContext extends ClientContext, TInput, TO | |
| */ | ||
| queryKey( | ||
| ...rest: MaybeOptionalOptions< | ||
| QueryKeyOptions<TInput> | ||
| QueryKeyOptions<TInput, TClientContext> | ||
| > | ||
| ): DataTag<QueryKey, TOutput, TError> | ||
|
|
||
|
|
@@ -61,7 +62,7 @@ export interface ProcedureUtils<TClientContext extends ClientContext, TInput, TO | |
| */ | ||
| experimental_streamedKey( | ||
| ...rest: MaybeOptionalOptions< | ||
| experimental_StreamedKeyOptions<TInput> | ||
| experimental_StreamedKeyOptions<TInput, TClientContext> | ||
| > | ||
| ): DataTag<QueryKey, experimental_StreamedQueryOutput<TOutput>, TError> | ||
|
|
||
|
|
@@ -85,7 +86,7 @@ export interface ProcedureUtils<TClientContext extends ClientContext, TInput, TO | |
| */ | ||
| experimental_liveKey( | ||
| ...rest: MaybeOptionalOptions< | ||
| QueryKeyOptions<TInput> | ||
| QueryKeyOptions<TInput, TClientContext> | ||
| > | ||
| ): DataTag<QueryKey, experimental_LiveQueryOutput<TOutput>, TError> | ||
|
|
||
|
|
@@ -110,7 +111,7 @@ export interface ProcedureUtils<TClientContext extends ClientContext, TInput, TO | |
| infiniteKey<UPageParam>( | ||
| options: Pick< | ||
| InfiniteOptionsIn<TClientContext, TInput, TOutput, TError, InfiniteData<TOutput, UPageParam>, UPageParam>, | ||
| 'input' | 'initialPageParam' | 'queryKey' | ||
| 'context' | 'initialPageParam' | 'input' | 'queryKey' | ||
| > | ||
| ): DataTag<QueryKey, InfiniteData<TOutput, UPageParam>, TError> | ||
|
|
||
|
|
@@ -129,9 +130,8 @@ export interface ProcedureUtils<TClientContext extends ClientContext, TInput, TO | |
| * @see {@link https://orpc.unnoq.com/docs/integrations/tanstack-query#query-mutation-key Tanstack Query/Mutation Key Docs} | ||
| */ | ||
| mutationKey( | ||
| options?: Pick< | ||
| MutationOptionsIn<TClientContext, TInput, TOutput, TError, any>, | ||
| 'mutationKey' | ||
| ...rest: MaybeOptionalOptions< | ||
| MutationKeyOptions<TClientContext> | ||
| > | ||
| ): DataTag<QueryKey, TOutput, TError> | ||
|
|
||
|
|
@@ -159,7 +159,10 @@ export function createProcedureUtils<TClientContext extends ClientContext, TInpu | |
| call: client, | ||
|
|
||
| queryKey(...[optionsIn = {} as any]) { | ||
| const queryKey = optionsIn.queryKey ?? generateOperationKey(options.path, { type: 'query', input: optionsIn.input }) | ||
| const queryKey = optionsIn.queryKey ?? generateOperationKey( | ||
| options.path, | ||
| { type: 'query', input: optionsIn.input, context: optionsIn.context }, | ||
| ) | ||
|
|
||
| return queryKey | ||
| }, | ||
|
|
@@ -191,7 +194,10 @@ export function createProcedureUtils<TClientContext extends ClientContext, TInpu | |
| }, | ||
|
|
||
| experimental_streamedKey(...[optionsIn = {} as any]) { | ||
| const queryKey = optionsIn.queryKey ?? generateOperationKey(options.path, { type: 'streamed', input: optionsIn.input, fnOptions: optionsIn.queryFnOptions }) | ||
| const queryKey = optionsIn.queryKey ?? generateOperationKey( | ||
| options.path, | ||
| { type: 'streamed', input: optionsIn.input, fnOptions: optionsIn.queryFnOptions, context: optionsIn.context }, | ||
|
Comment on lines
+198
to
+199
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider destructuring the const { input, queryFnOptions, context } = optionsIn;
const queryKey = optionsIn.queryKey ?? generateOperationKey(
options.path,
{ type: 'streamed', input, fnOptions: queryFnOptions, context },
) |
||
| ) | ||
|
|
||
| return queryKey | ||
| }, | ||
|
|
@@ -232,7 +238,10 @@ export function createProcedureUtils<TClientContext extends ClientContext, TInpu | |
| }, | ||
|
|
||
| experimental_liveKey(...[optionsIn = {} as any]) { | ||
| const queryKey = optionsIn.queryKey ?? generateOperationKey(options.path, { type: 'live', input: optionsIn.input }) | ||
| const queryKey = optionsIn.queryKey ?? generateOperationKey( | ||
| options.path, | ||
| { type: 'live', input: optionsIn.input, context: optionsIn.context }, | ||
|
Comment on lines
+242
to
+243
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider destructuring the const { input, context } = optionsIn;
const queryKey = optionsIn.queryKey ?? generateOperationKey(
options.path,
{ type: 'live', input, context },
) |
||
| ) | ||
|
|
||
| return queryKey | ||
| }, | ||
|
|
@@ -273,6 +282,7 @@ export function createProcedureUtils<TClientContext extends ClientContext, TInpu | |
| const queryKey = optionsIn.queryKey ?? generateOperationKey(options.path, { | ||
| type: 'infinite', | ||
| input: optionsIn.input === skipToken ? skipToken : optionsIn.input(optionsIn.initialPageParam) as any, | ||
| context: optionsIn.context, | ||
| }) | ||
|
|
||
| return queryKey as any | ||
|
|
@@ -305,7 +315,10 @@ export function createProcedureUtils<TClientContext extends ClientContext, TInpu | |
| }, | ||
|
|
||
| mutationKey(...[optionsIn = {} as any]) { | ||
| const mutationKey = optionsIn.mutationKey ?? generateOperationKey(options.path, { type: 'mutation' }) | ||
| const mutationKey = optionsIn.mutationKey ?? generateOperationKey( | ||
| options.path, | ||
| { type: 'mutation', context: optionsIn.context }, | ||
|
Comment on lines
+319
to
+320
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider destructuring the const { context } = optionsIn;
const mutationKey = optionsIn.mutationKey ?? generateOperationKey(
options.path,
{ type: 'mutation', context },
) |
||
| ) | ||
|
|
||
| return mutationKey | ||
| }, | ||
|
|
||
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.
Consider destructuring the
optionsInobject to improve readability and make it clear which properties are being used. This can also help prevent accidental usage of unintended properties.