@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/cjs/fetcher.js
CHANGED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FetcherRenderer = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
6
|
+
class FetcherRenderer {
|
|
7
|
+
constructor(visitor) {
|
|
8
|
+
this.visitor = visitor;
|
|
9
|
+
(0, auto_bind_1.default)(this);
|
|
10
|
+
}
|
|
11
|
+
createQueryMethodMap(isSuspense = false) {
|
|
12
|
+
const suspenseText = isSuspense ? 'Suspense' : '';
|
|
13
|
+
const queryMethodMap = {
|
|
14
|
+
infiniteQuery: {
|
|
15
|
+
getHook: (operationName = 'Query') => `use${suspenseText}Infinite${operationName}`,
|
|
16
|
+
getOptions: () => `Use${suspenseText}InfiniteQueryOptions`,
|
|
17
|
+
getOtherTypes: () => ({ infiniteData: 'InfiniteData' }),
|
|
18
|
+
},
|
|
19
|
+
query: {
|
|
20
|
+
getHook: (operationName = 'Query') => `use${suspenseText}${operationName}`,
|
|
21
|
+
getOptions: () => `Use${suspenseText}QueryOptions`,
|
|
22
|
+
},
|
|
23
|
+
mutation: {
|
|
24
|
+
getHook: (operationName = 'Mutation') => `use${operationName}`,
|
|
25
|
+
getOptions: () => `UseMutationOptions`,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
return queryMethodMap;
|
|
29
|
+
}
|
|
30
|
+
generateInfiniteQueryHelper(config, isSuspense) {
|
|
31
|
+
const { operationResultType, operationName } = config;
|
|
32
|
+
const { infiniteQuery } = this.createQueryMethodMap(isSuspense);
|
|
33
|
+
const isNextVersion = this.visitor.config.reactQueryVersion >= 5;
|
|
34
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(infiniteQuery.getHook());
|
|
35
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(infiniteQuery.getOptions());
|
|
36
|
+
if (isNextVersion) {
|
|
37
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(infiniteQuery.getOtherTypes().infiniteData);
|
|
38
|
+
}
|
|
39
|
+
const variables = this.generateInfiniteQueryVariablesSignature(config);
|
|
40
|
+
const options = this.generateInfiniteQueryOptionsSignature(config, isSuspense);
|
|
41
|
+
const generateBaseInfiniteQueryHook = (hookConfig) => {
|
|
42
|
+
const { implArguments, implHookOuter = '', implFetcher } = hookConfig;
|
|
43
|
+
const argumentsResult = implArguments !== null && implArguments !== void 0 ? implArguments : `
|
|
44
|
+
${variables},
|
|
45
|
+
${options}
|
|
46
|
+
`;
|
|
47
|
+
return `export const ${infiniteQuery.getHook(operationName)} = <
|
|
48
|
+
TData = ${isNextVersion
|
|
49
|
+
? `${infiniteQuery.getOtherTypes().infiniteData}<${operationResultType}>`
|
|
50
|
+
: operationResultType},
|
|
51
|
+
TError = ${this.visitor.config.errorType}
|
|
52
|
+
>(${argumentsResult}) => {
|
|
53
|
+
${implHookOuter}
|
|
54
|
+
return ${infiniteQuery.getHook()}<${operationResultType}, TError, TData>(
|
|
55
|
+
${this.generateInfiniteQueryFormattedParameters(this.generateInfiniteQueryKey(config, isSuspense), implFetcher)}
|
|
56
|
+
)};`;
|
|
57
|
+
};
|
|
58
|
+
return { generateBaseInfiniteQueryHook, variables, options };
|
|
59
|
+
}
|
|
60
|
+
generateQueryHelper(config, isSuspense) {
|
|
61
|
+
const { operationName, operationResultType } = config;
|
|
62
|
+
const { query } = this.createQueryMethodMap(isSuspense);
|
|
63
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(query.getHook());
|
|
64
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(query.getOptions());
|
|
65
|
+
const variables = this.generateQueryVariablesSignature(config);
|
|
66
|
+
const options = this.generateQueryOptionsSignature(config, isSuspense);
|
|
67
|
+
const generateBaseQueryHook = (hookConfig) => {
|
|
68
|
+
const { implArguments, implHookOuter = '', implFetcher } = hookConfig;
|
|
69
|
+
const argumentsResult = implArguments !== null && implArguments !== void 0 ? implArguments : `
|
|
70
|
+
${variables},
|
|
71
|
+
${options}
|
|
72
|
+
`;
|
|
73
|
+
return `export const ${query.getHook(operationName)} = <
|
|
74
|
+
TData = ${operationResultType},
|
|
75
|
+
TError = ${this.visitor.config.errorType}
|
|
76
|
+
>(${argumentsResult}) => {
|
|
77
|
+
${implHookOuter}
|
|
78
|
+
return ${query.getHook()}<${operationResultType}, TError, TData>(
|
|
79
|
+
${this.generateQueryFormattedParameters(this.generateQueryKey(config, isSuspense), implFetcher)}
|
|
80
|
+
)};`;
|
|
81
|
+
};
|
|
82
|
+
return {
|
|
83
|
+
generateBaseQueryHook,
|
|
84
|
+
variables,
|
|
85
|
+
options,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
generateMutationHelper(config) {
|
|
89
|
+
const { operationResultType, operationVariablesTypes, operationName } = config;
|
|
90
|
+
const { mutation } = this.createQueryMethodMap();
|
|
91
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(mutation.getHook());
|
|
92
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(mutation.getOptions());
|
|
93
|
+
const variables = `variables?: ${operationVariablesTypes}`;
|
|
94
|
+
const options = `options?: ${mutation.getOptions()}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
95
|
+
const generateBaseMutationHook = (hookConfig) => {
|
|
96
|
+
const { implArguments, implHookOuter = '', implFetcher } = hookConfig;
|
|
97
|
+
const argumentsResult = implArguments !== null && implArguments !== void 0 ? implArguments : `${options}`;
|
|
98
|
+
return `export const ${mutation.getHook(operationName)} = <
|
|
99
|
+
TError = ${this.visitor.config.errorType},
|
|
100
|
+
TContext = unknown
|
|
101
|
+
>(${argumentsResult}) => {
|
|
102
|
+
${implHookOuter}
|
|
103
|
+
return ${mutation.getHook()}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
104
|
+
${this.generateMutationFormattedParameters(this.generateMutationKey(config), implFetcher)}
|
|
105
|
+
)};`;
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
generateBaseMutationHook,
|
|
109
|
+
variables,
|
|
110
|
+
options,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
generateQueryVariablesSignature({ hasRequiredVariables, operationVariablesTypes, }) {
|
|
114
|
+
return `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
115
|
+
}
|
|
116
|
+
generateQueryOptionsSignature({ operationResultType }, isSuspense) {
|
|
117
|
+
const { query } = this.createQueryMethodMap(isSuspense);
|
|
118
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
119
|
+
return `options?: ${query.getOptions()}<${operationResultType}, TError, TData>`;
|
|
120
|
+
}
|
|
121
|
+
return `options?: Omit<${query.getOptions()}<${operationResultType}, TError, TData>, 'queryKey'> & { queryKey?: ${query.getOptions()}<${operationResultType}, TError, TData>['queryKey'] }`;
|
|
122
|
+
}
|
|
123
|
+
generateInfiniteQueryVariablesSignature(config) {
|
|
124
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
125
|
+
return `variables${config.hasRequiredVariables ? '' : '?'}: ${config.operationVariablesTypes}`;
|
|
126
|
+
}
|
|
127
|
+
return `variables: ${config.operationVariablesTypes}`;
|
|
128
|
+
}
|
|
129
|
+
generateInfiniteQueryOptionsSignature({ operationResultType }, isSuspense) {
|
|
130
|
+
const { infiniteQuery } = this.createQueryMethodMap(isSuspense);
|
|
131
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
132
|
+
return `options?: ${infiniteQuery.getOptions()}<${operationResultType}, TError, TData>`;
|
|
133
|
+
}
|
|
134
|
+
return `options: Omit<${infiniteQuery.getOptions()}<${operationResultType}, TError, TData>, 'queryKey'> & { queryKey?: ${infiniteQuery.getOptions()}<${operationResultType}, TError, TData>['queryKey'] }`;
|
|
135
|
+
}
|
|
136
|
+
generateInfiniteQueryKey(config, isSuspense) {
|
|
137
|
+
const identifier = isSuspense ? 'infiniteSuspense' : 'infinite';
|
|
138
|
+
if (config.hasRequiredVariables)
|
|
139
|
+
return `['${config.node.name.value}.${identifier}', variables]`;
|
|
140
|
+
return `variables === undefined ? ['${config.node.name.value}.${identifier}'] : ['${config.node.name.value}.${identifier}', variables]`;
|
|
141
|
+
}
|
|
142
|
+
generateInfiniteQueryOutput(config, isSuspense = false) {
|
|
143
|
+
const { infiniteQuery } = this.createQueryMethodMap(isSuspense);
|
|
144
|
+
const signature = this.generateQueryVariablesSignature(config);
|
|
145
|
+
const { operationName, node } = config;
|
|
146
|
+
return {
|
|
147
|
+
hook: this.generateInfiniteQueryHook(config, isSuspense),
|
|
148
|
+
getKey: `${infiniteQuery.getHook(operationName)}.getKey = (${signature}) => ${this.generateInfiniteQueryKey(config, isSuspense)};`,
|
|
149
|
+
rootKey: `${infiniteQuery.getHook(operationName)}.rootKey = '${node.name.value}.infinite';`,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
generateQueryKey(config, isSuspense) {
|
|
153
|
+
const identifier = isSuspense ? `${config.node.name.value}Suspense` : config.node.name.value;
|
|
154
|
+
if (config.hasRequiredVariables)
|
|
155
|
+
return `['${identifier}', variables]`;
|
|
156
|
+
return `variables === undefined ? ['${identifier}'] : ['${identifier}', variables]`;
|
|
157
|
+
}
|
|
158
|
+
generateQueryOutput(config, isSuspense = false) {
|
|
159
|
+
const { query } = this.createQueryMethodMap(isSuspense);
|
|
160
|
+
const signature = this.generateQueryVariablesSignature(config);
|
|
161
|
+
const { operationName, node, documentVariableName } = config;
|
|
162
|
+
return {
|
|
163
|
+
hook: this.generateQueryHook(config, isSuspense),
|
|
164
|
+
document: `${query.getHook(operationName)}.document = ${documentVariableName};`,
|
|
165
|
+
getKey: `${query.getHook(operationName)}.getKey = (${signature}) => ${this.generateQueryKey(config, isSuspense)};`,
|
|
166
|
+
rootKey: `${query.getHook(operationName)}.rootKey = '${node.name.value}';`,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
generateMutationKey({ node }) {
|
|
170
|
+
return `['${node.name.value}']`;
|
|
171
|
+
}
|
|
172
|
+
generateMutationOutput(config) {
|
|
173
|
+
const { mutation } = this.createQueryMethodMap();
|
|
174
|
+
const { operationName } = config;
|
|
175
|
+
return {
|
|
176
|
+
hook: this.generateMutationHook(config),
|
|
177
|
+
getKey: `${mutation.getHook(operationName)}.getKey = () => ${this.generateMutationKey(config)};`,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
generateInfiniteQueryFormattedParameters(queryKey, queryFn) {
|
|
181
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
182
|
+
return `${queryKey},
|
|
183
|
+
${queryFn},
|
|
184
|
+
options`;
|
|
185
|
+
}
|
|
186
|
+
return `(() => {
|
|
187
|
+
const { queryKey: optionsQueryKey, ...restOptions } = options;
|
|
188
|
+
return {
|
|
189
|
+
queryKey: optionsQueryKey ?? ${queryKey},
|
|
190
|
+
queryFn: ${queryFn},
|
|
191
|
+
...restOptions
|
|
192
|
+
}
|
|
193
|
+
})()`;
|
|
194
|
+
}
|
|
195
|
+
generateQueryFormattedParameters(queryKey, queryFn) {
|
|
196
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
197
|
+
return `${queryKey},
|
|
198
|
+
${queryFn},
|
|
199
|
+
options`;
|
|
200
|
+
}
|
|
201
|
+
return `{
|
|
202
|
+
queryKey: ${queryKey},
|
|
203
|
+
queryFn: ${queryFn},
|
|
204
|
+
...options
|
|
205
|
+
}`;
|
|
206
|
+
}
|
|
207
|
+
generateMutationFormattedParameters(mutationKey, mutationFn) {
|
|
208
|
+
if (this.visitor.config.reactQueryVersion <= 4) {
|
|
209
|
+
return `${mutationKey},
|
|
210
|
+
${mutationFn},
|
|
211
|
+
options`;
|
|
212
|
+
}
|
|
213
|
+
return `{
|
|
214
|
+
mutationKey: ${mutationKey},
|
|
215
|
+
mutationFn: ${mutationFn},
|
|
216
|
+
...options
|
|
217
|
+
}`;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
exports.FetcherRenderer = FetcherRenderer;
|
package/cjs/index.js
CHANGED
|
@@ -23,6 +23,7 @@ const plugin = (schema, documents, config) => {
|
|
|
23
23
|
return {
|
|
24
24
|
prepend: [...visitor.getImports(), visitor.getFetcherImplementation()],
|
|
25
25
|
content: [
|
|
26
|
+
'',
|
|
26
27
|
visitor.fragments,
|
|
27
28
|
...visitorResult.definitions.filter(t => typeof t === 'string'),
|
|
28
29
|
].join('\n'),
|
|
@@ -31,6 +32,7 @@ const plugin = (schema, documents, config) => {
|
|
|
31
32
|
return {
|
|
32
33
|
prepend: [...visitor.getImports()],
|
|
33
34
|
content: [
|
|
35
|
+
'',
|
|
34
36
|
visitor.fragments,
|
|
35
37
|
...visitorResult.definitions.filter(t => typeof t === 'string'),
|
|
36
38
|
].join('\n'),
|
|
@@ -41,5 +43,8 @@ const validate = async (schema, documents, config, outputFile) => {
|
|
|
41
43
|
if ((0, path_1.extname)(outputFile) !== '.ts' && (0, path_1.extname)(outputFile) !== '.tsx') {
|
|
42
44
|
throw new Error(`Plugin "typescript-react-query" requires extension to be ".ts" or ".tsx"!`);
|
|
43
45
|
}
|
|
46
|
+
if (config.reactQueryVersion !== 5 && config.addSuspenseQuery) {
|
|
47
|
+
throw new Error(`Suspense queries are only supported in react-query@5. Please upgrade your react-query version.`);
|
|
48
|
+
}
|
|
44
49
|
};
|
|
45
50
|
exports.validate = validate;
|
package/cjs/visitor.js
CHANGED
|
@@ -9,9 +9,9 @@ const fetcher_custom_mapper_js_1 = require("./fetcher-custom-mapper.js");
|
|
|
9
9
|
const fetcher_fetch_hardcoded_js_1 = require("./fetcher-fetch-hardcoded.js");
|
|
10
10
|
const fetcher_fetch_js_1 = require("./fetcher-fetch.js");
|
|
11
11
|
const fetcher_graphql_request_js_1 = require("./fetcher-graphql-request.js");
|
|
12
|
-
const variables_generator_js_1 = require("./variables-generator.js");
|
|
13
12
|
class ReactQueryVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
|
|
14
13
|
constructor(schema, fragments, rawConfig, documents) {
|
|
14
|
+
const defaultReactQueryVersion = !rawConfig.reactQueryVersion && rawConfig.legacyMode ? 3 : 4;
|
|
15
15
|
super(schema, fragments, rawConfig, {
|
|
16
16
|
documentMode: visitor_plugin_common_1.DocumentMode.string,
|
|
17
17
|
errorType: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.errorType, 'unknown'),
|
|
@@ -21,26 +21,13 @@ class ReactQueryVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
|
|
|
21
21
|
exposeMutationKeys: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.exposeMutationKeys, false),
|
|
22
22
|
exposeFetcher: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.exposeFetcher, false),
|
|
23
23
|
addInfiniteQuery: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.addInfiniteQuery, false),
|
|
24
|
-
|
|
24
|
+
addSuspenseQuery: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.addSuspenseQuery, false),
|
|
25
|
+
reactQueryVersion: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.reactQueryVersion, defaultReactQueryVersion),
|
|
25
26
|
reactQueryImportFrom: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.reactQueryImportFrom, ''),
|
|
26
27
|
});
|
|
27
28
|
this.rawConfig = rawConfig;
|
|
28
29
|
this.reactQueryHookIdentifiersInUse = new Set();
|
|
29
30
|
this.reactQueryOptionsIdentifiersInUse = new Set();
|
|
30
|
-
this.queryMethodMap = {
|
|
31
|
-
infiniteQuery: {
|
|
32
|
-
hook: 'useInfiniteQuery',
|
|
33
|
-
options: 'UseInfiniteQueryOptions',
|
|
34
|
-
},
|
|
35
|
-
query: {
|
|
36
|
-
hook: 'useQuery',
|
|
37
|
-
options: 'UseQueryOptions',
|
|
38
|
-
},
|
|
39
|
-
mutation: {
|
|
40
|
-
hook: 'useMutation',
|
|
41
|
-
options: 'UseMutationOptions',
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
31
|
this._externalImportPrefix = this.config.importOperationTypesFrom
|
|
45
32
|
? `${this.config.importOperationTypesFrom}.`
|
|
46
33
|
: '';
|
|
@@ -77,13 +64,13 @@ class ReactQueryVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
|
|
|
77
64
|
];
|
|
78
65
|
const moduleName = this.config.reactQueryImportFrom
|
|
79
66
|
? this.config.reactQueryImportFrom
|
|
80
|
-
: this.config.
|
|
67
|
+
: this.config.reactQueryVersion <= 3
|
|
81
68
|
? 'react-query'
|
|
82
69
|
: '@tanstack/react-query';
|
|
83
70
|
return [...baseImports, `import { ${hookAndTypeImports.join(', ')} } from '${moduleName}';`];
|
|
84
71
|
}
|
|
85
72
|
getFetcherImplementation() {
|
|
86
|
-
return this.fetcher.
|
|
73
|
+
return this.fetcher.generateFetcherImplementation();
|
|
87
74
|
}
|
|
88
75
|
_getHookSuffix(name, operationType) {
|
|
89
76
|
if (this.config.omitOperationSuffix) {
|
|
@@ -106,45 +93,63 @@ class ReactQueryVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
|
|
|
106
93
|
useTypesPrefix: false,
|
|
107
94
|
useTypesSuffix: false,
|
|
108
95
|
});
|
|
96
|
+
const generateConfig = {
|
|
97
|
+
node,
|
|
98
|
+
documentVariableName,
|
|
99
|
+
operationResultType,
|
|
100
|
+
operationVariablesTypes,
|
|
101
|
+
hasRequiredVariables,
|
|
102
|
+
operationName,
|
|
103
|
+
};
|
|
109
104
|
operationResultType = this._externalImportPrefix + operationResultType;
|
|
110
105
|
operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes;
|
|
106
|
+
const queries = [];
|
|
107
|
+
const getOutputFromQueries = () => `\n${queries.join('\n\n')}\n`;
|
|
111
108
|
if (operationType === 'Query') {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
109
|
+
const addQuery = (generateConfig, isSuspense = false) => {
|
|
110
|
+
const { hook, getKey, rootKey, document } = this.fetcher.generateQueryOutput(generateConfig, isSuspense);
|
|
111
|
+
queries.push(hook);
|
|
112
|
+
if (this.config.exposeDocument)
|
|
113
|
+
queries.push(document);
|
|
114
|
+
if (this.config.exposeQueryKeys)
|
|
115
|
+
queries.push(getKey);
|
|
116
|
+
if (this.config.exposeQueryRootKeys)
|
|
117
|
+
queries.push(rootKey);
|
|
118
|
+
};
|
|
119
|
+
addQuery(generateConfig);
|
|
120
|
+
if (this.config.addSuspenseQuery)
|
|
121
|
+
addQuery(generateConfig, true);
|
|
122
122
|
if (this.config.addInfiniteQuery) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
123
|
+
const addInfiniteQuery = (generateConfig, isSuspense = false) => {
|
|
124
|
+
const { hook, getKey, rootKey } = this.fetcher.generateInfiniteQueryOutput(generateConfig, isSuspense);
|
|
125
|
+
queries.push(hook);
|
|
126
|
+
if (this.config.exposeQueryKeys)
|
|
127
|
+
queries.push(getKey);
|
|
128
|
+
if (this.config.exposeQueryRootKeys)
|
|
129
|
+
queries.push(rootKey);
|
|
130
|
+
};
|
|
131
|
+
addInfiniteQuery(generateConfig);
|
|
132
|
+
if (this.config.addSuspenseQuery) {
|
|
133
|
+
addInfiniteQuery(generateConfig, true);
|
|
129
134
|
}
|
|
130
135
|
}
|
|
131
136
|
// The reason we're looking at the private field of the CustomMapperFetcher to see if it's a react hook
|
|
132
137
|
// is to prevent calling generateFetcherFetch for each query since all the queries won't be able to generate
|
|
133
138
|
// a fetcher field anyways.
|
|
134
139
|
if (this.config.exposeFetcher && !this.fetcher._isReactHook) {
|
|
135
|
-
|
|
140
|
+
queries.push(this.fetcher.generateFetcherFetch(generateConfig));
|
|
136
141
|
}
|
|
137
|
-
return
|
|
142
|
+
return getOutputFromQueries();
|
|
138
143
|
}
|
|
139
144
|
if (operationType === 'Mutation') {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
145
|
+
const { hook, getKey } = this.fetcher.generateMutationOutput(generateConfig);
|
|
146
|
+
queries.push(hook);
|
|
147
|
+
if (this.config.exposeMutationKeys)
|
|
148
|
+
queries.push(getKey);
|
|
144
149
|
if (this.config.exposeFetcher && !this.fetcher._isReactHook) {
|
|
145
|
-
|
|
150
|
+
queries.push(this.fetcher.generateFetcherFetch(generateConfig));
|
|
146
151
|
}
|
|
147
|
-
return
|
|
152
|
+
return getOutputFromQueries();
|
|
148
153
|
}
|
|
149
154
|
if (operationType === 'Subscription') {
|
|
150
155
|
// eslint-disable-next-line no-console
|
|
@@ -1,18 +1,21 @@
|
|
|
1
|
+
import autoBind from 'auto-bind';
|
|
1
2
|
import { buildMapperImport, parseMapper, } from '@graphql-codegen/visitor-plugin-common';
|
|
2
|
-
import {
|
|
3
|
-
export class CustomMapperFetcher {
|
|
3
|
+
import { FetcherRenderer } from './fetcher.js';
|
|
4
|
+
export class CustomMapperFetcher extends FetcherRenderer {
|
|
4
5
|
constructor(visitor, customFetcher) {
|
|
6
|
+
super(visitor);
|
|
5
7
|
this.visitor = visitor;
|
|
6
8
|
if (typeof customFetcher === 'string') {
|
|
7
9
|
customFetcher = { func: customFetcher };
|
|
8
10
|
}
|
|
9
11
|
this._mapper = parseMapper(customFetcher.func);
|
|
10
12
|
this._isReactHook = customFetcher.isReactHook;
|
|
13
|
+
autoBind(this);
|
|
11
14
|
}
|
|
12
15
|
getFetcherFnName(operationResultType, operationVariablesTypes) {
|
|
13
16
|
return `${this._mapper.type}<${operationResultType}, ${operationVariablesTypes}>`;
|
|
14
17
|
}
|
|
15
|
-
|
|
18
|
+
generateFetcherImplementation() {
|
|
16
19
|
if (this._mapper.isExternal) {
|
|
17
20
|
return buildMapperImport(this._mapper.source, [
|
|
18
21
|
{
|
|
@@ -23,77 +26,45 @@ export class CustomMapperFetcher {
|
|
|
23
26
|
}
|
|
24
27
|
return null;
|
|
25
28
|
}
|
|
26
|
-
generateInfiniteQueryHook(
|
|
27
|
-
const
|
|
28
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
29
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
30
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
31
|
-
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
29
|
+
generateInfiniteQueryHook(config, isSuspense = false) {
|
|
30
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
32
31
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
33
32
|
const implHookOuter = this._isReactHook
|
|
34
33
|
? `const query = ${typedFetcher}(${documentVariableName})`
|
|
35
34
|
: '';
|
|
36
|
-
const
|
|
35
|
+
const implFetcher = this._isReactHook
|
|
37
36
|
? `(metaData) => query({...variables, ...(metaData.pageParam ?? {})})`
|
|
38
37
|
: `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
${options}
|
|
45
|
-
) =>{
|
|
46
|
-
${implHookOuter}
|
|
47
|
-
return ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
48
|
-
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
49
|
-
${impl},
|
|
50
|
-
options
|
|
51
|
-
)};`;
|
|
38
|
+
const { generateBaseInfiniteQueryHook } = this.generateInfiniteQueryHelper(config, isSuspense);
|
|
39
|
+
return generateBaseInfiniteQueryHook({
|
|
40
|
+
implHookOuter,
|
|
41
|
+
implFetcher,
|
|
42
|
+
});
|
|
52
43
|
}
|
|
53
|
-
generateQueryHook(
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
|
|
57
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
58
|
-
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
44
|
+
generateQueryHook(config, isSuspense = false) {
|
|
45
|
+
const { generateBaseQueryHook } = this.generateQueryHelper(config, isSuspense);
|
|
46
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
59
47
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
60
|
-
const
|
|
48
|
+
const implFetcher = this._isReactHook
|
|
61
49
|
? `${typedFetcher}(${documentVariableName}).bind(null, variables)`
|
|
62
50
|
: `${typedFetcher}(${documentVariableName}, variables)`;
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
>(
|
|
67
|
-
${variables},
|
|
68
|
-
${options}
|
|
69
|
-
) =>
|
|
70
|
-
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
71
|
-
${generateQueryKey(node, hasRequiredVariables)},
|
|
72
|
-
${impl},
|
|
73
|
-
options
|
|
74
|
-
);`;
|
|
51
|
+
return generateBaseQueryHook({
|
|
52
|
+
implFetcher,
|
|
53
|
+
});
|
|
75
54
|
}
|
|
76
|
-
generateMutationHook(
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook);
|
|
80
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
81
|
-
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
55
|
+
generateMutationHook(config) {
|
|
56
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
57
|
+
const { generateBaseMutationHook, variables } = this.generateMutationHelper(config);
|
|
82
58
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
83
|
-
const
|
|
59
|
+
const implFetcher = this._isReactHook
|
|
84
60
|
? `${typedFetcher}(${documentVariableName})`
|
|
85
61
|
: `(${variables}) => ${typedFetcher}(${documentVariableName}, variables)()`;
|
|
86
|
-
return
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
>(${options}) =>
|
|
90
|
-
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
91
|
-
${generateMutationKey(node)},
|
|
92
|
-
${impl},
|
|
93
|
-
options
|
|
94
|
-
);`;
|
|
62
|
+
return generateBaseMutationHook({
|
|
63
|
+
implFetcher,
|
|
64
|
+
});
|
|
95
65
|
}
|
|
96
|
-
generateFetcherFetch(
|
|
66
|
+
generateFetcherFetch(config) {
|
|
67
|
+
const { documentVariableName, operationResultType, operationVariablesTypes, hasRequiredVariables, operationName, } = config;
|
|
97
68
|
// We can't generate a fetcher field since we can't call react hooks outside of a React Fucntion Component
|
|
98
69
|
// Related: https://reactjs.org/docs/hooks-rules.html
|
|
99
70
|
if (this._isReactHook)
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import autoBind from 'auto-bind';
|
|
2
|
+
import { FetcherRenderer } from './fetcher.js';
|
|
3
|
+
export class HardcodedFetchFetcher extends FetcherRenderer {
|
|
3
4
|
constructor(visitor, config) {
|
|
5
|
+
super(visitor);
|
|
4
6
|
this.visitor = visitor;
|
|
5
7
|
this.config = config;
|
|
8
|
+
autoBind(this);
|
|
6
9
|
}
|
|
7
10
|
getEndpoint() {
|
|
8
11
|
try {
|
|
@@ -23,7 +26,7 @@ export class HardcodedFetchFetcher {
|
|
|
23
26
|
}
|
|
24
27
|
return ` method: "POST",${fetchParamsPartial}`;
|
|
25
28
|
}
|
|
26
|
-
|
|
29
|
+
generateFetcherImplementation() {
|
|
27
30
|
return `
|
|
28
31
|
function fetcher<TData, TVariables>(query: string, variables?: TVariables) {
|
|
29
32
|
return async (): Promise<TData> => {
|
|
@@ -44,62 +47,30 @@ ${this.getFetchParams()}
|
|
|
44
47
|
}
|
|
45
48
|
}`;
|
|
46
49
|
}
|
|
47
|
-
generateInfiniteQueryHook(
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return `export const useInfinite${operationName} = <
|
|
54
|
-
TData = ${operationResultType},
|
|
55
|
-
TError = ${this.visitor.config.errorType}
|
|
56
|
-
>(
|
|
57
|
-
${variables},
|
|
58
|
-
${options}
|
|
59
|
-
) =>
|
|
60
|
-
${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
61
|
-
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
62
|
-
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})(),
|
|
63
|
-
options
|
|
64
|
-
);`;
|
|
50
|
+
generateInfiniteQueryHook(config, isSuspense = false) {
|
|
51
|
+
const { generateBaseInfiniteQueryHook } = this.generateInfiniteQueryHelper(config, isSuspense);
|
|
52
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
53
|
+
return generateBaseInfiniteQueryHook({
|
|
54
|
+
implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`,
|
|
55
|
+
});
|
|
65
56
|
}
|
|
66
|
-
generateQueryHook(
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return `export const use${operationName} = <
|
|
73
|
-
TData = ${operationResultType},
|
|
74
|
-
TError = ${this.visitor.config.errorType}
|
|
75
|
-
>(
|
|
76
|
-
${variables},
|
|
77
|
-
${options}
|
|
78
|
-
) =>
|
|
79
|
-
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
80
|
-
${generateQueryKey(node, hasRequiredVariables)},
|
|
81
|
-
fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables),
|
|
82
|
-
options
|
|
83
|
-
);`;
|
|
57
|
+
generateQueryHook(config, isSuspense = false) {
|
|
58
|
+
const { generateBaseQueryHook } = this.generateQueryHelper(config, isSuspense);
|
|
59
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
60
|
+
return generateBaseQueryHook({
|
|
61
|
+
implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)`,
|
|
62
|
+
});
|
|
84
63
|
}
|
|
85
|
-
generateMutationHook(
|
|
86
|
-
const
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return `export const use${operationName} = <
|
|
92
|
-
TError = ${this.visitor.config.errorType},
|
|
93
|
-
TContext = unknown
|
|
94
|
-
>(${options}) =>
|
|
95
|
-
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
96
|
-
${generateMutationKey(node)},
|
|
97
|
-
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)(),
|
|
98
|
-
options
|
|
99
|
-
);`;
|
|
64
|
+
generateMutationHook(config) {
|
|
65
|
+
const { generateBaseMutationHook, variables } = this.generateMutationHelper(config);
|
|
66
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
67
|
+
return generateBaseMutationHook({
|
|
68
|
+
implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)()`,
|
|
69
|
+
});
|
|
100
70
|
}
|
|
101
|
-
generateFetcherFetch(
|
|
102
|
-
const
|
|
71
|
+
generateFetcherFetch(config) {
|
|
72
|
+
const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
|
|
73
|
+
const variables = this.generateQueryVariablesSignature(config);
|
|
103
74
|
return `\nuse${operationName}.fetcher = (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables);`;
|
|
104
75
|
}
|
|
105
76
|
}
|