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 all 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"
}
}
56 changes: 38 additions & 18 deletions dev-test/githunt/types.react-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,13 @@ export const CommentDocument = `
}
}
${CommentsPageCommentFragmentDoc}`;

export const useCommentQuery = <TData = CommentQuery, TError = unknown>(
dataSource: { endpoint: string; fetchParams?: RequestInit },
variables: CommentQueryVariables,
options?: UseQueryOptions<CommentQuery, TError, TData>,
) =>
useQuery<CommentQuery, TError, TData>(
) => {
return useQuery<CommentQuery, TError, TData>(
['Comment', variables],
fetcher<CommentQuery, CommentQueryVariables>(
dataSource.endpoint,
Expand All @@ -475,12 +476,14 @@ export const useCommentQuery = <TData = CommentQuery, TError = unknown>(
),
options,
);
};

export const useInfiniteCommentQuery = <TData = CommentQuery, TError = unknown>(
dataSource: { endpoint: string; fetchParams?: RequestInit },
variables: CommentQueryVariables,
options?: UseInfiniteQueryOptions<CommentQuery, TError, TData>,
) =>
useInfiniteQuery<CommentQuery, TError, TData>(
) => {
return useInfiniteQuery<CommentQuery, TError, TData>(
['Comment.infinite', variables],
metaData =>
fetcher<CommentQuery, CommentQueryVariables>(
Expand All @@ -491,6 +494,7 @@ export const useInfiniteCommentQuery = <TData = CommentQuery, TError = unknown>(
)(),
options,
);
};

export const CurrentUserForProfileDocument = `
query CurrentUserForProfile {
Expand All @@ -500,12 +504,13 @@ export const CurrentUserForProfileDocument = `
}
}
`;

export const useCurrentUserForProfileQuery = <TData = CurrentUserForProfileQuery, TError = unknown>(
dataSource: { endpoint: string; fetchParams?: RequestInit },
variables?: CurrentUserForProfileQueryVariables,
options?: UseQueryOptions<CurrentUserForProfileQuery, TError, TData>,
) =>
useQuery<CurrentUserForProfileQuery, TError, TData>(
) => {
return useQuery<CurrentUserForProfileQuery, TError, TData>(
variables === undefined ? ['CurrentUserForProfile'] : ['CurrentUserForProfile', variables],
fetcher<CurrentUserForProfileQuery, CurrentUserForProfileQueryVariables>(
dataSource.endpoint,
Expand All @@ -515,15 +520,17 @@ export const useCurrentUserForProfileQuery = <TData = CurrentUserForProfileQuery
),
options,
);
};

export const useInfiniteCurrentUserForProfileQuery = <
TData = CurrentUserForProfileQuery,
TError = unknown,
>(
dataSource: { endpoint: string; fetchParams?: RequestInit },
variables?: CurrentUserForProfileQueryVariables,
options?: UseInfiniteQueryOptions<CurrentUserForProfileQuery, TError, TData>,
) =>
useInfiniteQuery<CurrentUserForProfileQuery, TError, TData>(
) => {
return useInfiniteQuery<CurrentUserForProfileQuery, TError, TData>(
variables === undefined
? ['CurrentUserForProfile.infinite']
: ['CurrentUserForProfile.infinite', variables],
Expand All @@ -536,6 +543,7 @@ export const useInfiniteCurrentUserForProfileQuery = <
)(),
options,
);
};

export const FeedDocument = `
query Feed($type: FeedType!, $offset: Int, $limit: Int) {
Expand All @@ -547,12 +555,13 @@ export const FeedDocument = `
}
}
${FeedEntryFragmentDoc}`;

export const useFeedQuery = <TData = FeedQuery, TError = unknown>(
dataSource: { endpoint: string; fetchParams?: RequestInit },
variables: FeedQueryVariables,
options?: UseQueryOptions<FeedQuery, TError, TData>,
) =>
useQuery<FeedQuery, TError, TData>(
) => {
return useQuery<FeedQuery, TError, TData>(
['Feed', variables],
fetcher<FeedQuery, FeedQueryVariables>(
dataSource.endpoint,
Expand All @@ -562,12 +571,14 @@ export const useFeedQuery = <TData = FeedQuery, TError = unknown>(
),
options,
);
};

export const useInfiniteFeedQuery = <TData = FeedQuery, TError = unknown>(
dataSource: { endpoint: string; fetchParams?: RequestInit },
variables: FeedQueryVariables,
options?: UseInfiniteQueryOptions<FeedQuery, TError, TData>,
) =>
useInfiniteQuery<FeedQuery, TError, TData>(
) => {
return useInfiniteQuery<FeedQuery, TError, TData>(
['Feed.infinite', variables],
metaData =>
fetcher<FeedQuery, FeedQueryVariables>(
Expand All @@ -578,6 +589,7 @@ export const useInfiniteFeedQuery = <TData = FeedQuery, TError = unknown>(
)(),
options,
);
};

export const SubmitRepositoryDocument = `
mutation submitRepository($repoFullName: String!) {
Expand All @@ -586,6 +598,7 @@ export const SubmitRepositoryDocument = `
}
}
`;

export const useSubmitRepositoryMutation = <TError = unknown, TContext = unknown>(
dataSource: { endpoint: string; fetchParams?: RequestInit },
options?: UseMutationOptions<
Expand All @@ -594,8 +607,8 @@ export const useSubmitRepositoryMutation = <TError = unknown, TContext = unknown
SubmitRepositoryMutationVariables,
TContext
>,
) =>
useMutation<SubmitRepositoryMutation, TError, SubmitRepositoryMutationVariables, TContext>(
) => {
return useMutation<SubmitRepositoryMutation, TError, SubmitRepositoryMutationVariables, TContext>(
['submitRepository'],
(variables?: SubmitRepositoryMutationVariables) =>
fetcher<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>(
Expand All @@ -606,13 +619,16 @@ export const useSubmitRepositoryMutation = <TError = unknown, TContext = unknown
)(),
options,
);
};

export const SubmitCommentDocument = `
mutation submitComment($repoFullName: String!, $commentContent: String!) {
submitComment(repoFullName: $repoFullName, commentContent: $commentContent) {
...CommentsPageComment
}
}
${CommentsPageCommentFragmentDoc}`;

export const useSubmitCommentMutation = <TError = unknown, TContext = unknown>(
dataSource: { endpoint: string; fetchParams?: RequestInit },
options?: UseMutationOptions<
Expand All @@ -621,8 +637,8 @@ export const useSubmitCommentMutation = <TError = unknown, TContext = unknown>(
SubmitCommentMutationVariables,
TContext
>,
) =>
useMutation<SubmitCommentMutation, TError, SubmitCommentMutationVariables, TContext>(
) => {
return useMutation<SubmitCommentMutation, TError, SubmitCommentMutationVariables, TContext>(
['submitComment'],
(variables?: SubmitCommentMutationVariables) =>
fetcher<SubmitCommentMutation, SubmitCommentMutationVariables>(
Expand All @@ -633,6 +649,8 @@ export const useSubmitCommentMutation = <TError = unknown, TContext = unknown>(
)(),
options,
);
};

export const VoteDocument = `
mutation vote($repoFullName: String!, $type: VoteType!) {
vote(repoFullName: $repoFullName, type: $type) {
Expand All @@ -644,11 +662,12 @@ export const VoteDocument = `
}
}
`;

export const useVoteMutation = <TError = unknown, TContext = unknown>(
dataSource: { endpoint: string; fetchParams?: RequestInit },
options?: UseMutationOptions<VoteMutation, TError, VoteMutationVariables, TContext>,
) =>
useMutation<VoteMutation, TError, VoteMutationVariables, TContext>(
) => {
return useMutation<VoteMutation, TError, VoteMutationVariables, TContext>(
['vote'],
(variables?: VoteMutationVariables) =>
fetcher<VoteMutation, VoteMutationVariables>(
Expand All @@ -659,3 +678,4 @@ export const useVoteMutation = <TError = unknown, TContext = unknown>(
)(),
options,
);
};
86 changes: 51 additions & 35 deletions packages/plugins/typescript/react-query/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,12 @@ export type HardcodedFetch = { endpoint: string; fetchParams?: string | Record<s
export type CustomFetch = { func: string; isReactHook?: boolean } | string;
export type GraphQlRequest = 'graphql-request' | { clientImportPath: string };

/**
* @description This plugin generates `React-Query` Hooks with TypeScript typings.
*
* It extends the basic TypeScript plugins: `@graphql-codegen/typescript`, `@graphql-codegen/typescript-operations` - and thus shares a similar configuration.
*
* > **If you are using the `react-query` package instead of the `@tanstack/react-query` package in your project, please set the `legacyMode` option to `true`.**
*
*/
export interface ReactQueryRawPluginConfig
extends Omit<
RawClientSideBasePluginConfig,
| 'documentMode'
| 'noGraphQLTag'
| 'gqlImport'
| 'documentNodeImport'
| 'noExport'
| 'importOperationTypesFrom'
| 'importDocumentNodeExternallyFrom'
| 'useTypeImports'
| 'legacyMode'
> {
export interface BaseReactQueryPluginConfig {
/**
* @description Customize the fetcher you wish to use in the generated file. React-Query is agnostic to the data-fetching layer, so you should provide it, or use a custom one.
*
* The following options are available to use:
*
* - 'fetch' - requires you to specify endpoint and headers on each call, and uses `fetch` to do the actual http call.
* - `{ endpoint: string, fetchParams: RequestInit }`: hardcode your endpoint and fetch options into the generated output, using the environment `fetch` method. You can also use `process.env.MY_VAR` as endpoint or header value.
* - `file#identifier` - You can use custom fetcher method that should implement the exported `ReactQueryFetcher` interface. Example: `./my-fetcher#myCustomFetcher`.
* - `graphql-request`: Will generate each hook with `client` argument, where you should pass your own `GraphQLClient` (created from `graphql-request`).
* @default unknown
* @description Changes the default "TError" generic type.
*/
fetcher?: 'fetch' | HardcodedFetch | GraphQlRequest | CustomFetch;
errorType?: string;

/**
* @default false
Expand Down Expand Up @@ -98,23 +72,30 @@ export interface ReactQueryRawPluginConfig
exposeFetcher?: boolean;

/**
* @default unknown
* @description Changes the default "TError" generic type.
* @default false
* @description Adds an Infinite Query along side the standard one
*/
errorType?: string;
addInfiniteQuery?: boolean;

/**
* @default false
* @description Adds an Infinite Query along side the standard one
* @description Adds a Suspense Query along side the standard one
*/
addInfiniteQuery?: boolean;
addSuspenseQuery?: boolean;

/**
* @default false
* @description If true, it imports `react-query` not `@tanstack/react-query`, default is false.
* @deprecated Please use `reactQueryVersion` instead.
*/
legacyMode?: boolean;

/**
* @default 4
* @description The version of react-query to use. Will override the legacyMode option.
*/
reactQueryVersion?: 3 | 4 | 5;

/**
* @default empty
* @description Add custom import for react-query.
Expand All @@ -126,3 +107,38 @@ export interface ReactQueryRawPluginConfig
*/
reactQueryImportFrom?: string;
}

/**
* @description This plugin generates `React-Query` Hooks with TypeScript typings.
*
* It extends the basic TypeScript plugins: `@graphql-codegen/typescript`, `@graphql-codegen/typescript-operations` - and thus shares a similar configuration.
*
* > **If you are using the `react-query` package instead of the `@tanstack/react-query` package in your project, please set the `legacyMode` option to `true`.**
*
*/
export interface ReactQueryRawPluginConfig
extends Omit<
RawClientSideBasePluginConfig,
| 'documentMode'
| 'noGraphQLTag'
| 'gqlImport'
| 'documentNodeImport'
| 'noExport'
| 'importOperationTypesFrom'
| 'importDocumentNodeExternallyFrom'
| 'useTypeImports'
| 'legacyMode'
>,
BaseReactQueryPluginConfig {
/**
* @description Customize the fetcher you wish to use in the generated file. React-Query is agnostic to the data-fetching layer, so you should provide it, or use a custom one.
*
* The following options are available to use:
*
* - 'fetch' - requires you to specify endpoint and headers on each call, and uses `fetch` to do the actual http call.
* - `{ endpoint: string, fetchParams: RequestInit }`: hardcode your endpoint and fetch options into the generated output, using the environment `fetch` method. You can also use `process.env.MY_VAR` as endpoint or header value.
* - `file#identifier` - You can use custom fetcher method that should implement the exported `ReactQueryFetcher` interface. Example: `./my-fetcher#myCustomFetcher`.
* - `graphql-request`: Will generate each hook with `client` argument, where you should pass your own `GraphQLClient` (created from `graphql-request`).
*/
fetcher?: 'fetch' | HardcodedFetch | GraphQlRequest | CustomFetch;
}
Loading