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(react-query): support react query v5 #434

Merged
merged 43 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
abcbe83
refactor: formatQueryParameters
neil585456525 Sep 28, 2023
983183b
refactor: rename
neil585456525 Sep 28, 2023
63946dc
refactor: generateFormattedMutationParameters
neil585456525 Sep 28, 2023
33b5fe0
refactor: rename
neil585456525 Sep 28, 2023
a44e46b
refactor: generateInfiniteQueryFormattedParameters
neil585456525 Sep 28, 2023
6e2c03c
feat: plugin config for support v5
neil585456525 Sep 28, 2023
c190135
fix: infiniteQuery option to required
neil585456525 Oct 2, 2023
b1d99e0
fix: infinite query type
neil585456525 Oct 2, 2023
1d26870
fix: Unnecessary semicolon
neil585456525 Oct 2, 2023
d5fe046
fix: test
neil585456525 Oct 2, 2023
ca36a52
refactor: use class inherent
neil585456525 Oct 2, 2023
5102f38
fix: v5 parameters type
neil585456525 Oct 2, 2023
52ce6ea
add: changeset
neil585456525 Oct 2, 2023
18f5d32
refactor: interface
neil585456525 Oct 12, 2023
39530e5
fix: typo
neil585456525 Oct 12, 2023
8789c7a
refactor: queryHook
neil585456525 Oct 12, 2023
0c6f67c
refactor: test & custom-mapper query
neil585456525 Oct 12, 2023
166c2f1
refactor: generateQuery
neil585456525 Oct 12, 2023
afd2cc2
refactor: interface
neil585456525 Oct 12, 2023
040249d
refactor: type
neil585456525 Oct 13, 2023
45c0e17
refactor: type
neil585456525 Oct 13, 2023
67799ea
refactor: fetcher-fetch mutation
neil585456525 Oct 13, 2023
b47f600
refactor: fetch-hardcode mutation
neil585456525 Oct 13, 2023
40f8787
refactor: fetch-graphql-request mutation
neil585456525 Oct 13, 2023
985b834
refactor: fetch-custom-mapper mutation
neil585456525 Oct 13, 2023
db04e44
refactor: fetch-hardcode infiniteQuery
neil585456525 Oct 13, 2023
206dea6
refactor: custom-mapper infiniteQuery
neil585456525 Oct 13, 2023
fc3ffae
refactor: fetch infiniteQuery
neil585456525 Oct 13, 2023
3f1a062
refactor: graphql-request infiniteQuery
neil585456525 Oct 13, 2023
5dbbcfb
fix: v5 queryOptions queryKey type issue
neil585456525 Oct 13, 2023
ddace77
refactor: move queryMethodMap to private
neil585456525 Oct 13, 2023
b226b46
fix: dev-test
neil585456525 Oct 13, 2023
2f1c7f1
fix: text break
neil585456525 Oct 13, 2023
c3d8785
edit: deprecated legacy mode flag
neil585456525 Oct 13, 2023
c81ea60
refactor: generate fn args format
neil585456525 Oct 13, 2023
f1ea720
add: v5 suspense support
neil585456525 Oct 13, 2023
adece6b
refactor: dry
neil585456525 Oct 13, 2023
d1735f7
fix: suspense queryKey
neil585456525 Oct 13, 2023
cbb32c3
fix: suspense optionsType
neil585456525 Oct 13, 2023
a2ac555
refactor: dry
neil585456525 Oct 14, 2023
b22c111
refactor: format
neil585456525 Oct 15, 2023
a36fe2a
fix: infiniteData type
neil585456525 Oct 15, 2023
753e929
Update packages/plugins/typescript/react-query/package.json
saihaj Oct 25, 2023
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
5 changes: 5 additions & 0 deletions .changeset/twelve-mugs-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-react-query': major
---

Support react-query v5
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
},
"typescript.tsdk": "node_modules/typescript/lib",
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
2 changes: 1 addition & 1 deletion packages/plugins/typescript/react-query/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@graphql-codegen/typescript-react-query",
"version": "5.0.0",
"version": "5.0.1",
saihaj marked this conversation as resolved.
Show resolved Hide resolved
"type": "module",
"description": "GraphQL Code Generator plugin for generating a ready-to-use React-Query Hooks based on GraphQL operations",
"repository": {
Expand Down
6 changes: 6 additions & 0 deletions packages/plugins/typescript/react-query/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ export interface ReactQueryRawPluginConfig
*/
legacyMode?: boolean;

/**
* @default 4
* @description The version of react-query to use. Default is 4. legacyMode must be false to active this flag.
*/
reactQueryVersion?: 4 | 5;
neil585456525 marked this conversation as resolved.
Show resolved Hide resolved

/**
* @default empty
* @description Add custom import for react-query.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import autoBind from 'auto-bind';
import { OperationDefinitionNode } from 'graphql';
import {
buildMapperImport,
Expand All @@ -6,23 +7,20 @@ import {
} from '@graphql-codegen/visitor-plugin-common';
import { CustomFetch } from './config.js';
import { FetcherRenderer } from './fetcher.js';
import {
generateInfiniteQueryKey,
generateMutationKey,
generateQueryKey,
} from './variables-generator.js';
import { ReactQueryVisitor } from './visitor.js';

export class CustomMapperFetcher implements FetcherRenderer {
export class CustomMapperFetcher extends FetcherRenderer {
private _mapper: ParsedMapper;
private _isReactHook: boolean;

constructor(private visitor: ReactQueryVisitor, customFetcher: CustomFetch) {
constructor(protected visitor: ReactQueryVisitor, customFetcher: CustomFetch) {
super(visitor);
if (typeof customFetcher === 'string') {
customFetcher = { func: customFetcher };
}
this._mapper = parseMapper(customFetcher.func);
this._isReactHook = customFetcher.isReactHook;
autoBind(this);
}

private getFetcherFnName(operationResultType: string, operationVariablesTypes: string): string {
Expand Down Expand Up @@ -54,13 +52,19 @@ export class CustomMapperFetcher implements FetcherRenderer {
operationVariablesTypes: string,
hasRequiredVariables: boolean,
): string {
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
const variables = this.generateInfiniteQueryVariablesSignature(
hasRequiredVariables,
operationVariablesTypes,
);

const hookConfig = this.visitor.queryMethodMap;
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);

const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
const options = this.generateInfiniteQueryOptionsSignature(
hookConfig.infiniteQuery.options,
operationResultType,
);

const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
const implHookOuter = this._isReactHook
Expand All @@ -79,9 +83,10 @@ export class CustomMapperFetcher implements FetcherRenderer {
) =>{
${implHookOuter}
return ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
${generateInfiniteQueryKey(node, hasRequiredVariables)},
${impl},
options
${this.generateInfiniteQueryFormattedParameters(
this.generateInfiniteQueryKey(node, hasRequiredVariables),
impl,
)}
)};`;
}

Expand Down Expand Up @@ -114,9 +119,10 @@ export class CustomMapperFetcher implements FetcherRenderer {
${options}
) =>
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
${generateQueryKey(node, hasRequiredVariables)},
${impl},
options
${this.generateQueryFormattedParameters(
this.generateQueryKey(node, hasRequiredVariables),
impl,
)}
);`;
}

Expand Down Expand Up @@ -146,9 +152,7 @@ export class CustomMapperFetcher implements FetcherRenderer {
${
hookConfig.mutation.hook
}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
${generateMutationKey(node)},
${impl},
options
${this.generateMutationFormattedParameters(this.generateMutationKey(node), impl)}
);`;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import autoBind from 'auto-bind';
import { OperationDefinitionNode } from 'graphql';
import { HardcodedFetch } from './config.js';
import { FetcherRenderer } from './fetcher.js';
import {
generateInfiniteQueryKey,
generateMutationKey,
generateQueryKey,
generateQueryVariablesSignature,
} from './variables-generator.js';
import { ReactQueryVisitor } from './visitor.js';

export class HardcodedFetchFetcher implements FetcherRenderer {
constructor(private visitor: ReactQueryVisitor, private config: HardcodedFetch) {}
export class HardcodedFetchFetcher extends FetcherRenderer {
constructor(protected visitor: ReactQueryVisitor, private config: HardcodedFetch) {
super(visitor);
autoBind(this);
}

private getEndpoint(): string {
try {
Expand Down Expand Up @@ -67,15 +65,18 @@ ${this.getFetchParams()}
operationVariablesTypes: string,
hasRequiredVariables: boolean,
): string {
const variables = generateQueryVariablesSignature(
const variables = this.generateInfiniteQueryVariablesSignature(
hasRequiredVariables,
operationVariablesTypes,
);
const hookConfig = this.visitor.queryMethodMap;
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);

const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
const options = this.generateInfiniteQueryOptionsSignature(
hookConfig.infiniteQuery.options,
operationResultType,
);

return `export const useInfinite${operationName} = <
TData = ${operationResultType},
Expand All @@ -85,9 +86,10 @@ ${this.getFetchParams()}
${options}
) =>
${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
${generateInfiniteQueryKey(node, hasRequiredVariables)},
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})(),
options
${this.generateInfiniteQueryFormattedParameters(
this.generateInfiniteQueryKey(node, hasRequiredVariables),
`(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`,
)}
);`;
}

Expand All @@ -99,7 +101,7 @@ ${this.getFetchParams()}
operationVariablesTypes: string,
hasRequiredVariables: boolean,
): string {
const variables = generateQueryVariablesSignature(
const variables = this.generateQueryVariablesSignature(
hasRequiredVariables,
operationVariablesTypes,
);
Expand All @@ -117,9 +119,10 @@ ${this.getFetchParams()}
${options}
) =>
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
${generateQueryKey(node, hasRequiredVariables)},
fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables),
options
${this.generateQueryFormattedParameters(
this.generateQueryKey(node, hasRequiredVariables),
`fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)`,
)}
);`;
}

Expand All @@ -145,9 +148,10 @@ ${this.getFetchParams()}
${
hookConfig.mutation.hook
}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
${generateMutationKey(node)},
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)(),
options
${this.generateMutationFormattedParameters(
this.generateMutationKey(node),
`(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)()`,
)}
);`;
}

Expand All @@ -159,7 +163,7 @@ ${this.getFetchParams()}
operationVariablesTypes: string,
hasRequiredVariables: boolean,
): string {
const variables = generateQueryVariablesSignature(
const variables = this.generateQueryVariablesSignature(
hasRequiredVariables,
operationVariablesTypes,
);
Expand Down
46 changes: 25 additions & 21 deletions packages/plugins/typescript/react-query/src/fetcher-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import autoBind from 'auto-bind';
import { OperationDefinitionNode } from 'graphql';
import { FetcherRenderer } from './fetcher.js';
import {
generateInfiniteQueryKey,
generateMutationKey,
generateQueryKey,
generateQueryVariablesSignature,
} from './variables-generator.js';
import { ReactQueryVisitor } from './visitor.js';

export class FetchFetcher implements FetcherRenderer {
constructor(private visitor: ReactQueryVisitor) {}
export class FetchFetcher extends FetcherRenderer {
constructor(protected visitor: ReactQueryVisitor) {
super(visitor);
autoBind(this);
}

generateFetcherImplementaion(): string {
return `
Expand Down Expand Up @@ -42,15 +40,18 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
operationVariablesTypes: string,
hasRequiredVariables: boolean,
): string {
const variables = generateQueryVariablesSignature(
const variables = this.generateInfiniteQueryVariablesSignature(
hasRequiredVariables,
operationVariablesTypes,
);
const hookConfig = this.visitor.queryMethodMap;
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);

const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
const options = this.generateInfiniteQueryOptionsSignature(
hookConfig.infiniteQuery.options,
operationResultType,
);

return `export const useInfinite${operationName} = <
TData = ${operationResultType},
Expand All @@ -61,9 +62,10 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
${options}
) =>
${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
${generateInfiniteQueryKey(node, hasRequiredVariables)},
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})(),
options
${this.generateInfiniteQueryFormattedParameters(
this.generateInfiniteQueryKey(node, hasRequiredVariables),
`(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`,
)}
);`;
}

Expand All @@ -75,7 +77,7 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
operationVariablesTypes: string,
hasRequiredVariables: boolean,
): string {
const variables = generateQueryVariablesSignature(
const variables = this.generateQueryVariablesSignature(
hasRequiredVariables,
operationVariablesTypes,
);
Expand All @@ -94,9 +96,10 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
${options}
) =>
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
${generateQueryKey(node, hasRequiredVariables)},
fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables),
options
${this.generateQueryFormattedParameters(
this.generateQueryKey(node, hasRequiredVariables),
`fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)`,
)}
);`;
}

Expand Down Expand Up @@ -125,9 +128,10 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
${
hookConfig.mutation.hook
}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
${generateMutationKey(node)},
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)(),
options
${this.generateMutationFormattedParameters(
this.generateMutationKey(node),
`(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)()`,
)}
);`;
}

Expand All @@ -139,7 +143,7 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
operationVariablesTypes: string,
hasRequiredVariables: boolean,
): string {
const variables = generateQueryVariablesSignature(
const variables = this.generateQueryVariablesSignature(
hasRequiredVariables,
operationVariablesTypes,
);
Expand Down
Loading