From 5560162a9e965ce86d33a3d7054b4f684a2e0ce7 Mon Sep 17 00:00:00 2001 From: EgorPopovPP Date: Mon, 19 Aug 2024 03:10:23 +0300 Subject: [PATCH 1/2] feat: add shouldSplitQueryKey --- packages/core/src/types.ts | 2 ++ packages/query/src/index.ts | 2 +- packages/query/src/utils.ts | 3 +++ samples/react-query/basic/orval.config.ts | 3 +++ tests/configs/react-query.config.ts | 24 +++++++++++++++++++++++ tests/specifications/petstore.yaml | 21 ++++++++++++++++++++ 6 files changed, 54 insertions(+), 1 deletion(-) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 8f2c3d464..1dbd6b900 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -465,6 +465,7 @@ export type NormalizedQueryOptions = { shouldExportMutatorHooks?: boolean; shouldExportHttpClient?: boolean; shouldExportQueryKey?: boolean; + shouldSplitQueryKey?: boolean; signal?: boolean; version?: 3 | 4 | 5; }; @@ -484,6 +485,7 @@ export type QueryOptions = { shouldExportMutatorHooks?: boolean; shouldExportHttpClient?: boolean; shouldExportQueryKey?: boolean; + shouldSplitQueryKey?: boolean; signal?: boolean; version?: 3 | 4 | 5; }; diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index 2b8f37829..f95fca579 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -1182,7 +1182,7 @@ const generateQueryHook = async ( 'implementation', ); - const routeString = isVue(outputClient) + const routeString = isVue(outputClient) || override.query.shouldSplitQueryKey ? getRouteAsArray(route) // Note: this is required for reactivity to work, we will lose it if route params are converted into string, only as array they will be tracked // TODO: add tests for this : `\`${route}\``; diff --git a/packages/query/src/utils.ts b/packages/query/src/utils.ts index 49285ae38..1094b9237 100644 --- a/packages/query/src/utils.ts +++ b/packages/query/src/utils.ts @@ -58,6 +58,9 @@ export const normalizeQueryOptions = ( ...(queryOptions.shouldExportHttpClient ? { shouldExportHttpClient: true } : {}), + ...(queryOptions.shouldSplitQueryKey + ? { shouldSplitQueryKey: true } + : {}), }; }; diff --git a/samples/react-query/basic/orval.config.ts b/samples/react-query/basic/orval.config.ts index 479a4cd64..d064460c9 100644 --- a/samples/react-query/basic/orval.config.ts +++ b/samples/react-query/basic/orval.config.ts @@ -15,6 +15,9 @@ export default defineConfig({ path: './src/api/mutator/custom-instance.ts', name: 'customInstance', }, + query: { + shouldSplitQueryKey: true, + }, operations: { listPets: { mock: { diff --git a/tests/configs/react-query.config.ts b/tests/configs/react-query.config.ts index 872d829c5..105c593b3 100644 --- a/tests/configs/react-query.config.ts +++ b/tests/configs/react-query.config.ts @@ -8,6 +8,11 @@ export default defineConfig({ client: 'react-query', mock: true, headers: true, + override: { + query: { + shouldSplitQueryKey: true, + }, + } }, input: { target: '../specifications/petstore.yaml', @@ -25,6 +30,7 @@ export default defineConfig({ target: '../specifications/petstore.yaml', }, }, + petstoreSplit: { output: { target: '../generated/react-query/split/endpoints.ts', @@ -32,6 +38,7 @@ export default defineConfig({ mock: true, mode: 'split', client: 'react-query', + }, input: { target: '../specifications/petstore.yaml', @@ -409,4 +416,21 @@ export default defineConfig({ }, }, }, + splitedQueryKey: { + output: { + target: '../generated/react-query/splitedQueryKey/endpoints.ts', + schemas: '../generated/react-query/splitedQueryKey/model', + client: 'react-query', + mock: true, + headers: true, + override: { + query: { + shouldSplitQueryKey: true, + }, + }, + }, + input: { + target: '../specifications/petstore.yaml', + }, + }, }); diff --git a/tests/specifications/petstore.yaml b/tests/specifications/petstore.yaml index cde856ea4..bb7a88fc8 100644 --- a/tests/specifications/petstore.yaml +++ b/tests/specifications/petstore.yaml @@ -176,6 +176,27 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + /pets/status/{status}: + get: + summary: Finds Pets by status + operationId: findPetsByStatus + tags: + - pets + parameters: + - name: status + in: path + required: true + description: Status values that need to be considered for filter + schema: + type: string + enum: + - available + - pending + - sold + responses: + '200': + description: A paged array of pets + content: /health: get: summary: health check From 9464043959d2e763d3c037bdd7162c7582747372 Mon Sep 17 00:00:00 2001 From: EgorPopovPP Date: Mon, 19 Aug 2024 14:19:46 +0300 Subject: [PATCH 2/2] chore: format --- packages/query/src/index.ts | 7 ++++--- packages/query/src/utils.ts | 4 +--- tests/configs/react-query.config.ts | 3 +-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index f95fca579..d90d8b5c8 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -1182,9 +1182,10 @@ const generateQueryHook = async ( 'implementation', ); - const routeString = isVue(outputClient) || override.query.shouldSplitQueryKey - ? getRouteAsArray(route) // Note: this is required for reactivity to work, we will lose it if route params are converted into string, only as array they will be tracked // TODO: add tests for this - : `\`${route}\``; + const routeString = + isVue(outputClient) || override.query.shouldSplitQueryKey + ? getRouteAsArray(route) // Note: this is required for reactivity to work, we will lose it if route params are converted into string, only as array they will be tracked // TODO: add tests for this + : `\`${route}\``; // Note: do not unref() params in Vue - this will make key lose reactivity const queryKeyFn = `${ diff --git a/packages/query/src/utils.ts b/packages/query/src/utils.ts index 1094b9237..cceca706f 100644 --- a/packages/query/src/utils.ts +++ b/packages/query/src/utils.ts @@ -58,9 +58,7 @@ export const normalizeQueryOptions = ( ...(queryOptions.shouldExportHttpClient ? { shouldExportHttpClient: true } : {}), - ...(queryOptions.shouldSplitQueryKey - ? { shouldSplitQueryKey: true } - : {}), + ...(queryOptions.shouldSplitQueryKey ? { shouldSplitQueryKey: true } : {}), }; }; diff --git a/tests/configs/react-query.config.ts b/tests/configs/react-query.config.ts index 105c593b3..5ee9ab238 100644 --- a/tests/configs/react-query.config.ts +++ b/tests/configs/react-query.config.ts @@ -12,7 +12,7 @@ export default defineConfig({ query: { shouldSplitQueryKey: true, }, - } + }, }, input: { target: '../specifications/petstore.yaml', @@ -38,7 +38,6 @@ export default defineConfig({ mock: true, mode: 'split', client: 'react-query', - }, input: { target: '../specifications/petstore.yaml',