Skip to content

fix: allow providing custom query key to TanStack Query #1709

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

Draft
wants to merge 2 commits into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
}) => {
const file = context.file({ id: plugin.name })!;

if (plugin.runtimeConfigPath) {
file.import({
module: file.relativePathToFile({
context,
id: plugin.runtimeConfigPath,
}),
name: createInfiniteParamsFn,
});
return;
}

Check warning on line 45 in packages/openapi-ts/src/plugins/@tanstack/query-core/infiniteQueryOptions.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@tanstack/query-core/infiniteQueryOptions.ts#L35-L45

Added lines #L35 - L45 were not covered by tests
const fn = compiler.constVariable({
expression: compiler.arrowFunction({
multiLine: true,
Expand Down
23 changes: 23 additions & 0 deletions packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
});

if (identifierCreateQueryKey.name) {
if (plugin.runtimeConfigPath) {
file.import({
module: file.relativePathToFile({
context,
id: plugin.runtimeConfigPath,
}),
name: identifierCreateQueryKey.name,
});
return;
}

Check warning on line 43 in packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts#L33-L43

Added lines #L33 - L43 were not covered by tests
const returnType = compiler.indexedAccessTypeNode({
indexType: compiler.literalTypeNode({
literal: compiler.ots.number(0),
Expand Down Expand Up @@ -250,6 +261,18 @@
}) => {
const file = context.file({ id: plugin.name })!;

if (plugin.runtimeConfigPath) {
file.import({
asType: true,
module: file.relativePathToFile({
context,
id: plugin.runtimeConfigPath,
}),
name: queryKeyName,
});
return;
}

Check warning on line 275 in packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts#L264-L275

Added lines #L264 - L275 were not covered by tests
const properties: Property[] = [
{
name: '_id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ export namespace TanStackQuery {
* @default false
*/
exportFromIndex?: boolean;
/**
* TODO
*/
runtimeConfigPath?: string;
};
}
70 changes: 70 additions & 0 deletions packages/openapi-ts/test/hey-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { client as _heyApiClient } from './generated/sample/client.gen';
import type { Options } from './generated/sample/sdk.gen';

export type QueryKey<TOptions extends Options> = [
Pick<TOptions, 'baseUrl' | 'body' | 'headers' | 'path' | 'query'> & {
_id: string;
_infinite?: boolean;
},
];

export const createQueryKey = <TOptions extends Options>(
id: string,
options?: TOptions,
infinite?: boolean,
): [QueryKey<TOptions>[0]] => {
const params: QueryKey<TOptions>[0] = {
_id: id,
baseUrl: (options?.client ?? _heyApiClient).getConfig().baseUrl,
} as QueryKey<TOptions>[0];
if (infinite) {
params._infinite = infinite;
}
if (options?.body) {
params.body = options.body;
}
if (options?.headers) {
params.headers = options.headers;
}
if (options?.path) {
params.path = options.path;
}
if (options?.query) {
params.query = options.query;
}
return [params];
};

export const createInfiniteParams = <
K extends Pick<QueryKey<Options>[0], 'body' | 'headers' | 'path' | 'query'>,
>(
queryKey: QueryKey<Options>,
page: K,
) => {
const params = queryKey[0];
if (page.body) {
params.body = {
...(queryKey[0].body as any),
...(page.body as any),
};
}
if (page.headers) {
params.headers = {
...queryKey[0].headers,
...page.headers,
};
}
if (page.path) {
params.path = {
...(queryKey[0].path as any),
...(page.path as any),
};
}
if (page.query) {
params.query = {
...(queryKey[0].query as any),
...(page.query as any),
};
}
return params as unknown as typeof page;
};
1 change: 1 addition & 0 deletions packages/openapi-ts/test/openapi-ts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default defineConfig({
// @ts-ignore
{
name: '@tanstack/react-query',
runtimeConfigPath: './hey-api.ts',
},
// @ts-ignore
{
Expand Down
Loading