@graphql-codegen/typescript-react-query 5.1.0-alpha-20231024141714-d4d2e5e06 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/fetcher-custom-mapper.js +31 -59
- package/cjs/fetcher-fetch-hardcoded.js +28 -56
- package/cjs/fetcher-fetch.js +34 -53
- package/cjs/fetcher-graphql-request.js +49 -83
- package/cjs/fetcher.js +220 -0
- package/cjs/index.js +5 -0
- package/cjs/visitor.js +47 -42
- package/esm/fetcher-custom-mapper.js +30 -59
- package/esm/fetcher-fetch-hardcoded.js +27 -56
- package/esm/fetcher-fetch.js +33 -53
- package/esm/fetcher-graphql-request.js +48 -83
- package/esm/fetcher.js +215 -0
- package/esm/index.js +5 -0
- package/esm/visitor.js +47 -42
- package/package.json +1 -1
- package/typings/config.d.cts +36 -23
- package/typings/config.d.ts +36 -23
- package/typings/fetcher-custom-mapper.d.cts +8 -9
- package/typings/fetcher-custom-mapper.d.ts +8 -9
- package/typings/fetcher-fetch-hardcoded.d.cts +8 -9
- package/typings/fetcher-fetch-hardcoded.d.ts +8 -9
- package/typings/fetcher-fetch.d.cts +8 -9
- package/typings/fetcher-fetch.d.ts +8 -9
- package/typings/fetcher-graphql-request.d.cts +8 -9
- package/typings/fetcher-graphql-request.d.ts +8 -9
- package/typings/fetcher.d.cts +73 -6
- package/typings/fetcher.d.ts +73 -6
- package/typings/visitor.d.cts +2 -27
- package/typings/visitor.d.ts +2 -27
- package/cjs/variables-generator.js +0 -45
- package/esm/variables-generator.js +0 -33
- package/typings/variables-generator.d.cts +0 -10
- package/typings/variables-generator.d.ts +0 -10
package/esm/fetcher-fetch.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import autoBind from 'auto-bind';
|
|
2
|
+
import { FetcherRenderer } from './fetcher.js';
|
|
3
|
+
export class FetchFetcher extends FetcherRenderer {
|
|
3
4
|
constructor(visitor) {
|
|
5
|
+
super(visitor);
|
|
4
6
|
this.visitor = visitor;
|
|
7
|
+
autoBind(this);
|
|
5
8
|
}
|
|
6
|
-
|
|
9
|
+
generateFetcherImplementation() {
|
|
7
10
|
return `
|
|
8
11
|
function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit, query: string, variables?: TVariables) {
|
|
9
12
|
return async (): Promise<TData> => {
|
|
@@ -25,67 +28,44 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
|
|
|
25
28
|
}
|
|
26
29
|
}`;
|
|
27
30
|
}
|
|
28
|
-
generateInfiniteQueryHook(
|
|
29
|
-
const variables =
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
34
|
-
return `export const useInfinite${operationName} = <
|
|
35
|
-
TData = ${operationResultType},
|
|
36
|
-
TError = ${this.visitor.config.errorType}
|
|
37
|
-
>(
|
|
31
|
+
generateInfiniteQueryHook(config, isSuspense = false) {
|
|
32
|
+
const { generateBaseInfiniteQueryHook, variables, options } = this.generateInfiniteQueryHelper(config, isSuspense);
|
|
33
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
34
|
+
return generateBaseInfiniteQueryHook({
|
|
35
|
+
implArguments: `
|
|
38
36
|
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
39
37
|
${variables},
|
|
40
38
|
${options}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})(),
|
|
45
|
-
options
|
|
46
|
-
);`;
|
|
39
|
+
`,
|
|
40
|
+
implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`,
|
|
41
|
+
});
|
|
47
42
|
}
|
|
48
|
-
generateQueryHook(
|
|
49
|
-
const variables =
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
54
|
-
return `export const use${operationName} = <
|
|
55
|
-
TData = ${operationResultType},
|
|
56
|
-
TError = ${this.visitor.config.errorType}
|
|
57
|
-
>(
|
|
43
|
+
generateQueryHook(config, isSuspense = false) {
|
|
44
|
+
const { generateBaseQueryHook, variables, options } = this.generateQueryHelper(config, isSuspense);
|
|
45
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
46
|
+
return generateBaseQueryHook({
|
|
47
|
+
implArguments: `
|
|
58
48
|
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
59
49
|
${variables},
|
|
60
50
|
${options}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables),
|
|
65
|
-
options
|
|
66
|
-
);`;
|
|
51
|
+
`,
|
|
52
|
+
implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)`,
|
|
53
|
+
});
|
|
67
54
|
}
|
|
68
|
-
generateMutationHook(
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
74
|
-
return `export const use${operationName} = <
|
|
75
|
-
TError = ${this.visitor.config.errorType},
|
|
76
|
-
TContext = unknown
|
|
77
|
-
>(
|
|
55
|
+
generateMutationHook(config) {
|
|
56
|
+
const { generateBaseMutationHook, variables, options } = this.generateMutationHelper(config);
|
|
57
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
58
|
+
return generateBaseMutationHook({
|
|
59
|
+
implArguments: `
|
|
78
60
|
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
79
61
|
${options}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)(),
|
|
84
|
-
options
|
|
85
|
-
);`;
|
|
62
|
+
`,
|
|
63
|
+
implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)()`,
|
|
64
|
+
});
|
|
86
65
|
}
|
|
87
|
-
generateFetcherFetch(
|
|
88
|
-
const
|
|
66
|
+
generateFetcherFetch(config) {
|
|
67
|
+
const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
|
|
68
|
+
const variables = this.generateQueryVariablesSignature(config);
|
|
89
69
|
return `\nuse${operationName}.fetcher = (dataSource: { endpoint: string, fetchParams?: RequestInit }, ${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables);`;
|
|
90
70
|
}
|
|
91
71
|
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import autoBind from 'auto-bind';
|
|
2
|
+
import { FetcherRenderer } from './fetcher.js';
|
|
3
|
+
export class GraphQLRequestClientFetcher extends FetcherRenderer {
|
|
3
4
|
constructor(visitor, config) {
|
|
5
|
+
super(visitor);
|
|
4
6
|
this.visitor = visitor;
|
|
5
7
|
this.clientPath = typeof config === 'object' ? config.clientImportPath : null;
|
|
8
|
+
autoBind(this);
|
|
6
9
|
}
|
|
7
|
-
|
|
10
|
+
generateFetcherImplementation() {
|
|
8
11
|
return this.clientPath
|
|
9
12
|
? `
|
|
10
13
|
function fetcher<TData, TVariables extends { [key: string]: any }>(query: string, variables?: TVariables, requestHeaders?: RequestInit['headers']) {
|
|
@@ -23,125 +26,87 @@ function fetcher<TData, TVariables extends { [key: string]: any }>(client: Graph
|
|
|
23
26
|
});
|
|
24
27
|
}`;
|
|
25
28
|
}
|
|
26
|
-
generateInfiniteQueryHook(
|
|
27
|
-
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
29
|
+
generateInfiniteQueryHook(config, isSuspense = false) {
|
|
28
30
|
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
29
31
|
if (this.clientPath)
|
|
30
32
|
this.visitor.imports.add(this.clientPath);
|
|
31
33
|
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
35
|
-
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
34
|
+
const { generateBaseInfiniteQueryHook, variables, options } = this.generateInfiniteQueryHelper(config, isSuspense);
|
|
35
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
36
36
|
return this.clientPath
|
|
37
|
-
?
|
|
38
|
-
|
|
39
|
-
TError = ${this.visitor.config.errorType}
|
|
40
|
-
>(
|
|
37
|
+
? generateBaseInfiniteQueryHook({
|
|
38
|
+
implArguments: `
|
|
41
39
|
pageParamKey: keyof ${operationVariablesTypes},
|
|
42
40
|
${variables},
|
|
43
41
|
${options},
|
|
44
42
|
headers?: RequestInit['headers']
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
);`
|
|
51
|
-
: `export const useInfinite${operationName} = <
|
|
52
|
-
TData = ${operationResultType},
|
|
53
|
-
TError = ${this.visitor.config.errorType}
|
|
54
|
-
>(
|
|
43
|
+
`,
|
|
44
|
+
implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, [pageParamKey]: metaData.pageParam}, headers)()`,
|
|
45
|
+
})
|
|
46
|
+
: generateBaseInfiniteQueryHook({
|
|
47
|
+
implArguments: `
|
|
55
48
|
client: GraphQLClient,
|
|
56
49
|
${variables},
|
|
57
50
|
${options},
|
|
58
51
|
headers?: RequestInit['headers']
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})}, headers)(),
|
|
63
|
-
options
|
|
64
|
-
);`;
|
|
52
|
+
`,
|
|
53
|
+
implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})}, headers)()`,
|
|
54
|
+
});
|
|
65
55
|
}
|
|
66
|
-
generateQueryHook(
|
|
67
|
-
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
56
|
+
generateQueryHook(config, isSuspense = false) {
|
|
68
57
|
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
69
58
|
if (this.clientPath)
|
|
70
59
|
this.visitor.imports.add(this.clientPath);
|
|
71
60
|
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
72
61
|
this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
76
|
-
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
62
|
+
const { generateBaseQueryHook, variables, options } = this.generateQueryHelper(config, isSuspense);
|
|
63
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
77
64
|
return this.clientPath
|
|
78
|
-
?
|
|
79
|
-
|
|
80
|
-
TError = ${this.visitor.config.errorType}
|
|
81
|
-
>(
|
|
65
|
+
? generateBaseQueryHook({
|
|
66
|
+
implArguments: `
|
|
82
67
|
${variables},
|
|
83
68
|
${options},
|
|
84
69
|
headers?: RequestInit['headers']
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
);`
|
|
91
|
-
: `export const use${operationName} = <
|
|
92
|
-
TData = ${operationResultType},
|
|
93
|
-
TError = ${this.visitor.config.errorType}
|
|
94
|
-
>(
|
|
70
|
+
`,
|
|
71
|
+
implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers)`,
|
|
72
|
+
})
|
|
73
|
+
: generateBaseQueryHook({
|
|
74
|
+
implArguments: `
|
|
95
75
|
client: GraphQLClient,
|
|
96
76
|
${variables},
|
|
97
77
|
${options},
|
|
98
78
|
headers?: RequestInit['headers']
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers),
|
|
103
|
-
options
|
|
104
|
-
);`;
|
|
79
|
+
`,
|
|
80
|
+
implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)`,
|
|
81
|
+
});
|
|
105
82
|
}
|
|
106
|
-
generateMutationHook(
|
|
107
|
-
const variables = `variables?: ${operationVariablesTypes}`;
|
|
83
|
+
generateMutationHook(config) {
|
|
108
84
|
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
109
85
|
if (this.clientPath)
|
|
110
86
|
this.visitor.imports.add(this.clientPath);
|
|
111
87
|
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
115
|
-
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
88
|
+
const { generateBaseMutationHook, variables, options } = this.generateMutationHelper(config);
|
|
89
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
116
90
|
return this.clientPath
|
|
117
|
-
?
|
|
118
|
-
|
|
119
|
-
TContext = unknown
|
|
120
|
-
>(
|
|
91
|
+
? generateBaseMutationHook({
|
|
92
|
+
implArguments: `
|
|
121
93
|
${options},
|
|
122
94
|
headers?: RequestInit['headers']
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
);`
|
|
129
|
-
: `export const use${operationName} = <
|
|
130
|
-
TError = ${this.visitor.config.errorType},
|
|
131
|
-
TContext = unknown
|
|
132
|
-
>(
|
|
95
|
+
`,
|
|
96
|
+
implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers)()`,
|
|
97
|
+
})
|
|
98
|
+
: generateBaseMutationHook({
|
|
99
|
+
implArguments: `
|
|
133
100
|
client: GraphQLClient,
|
|
134
101
|
${options},
|
|
135
102
|
headers?: RequestInit['headers']
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)(),
|
|
140
|
-
options
|
|
141
|
-
);`;
|
|
103
|
+
`,
|
|
104
|
+
implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)()`,
|
|
105
|
+
});
|
|
142
106
|
}
|
|
143
|
-
generateFetcherFetch(
|
|
144
|
-
const
|
|
107
|
+
generateFetcherFetch(config) {
|
|
108
|
+
const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
|
|
109
|
+
const variables = this.generateQueryVariablesSignature(config);
|
|
145
110
|
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
146
111
|
if (this.clientPath)
|
|
147
112
|
this.visitor.imports.add(this.clientPath);
|
package/esm/fetcher.js
CHANGED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import autoBind from 'auto-bind';
|
|
2
|
+
export class FetcherRenderer {
|
|
3
|
+
constructor(visitor) {
|
|
4
|
+
this.visitor = visitor;
|
|
5
|
+
autoBind(this);
|
|
6
|
+
}
|
|
7
|
+
createQueryMethodMap(isSuspense = false) {
|
|
8
|
+
const suspenseText = isSuspense ? 'Suspense' : '';
|
|
9
|
+
const queryMethodMap = {
|
|
10
|
+
infiniteQuery: {
|
|
11
|
+
getHook: (operationName = 'Query') => `use${suspenseText}Infinite${operationName}`,
|
|
12
|
+
getOptions: () => `Use${suspenseText}InfiniteQueryOptions`,
|
|
13
|
+
getOtherTypes: () => ({ infiniteData: 'InfiniteData' }),
|
|
14
|
+
},
|
|
15
|
+
query: {
|
|
16
|
+
getHook: (operationName = 'Query') => `use${suspenseText}${operationName}`,
|
|
17
|
+
getOptions: () => `Use${suspenseText}QueryOptions`,
|
|
18
|
+
},
|
|
19
|
+
mutation: {
|
|
20
|
+
getHook: (operationName = 'Mutation') => `use${operationName}`,
|
|
21
|
+
getOptions: () => `UseMutationOptions`,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
return queryMethodMap;
|
|
25
|
+
}
|
|
26
|
+
generateInfiniteQueryHelper(config, isSuspense) {
|
|
27
|
+
const { operationResultType, operationName } = config;
|
|
28
|
+
const { infiniteQuery } = this.createQueryMethodMap(isSuspense);
|
|
29
|
+
const isNextVersion = this.visitor.config.reactQueryVersion >= 5;
|
|
30
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(infiniteQuery.getHook());
|
|
31
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(infiniteQuery.getOptions());
|
|
32
|
+
if (isNextVersion) {
|
|
33
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(infiniteQuery.getOtherTypes().infiniteData);
|
|
34
|
+
}
|
|
35
|
+
const variables = this.generateInfiniteQueryVariablesSignature(config);
|
|
36
|
+
const options = this.generateInfiniteQueryOptionsSignature(config, isSuspense);
|
|
37
|
+
const generateBaseInfiniteQueryHook = (hookConfig) => {
|
|
38
|
+
const { implArguments, implHookOuter = '', implFetcher } = hookConfig;
|
|
39
|
+
const argumentsResult = implArguments !== null && implArguments !== void 0 ? implArguments : `
|
|
40
|
+
${variables},
|
|
41
|
+
${options}
|
|
42
|
+
`;
|
|
43
|
+
return `export const ${infiniteQuery.getHook(operationName)} = <
|
|
44
|
+
TData = ${isNextVersion
|
|
45
|
+
? `${infiniteQuery.getOtherTypes().infiniteData}<${operationResultType}>`
|
|
46
|
+
: operationResultType},
|
|
47
|
+
TError = ${this.visitor.config.errorType}
|
|
48
|
+
>(${argumentsResult}) => {
|
|
49
|
+
${implHookOuter}
|
|
50
|
+
return ${infiniteQuery.getHook()}<${operationResultType}, TError, TData>(
|
|
51
|
+
${this.generateInfiniteQueryFormattedParameters(this.generateInfiniteQueryKey(config, isSuspense), implFetcher)}
|
|
52
|
+
)};`;
|
|
53
|
+
};
|
|
54
|
+
return { generateBaseInfiniteQueryHook, variables, options };
|
|
55
|
+
}
|
|
56
|
+
generateQueryHelper(config, isSuspense) {
|
|
57
|
+
const { operationName, operationResultType } = config;
|
|
58
|
+
const { query } = this.createQueryMethodMap(isSuspense);
|
|
59
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(query.getHook());
|
|
60
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(query.getOptions());
|
|
61
|
+
const variables = this.generateQueryVariablesSignature(config);
|
|
62
|
+
const options = this.generateQueryOptionsSignature(config, isSuspense);
|
|
63
|
+
const generateBaseQueryHook = (hookConfig) => {
|
|
64
|
+
const { implArguments, implHookOuter = '', implFetcher } = hookConfig;
|
|
65
|
+
const argumentsResult = implArguments !== null && implArguments !== void 0 ? implArguments : `
|
|
66
|
+
${variables},
|
|
67
|
+
${options}
|
|
68
|
+
`;
|
|
69
|
+
return `export const ${query.getHook(operationName)} = <
|
|
70
|
+
TData = ${operationResultType},
|
|
71
|
+
TError = ${this.visitor.config.errorType}
|
|
72
|
+
>(${argumentsResult}) => {
|
|
73
|
+
${implHookOuter}
|
|
74
|
+
return ${query.getHook()}<${operationResultType}, TError, TData>(
|
|
75
|
+
${this.generateQueryFormattedParameters(this.generateQueryKey(config, isSuspense), implFetcher)}
|
|
76
|
+
)};`;
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
generateBaseQueryHook,
|
|
80
|
+
variables,
|
|
81
|
+
options,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
generateMutationHelper(config) {
|
|
85
|
+
const { operationResultType, operationVariablesTypes, operationName } = config;
|
|
86
|
+
const { mutation } = this.createQueryMethodMap();
|
|
87
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(mutation.getHook());
|
|
88
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(mutation.getOptions());
|
|
89
|
+
const variables = `variables?: ${operationVariablesTypes}`;
|
|
90
|
+
const options = `options?: ${mutation.getOptions()}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
91
|
+
const generateBaseMutationHook = (hookConfig) => {
|
|
92
|
+
const { implArguments, implHookOuter = '', implFetcher } = hookConfig;
|
|
93
|
+
const argumentsResult = implArguments !== null && implArguments !== void 0 ? implArguments : `${options}`;
|
|
94
|
+
return `export const ${mutation.getHook(operationName)} = <
|
|
95
|
+
TError = ${this.visitor.config.errorType},
|
|
96
|
+
TContext = unknown
|
|
97
|
+
>(${argumentsResult}) => {
|
|
98
|
+
${implHookOuter}
|
|
99
|
+
return ${mutation.getHook()}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
100
|
+
${this.generateMutationFormattedParameters(this.generateMutationKey(config), implFetcher)}
|
|
101
|
+
)};`;
|
|
102
|
+
};
|
|
103
|
+
return {
|
|
104
|
+
generateBaseMutationHook,
|
|
105
|
+
variables,
|
|
106
|
+
options,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
generateQueryVariablesSignature({ hasRequiredVariables, operationVariablesTypes, }) {
|
|
110
|
+
return `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
111
|
+
}
|
|
112
|
+
generateQueryOptionsSignature({ operationResultType }, isSuspense) {
|
|
113
|
+
const { query } = this.createQueryMethodMap(isSuspense);
|
|
114
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
115
|
+
return `options?: ${query.getOptions()}<${operationResultType}, TError, TData>`;
|
|
116
|
+
}
|
|
117
|
+
return `options?: Omit<${query.getOptions()}<${operationResultType}, TError, TData>, 'queryKey'> & { queryKey?: ${query.getOptions()}<${operationResultType}, TError, TData>['queryKey'] }`;
|
|
118
|
+
}
|
|
119
|
+
generateInfiniteQueryVariablesSignature(config) {
|
|
120
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
121
|
+
return `variables${config.hasRequiredVariables ? '' : '?'}: ${config.operationVariablesTypes}`;
|
|
122
|
+
}
|
|
123
|
+
return `variables: ${config.operationVariablesTypes}`;
|
|
124
|
+
}
|
|
125
|
+
generateInfiniteQueryOptionsSignature({ operationResultType }, isSuspense) {
|
|
126
|
+
const { infiniteQuery } = this.createQueryMethodMap(isSuspense);
|
|
127
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
128
|
+
return `options?: ${infiniteQuery.getOptions()}<${operationResultType}, TError, TData>`;
|
|
129
|
+
}
|
|
130
|
+
return `options: Omit<${infiniteQuery.getOptions()}<${operationResultType}, TError, TData>, 'queryKey'> & { queryKey?: ${infiniteQuery.getOptions()}<${operationResultType}, TError, TData>['queryKey'] }`;
|
|
131
|
+
}
|
|
132
|
+
generateInfiniteQueryKey(config, isSuspense) {
|
|
133
|
+
const identifier = isSuspense ? 'infiniteSuspense' : 'infinite';
|
|
134
|
+
if (config.hasRequiredVariables)
|
|
135
|
+
return `['${config.node.name.value}.${identifier}', variables]`;
|
|
136
|
+
return `variables === undefined ? ['${config.node.name.value}.${identifier}'] : ['${config.node.name.value}.${identifier}', variables]`;
|
|
137
|
+
}
|
|
138
|
+
generateInfiniteQueryOutput(config, isSuspense = false) {
|
|
139
|
+
const { infiniteQuery } = this.createQueryMethodMap(isSuspense);
|
|
140
|
+
const signature = this.generateQueryVariablesSignature(config);
|
|
141
|
+
const { operationName, node } = config;
|
|
142
|
+
return {
|
|
143
|
+
hook: this.generateInfiniteQueryHook(config, isSuspense),
|
|
144
|
+
getKey: `${infiniteQuery.getHook(operationName)}.getKey = (${signature}) => ${this.generateInfiniteQueryKey(config, isSuspense)};`,
|
|
145
|
+
rootKey: `${infiniteQuery.getHook(operationName)}.rootKey = '${node.name.value}.infinite';`,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
generateQueryKey(config, isSuspense) {
|
|
149
|
+
const identifier = isSuspense ? `${config.node.name.value}Suspense` : config.node.name.value;
|
|
150
|
+
if (config.hasRequiredVariables)
|
|
151
|
+
return `['${identifier}', variables]`;
|
|
152
|
+
return `variables === undefined ? ['${identifier}'] : ['${identifier}', variables]`;
|
|
153
|
+
}
|
|
154
|
+
generateQueryOutput(config, isSuspense = false) {
|
|
155
|
+
const { query } = this.createQueryMethodMap(isSuspense);
|
|
156
|
+
const signature = this.generateQueryVariablesSignature(config);
|
|
157
|
+
const { operationName, node, documentVariableName } = config;
|
|
158
|
+
return {
|
|
159
|
+
hook: this.generateQueryHook(config, isSuspense),
|
|
160
|
+
document: `${query.getHook(operationName)}.document = ${documentVariableName};`,
|
|
161
|
+
getKey: `${query.getHook(operationName)}.getKey = (${signature}) => ${this.generateQueryKey(config, isSuspense)};`,
|
|
162
|
+
rootKey: `${query.getHook(operationName)}.rootKey = '${node.name.value}';`,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
generateMutationKey({ node }) {
|
|
166
|
+
return `['${node.name.value}']`;
|
|
167
|
+
}
|
|
168
|
+
generateMutationOutput(config) {
|
|
169
|
+
const { mutation } = this.createQueryMethodMap();
|
|
170
|
+
const { operationName } = config;
|
|
171
|
+
return {
|
|
172
|
+
hook: this.generateMutationHook(config),
|
|
173
|
+
getKey: `${mutation.getHook(operationName)}.getKey = () => ${this.generateMutationKey(config)};`,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
generateInfiniteQueryFormattedParameters(queryKey, queryFn) {
|
|
177
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
178
|
+
return `${queryKey},
|
|
179
|
+
${queryFn},
|
|
180
|
+
options`;
|
|
181
|
+
}
|
|
182
|
+
return `(() => {
|
|
183
|
+
const { queryKey: optionsQueryKey, ...restOptions } = options;
|
|
184
|
+
return {
|
|
185
|
+
queryKey: optionsQueryKey ?? ${queryKey},
|
|
186
|
+
queryFn: ${queryFn},
|
|
187
|
+
...restOptions
|
|
188
|
+
}
|
|
189
|
+
})()`;
|
|
190
|
+
}
|
|
191
|
+
generateQueryFormattedParameters(queryKey, queryFn) {
|
|
192
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
193
|
+
return `${queryKey},
|
|
194
|
+
${queryFn},
|
|
195
|
+
options`;
|
|
196
|
+
}
|
|
197
|
+
return `{
|
|
198
|
+
queryKey: ${queryKey},
|
|
199
|
+
queryFn: ${queryFn},
|
|
200
|
+
...options
|
|
201
|
+
}`;
|
|
202
|
+
}
|
|
203
|
+
generateMutationFormattedParameters(mutationKey, mutationFn) {
|
|
204
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
205
|
+
return `${mutationKey},
|
|
206
|
+
${mutationFn},
|
|
207
|
+
options`;
|
|
208
|
+
}
|
|
209
|
+
return `{
|
|
210
|
+
mutationKey: ${mutationKey},
|
|
211
|
+
mutationFn: ${mutationFn},
|
|
212
|
+
...options
|
|
213
|
+
}`;
|
|
214
|
+
}
|
|
215
|
+
}
|
package/esm/index.js
CHANGED
|
@@ -19,6 +19,7 @@ export const plugin = (schema, documents, config) => {
|
|
|
19
19
|
return {
|
|
20
20
|
prepend: [...visitor.getImports(), visitor.getFetcherImplementation()],
|
|
21
21
|
content: [
|
|
22
|
+
'',
|
|
22
23
|
visitor.fragments,
|
|
23
24
|
...visitorResult.definitions.filter(t => typeof t === 'string'),
|
|
24
25
|
].join('\n'),
|
|
@@ -27,6 +28,7 @@ export const plugin = (schema, documents, config) => {
|
|
|
27
28
|
return {
|
|
28
29
|
prepend: [...visitor.getImports()],
|
|
29
30
|
content: [
|
|
31
|
+
'',
|
|
30
32
|
visitor.fragments,
|
|
31
33
|
...visitorResult.definitions.filter(t => typeof t === 'string'),
|
|
32
34
|
].join('\n'),
|
|
@@ -36,5 +38,8 @@ export const validate = async (schema, documents, config, outputFile) => {
|
|
|
36
38
|
if (extname(outputFile) !== '.ts' && extname(outputFile) !== '.tsx') {
|
|
37
39
|
throw new Error(`Plugin "typescript-react-query" requires extension to be ".ts" or ".tsx"!`);
|
|
38
40
|
}
|
|
41
|
+
if (config.reactQueryVersion !== 5 && config.addSuspenseQuery) {
|
|
42
|
+
throw new Error(`Suspense queries are only supported in react-query@5. Please upgrade your react-query version.`);
|
|
43
|
+
}
|
|
39
44
|
};
|
|
40
45
|
export { ReactQueryVisitor };
|