Skip to content

Commit 8ecabfc

Browse files
authored
feat(query): specify how querykey is filtered (orval-labs#3522)
* feat(query): specify how querykey is filtered * fix(query): not boolean by default
1 parent 146124d commit 8ecabfc

28 files changed

Lines changed: 1427 additions & 7 deletions

File tree

docs/content/docs/reference/configuration/output.mdx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,12 +910,13 @@ Export query keys.
910910
**Type:** `Boolean`
911911
**Default:** `false`
912912

913-
Add `.filter(Boolean)` to the query key. If false, `as const` is added instead.
913+
Add `.filter(q => q !== undefined)` to the query key. If false, `as const` is added instead.
914+
The filter can be adjusted with the `queryKeyFilter` option
914915

915916
When shouldFilterQueryKey is true:
916917
```ts title="endpoints.ts"
917918
export const getShowPetByIdQueryKey = (petId: string) => {
918-
return ['pets', petId].filter(Boolean);
919+
return ['pets', petId].filter(q => q !== undefined);
919920
};
920921
```
921922

@@ -926,6 +927,28 @@ export const getShowPetByIdQueryKey = (petId: string) => {
926927
};
927928
```
928929

930+
### queryKeyFilter
931+
932+
**Type:** `String`
933+
**Default:** `'q => q !== undefined'`
934+
935+
Adjusts how the queryKey is filtered, when `shouldFilterQueryKey` is `true`. Default is `'q => q !== undefined'`, which will result
936+
in it ending up beeing
937+
```ts title="endpoints.ts"
938+
.filter(q => q !== undefined)
939+
```
940+
941+
One option could be to only make it filter out all falsy keys:
942+
```ts title="orval.config.ts"
943+
shouldFilterQueryKey: true,
944+
queryKeyFilter: 'Boolean'
945+
```
946+
947+
which would result in the generated code being
948+
```ts title="endpoints.ts"
949+
.filter(Boolean)
950+
```
951+
929952
### shouldSplitQueryKey
930953

931954
**Type:** `Boolean`

packages/core/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ export interface NormalizedQueryOptions {
916916
shouldExportHttpClient?: boolean;
917917
shouldExportQueryKey?: boolean;
918918
shouldFilterQueryKey?: boolean;
919+
queryKeyFilter?: string;
919920
shouldSplitQueryKey?: boolean;
920921
useOperationIdAsQueryKey?: boolean;
921922
signal?: boolean;
@@ -944,6 +945,7 @@ export interface QueryOptions {
944945
shouldExportHttpClient?: boolean;
945946
shouldExportQueryKey?: boolean;
946947
shouldFilterQueryKey?: boolean;
948+
queryKeyFilter?: string;
947949
shouldSplitQueryKey?: boolean;
948950
useOperationIdAsQueryKey?: boolean;
949951
signal?: boolean;

packages/orval/src/utils/options.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,14 @@ function normalizeQueryOptions(
11261126
...(isNullish(queryOptions.shouldFilterQueryKey)
11271127
? {}
11281128
: { shouldFilterQueryKey: queryOptions.shouldFilterQueryKey }),
1129+
...(isNullish(globalOptions.queryKeyFilter)
1130+
? {}
1131+
: {
1132+
queryKeyFilter: globalOptions.queryKeyFilter,
1133+
}),
1134+
...(isNullish(queryOptions.queryKeyFilter)
1135+
? {}
1136+
: { queryKeyFilter: queryOptions.queryKeyFilter }),
11291137
...(isNullish(globalOptions.shouldExportHttpClient)
11301138
? {}
11311139
: {

packages/query/src/query-generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ ${override.query.shouldExportQueryKey ? 'export ' : ''}const ${queryOption.query
11641164
]
11651165
.filter((x) => !!x)
11661166
.join(', ')}
1167-
]${override.query.shouldFilterQueryKey ? '.filter(Boolean)' : ' as const'};
1167+
]${override.query.shouldFilterQueryKey ? `.filter(${override.query.queryKeyFilter ?? 'q => q !== undefined'})` : ' as const'};
11681168
}
11691169
`;
11701170
}

packages/query/src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export const normalizeQueryOptions = (
7070
...(queryOptions.shouldFilterQueryKey
7171
? { shouldFilterQueryKey: true }
7272
: {}),
73+
...(queryOptions.queryKeyFilter
74+
? { queryKeyFilter: queryOptions.queryKeyFilter }
75+
: {}),
7376
...(queryOptions.shouldExportHttpClient
7477
? { shouldExportHttpClient: true }
7578
: {}),

0 commit comments

Comments
 (0)