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: Add shouldSplitQueryKey property #1585

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ export type NormalizedQueryOptions = {
shouldExportMutatorHooks?: boolean;
shouldExportHttpClient?: boolean;
shouldExportQueryKey?: boolean;
shouldSplitQueryKey?: boolean;
signal?: boolean;
version?: 3 | 4 | 5;
};
Expand All @@ -484,6 +485,7 @@ export type QueryOptions = {
shouldExportMutatorHooks?: boolean;
shouldExportHttpClient?: boolean;
shouldExportQueryKey?: boolean;
shouldSplitQueryKey?: boolean;
signal?: boolean;
version?: 3 | 4 | 5;
};
Expand Down
7 changes: 4 additions & 3 deletions packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1182,9 +1182,10 @@ const generateQueryHook = async (
'implementation',
);

const routeString = isVue(outputClient)
? 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 = `${
Expand Down
1 change: 1 addition & 0 deletions packages/query/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const normalizeQueryOptions = (
...(queryOptions.shouldExportHttpClient
? { shouldExportHttpClient: true }
: {}),
...(queryOptions.shouldSplitQueryKey ? { shouldSplitQueryKey: true } : {}),
};
};

Expand Down
3 changes: 3 additions & 0 deletions samples/react-query/basic/orval.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export default defineConfig({
path: './src/api/mutator/custom-instance.ts',
name: 'customInstance',
},
query: {
shouldSplitQueryKey: true,
},
operations: {
listPets: {
mock: {
Expand Down
23 changes: 23 additions & 0 deletions tests/configs/react-query.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export default defineConfig({
client: 'react-query',
mock: true,
headers: true,
override: {
query: {
shouldSplitQueryKey: true,
},
},
},
input: {
target: '../specifications/petstore.yaml',
Expand All @@ -25,6 +30,7 @@ export default defineConfig({
target: '../specifications/petstore.yaml',
},
},

petstoreSplit: {
output: {
target: '../generated/react-query/split/endpoints.ts',
Expand Down Expand Up @@ -409,4 +415,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',
},
},
});
21 changes: 21 additions & 0 deletions tests/specifications/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading