@graphql-codegen/typescript-react-query 5.1.0-alpha-20231024141603-98360b670 → 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
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CustomMapperFetcher = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
4
6
|
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
5
|
-
const
|
|
6
|
-
class CustomMapperFetcher {
|
|
7
|
+
const fetcher_js_1 = require("./fetcher.js");
|
|
8
|
+
class CustomMapperFetcher extends fetcher_js_1.FetcherRenderer {
|
|
7
9
|
constructor(visitor, customFetcher) {
|
|
10
|
+
super(visitor);
|
|
8
11
|
this.visitor = visitor;
|
|
9
12
|
if (typeof customFetcher === 'string') {
|
|
10
13
|
customFetcher = { func: customFetcher };
|
|
11
14
|
}
|
|
12
15
|
this._mapper = (0, visitor_plugin_common_1.parseMapper)(customFetcher.func);
|
|
13
16
|
this._isReactHook = customFetcher.isReactHook;
|
|
17
|
+
(0, auto_bind_1.default)(this);
|
|
14
18
|
}
|
|
15
19
|
getFetcherFnName(operationResultType, operationVariablesTypes) {
|
|
16
20
|
return `${this._mapper.type}<${operationResultType}, ${operationVariablesTypes}>`;
|
|
17
21
|
}
|
|
18
|
-
|
|
22
|
+
generateFetcherImplementation() {
|
|
19
23
|
if (this._mapper.isExternal) {
|
|
20
24
|
return (0, visitor_plugin_common_1.buildMapperImport)(this._mapper.source, [
|
|
21
25
|
{
|
|
@@ -26,77 +30,45 @@ class CustomMapperFetcher {
|
|
|
26
30
|
}
|
|
27
31
|
return null;
|
|
28
32
|
}
|
|
29
|
-
generateInfiniteQueryHook(
|
|
30
|
-
const
|
|
31
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
32
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
33
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
34
|
-
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
33
|
+
generateInfiniteQueryHook(config, isSuspense = false) {
|
|
34
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
35
35
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
36
36
|
const implHookOuter = this._isReactHook
|
|
37
37
|
? `const query = ${typedFetcher}(${documentVariableName})`
|
|
38
38
|
: '';
|
|
39
|
-
const
|
|
39
|
+
const implFetcher = this._isReactHook
|
|
40
40
|
? `(metaData) => query({...variables, ...(metaData.pageParam ?? {})})`
|
|
41
41
|
: `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
${options}
|
|
48
|
-
) =>{
|
|
49
|
-
${implHookOuter}
|
|
50
|
-
return ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
51
|
-
${(0, variables_generator_js_1.generateInfiniteQueryKey)(node, hasRequiredVariables)},
|
|
52
|
-
${impl},
|
|
53
|
-
options
|
|
54
|
-
)};`;
|
|
42
|
+
const { generateBaseInfiniteQueryHook } = this.generateInfiniteQueryHelper(config, isSuspense);
|
|
43
|
+
return generateBaseInfiniteQueryHook({
|
|
44
|
+
implHookOuter,
|
|
45
|
+
implFetcher,
|
|
46
|
+
});
|
|
55
47
|
}
|
|
56
|
-
generateQueryHook(
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
|
|
60
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
61
|
-
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
48
|
+
generateQueryHook(config, isSuspense = false) {
|
|
49
|
+
const { generateBaseQueryHook } = this.generateQueryHelper(config, isSuspense);
|
|
50
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
62
51
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
63
|
-
const
|
|
52
|
+
const implFetcher = this._isReactHook
|
|
64
53
|
? `${typedFetcher}(${documentVariableName}).bind(null, variables)`
|
|
65
54
|
: `${typedFetcher}(${documentVariableName}, variables)`;
|
|
66
|
-
return
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
>(
|
|
70
|
-
${variables},
|
|
71
|
-
${options}
|
|
72
|
-
) =>
|
|
73
|
-
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
74
|
-
${(0, variables_generator_js_1.generateQueryKey)(node, hasRequiredVariables)},
|
|
75
|
-
${impl},
|
|
76
|
-
options
|
|
77
|
-
);`;
|
|
55
|
+
return generateBaseQueryHook({
|
|
56
|
+
implFetcher,
|
|
57
|
+
});
|
|
78
58
|
}
|
|
79
|
-
generateMutationHook(
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook);
|
|
83
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
84
|
-
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
59
|
+
generateMutationHook(config) {
|
|
60
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
61
|
+
const { generateBaseMutationHook, variables } = this.generateMutationHelper(config);
|
|
85
62
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
86
|
-
const
|
|
63
|
+
const implFetcher = this._isReactHook
|
|
87
64
|
? `${typedFetcher}(${documentVariableName})`
|
|
88
65
|
: `(${variables}) => ${typedFetcher}(${documentVariableName}, variables)()`;
|
|
89
|
-
return
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
>(${options}) =>
|
|
93
|
-
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
94
|
-
${(0, variables_generator_js_1.generateMutationKey)(node)},
|
|
95
|
-
${impl},
|
|
96
|
-
options
|
|
97
|
-
);`;
|
|
66
|
+
return generateBaseMutationHook({
|
|
67
|
+
implFetcher,
|
|
68
|
+
});
|
|
98
69
|
}
|
|
99
|
-
generateFetcherFetch(
|
|
70
|
+
generateFetcherFetch(config) {
|
|
71
|
+
const { documentVariableName, operationResultType, operationVariablesTypes, hasRequiredVariables, operationName, } = config;
|
|
100
72
|
// We can't generate a fetcher field since we can't call react hooks outside of a React Fucntion Component
|
|
101
73
|
// Related: https://reactjs.org/docs/hooks-rules.html
|
|
102
74
|
if (this._isReactHook)
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HardcodedFetchFetcher = void 0;
|
|
4
|
-
const
|
|
5
|
-
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
6
|
+
const fetcher_js_1 = require("./fetcher.js");
|
|
7
|
+
class HardcodedFetchFetcher extends fetcher_js_1.FetcherRenderer {
|
|
6
8
|
constructor(visitor, config) {
|
|
9
|
+
super(visitor);
|
|
7
10
|
this.visitor = visitor;
|
|
8
11
|
this.config = config;
|
|
12
|
+
(0, auto_bind_1.default)(this);
|
|
9
13
|
}
|
|
10
14
|
getEndpoint() {
|
|
11
15
|
try {
|
|
@@ -26,7 +30,7 @@ class HardcodedFetchFetcher {
|
|
|
26
30
|
}
|
|
27
31
|
return ` method: "POST",${fetchParamsPartial}`;
|
|
28
32
|
}
|
|
29
|
-
|
|
33
|
+
generateFetcherImplementation() {
|
|
30
34
|
return `
|
|
31
35
|
function fetcher<TData, TVariables>(query: string, variables?: TVariables) {
|
|
32
36
|
return async (): Promise<TData> => {
|
|
@@ -47,62 +51,30 @@ ${this.getFetchParams()}
|
|
|
47
51
|
}
|
|
48
52
|
}`;
|
|
49
53
|
}
|
|
50
|
-
generateInfiniteQueryHook(
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return `export const useInfinite${operationName} = <
|
|
57
|
-
TData = ${operationResultType},
|
|
58
|
-
TError = ${this.visitor.config.errorType}
|
|
59
|
-
>(
|
|
60
|
-
${variables},
|
|
61
|
-
${options}
|
|
62
|
-
) =>
|
|
63
|
-
${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
64
|
-
${(0, variables_generator_js_1.generateInfiniteQueryKey)(node, hasRequiredVariables)},
|
|
65
|
-
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})(),
|
|
66
|
-
options
|
|
67
|
-
);`;
|
|
54
|
+
generateInfiniteQueryHook(config, isSuspense = false) {
|
|
55
|
+
const { generateBaseInfiniteQueryHook } = this.generateInfiniteQueryHelper(config, isSuspense);
|
|
56
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
57
|
+
return generateBaseInfiniteQueryHook({
|
|
58
|
+
implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`,
|
|
59
|
+
});
|
|
68
60
|
}
|
|
69
|
-
generateQueryHook(
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return `export const use${operationName} = <
|
|
76
|
-
TData = ${operationResultType},
|
|
77
|
-
TError = ${this.visitor.config.errorType}
|
|
78
|
-
>(
|
|
79
|
-
${variables},
|
|
80
|
-
${options}
|
|
81
|
-
) =>
|
|
82
|
-
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
83
|
-
${(0, variables_generator_js_1.generateQueryKey)(node, hasRequiredVariables)},
|
|
84
|
-
fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables),
|
|
85
|
-
options
|
|
86
|
-
);`;
|
|
61
|
+
generateQueryHook(config, isSuspense = false) {
|
|
62
|
+
const { generateBaseQueryHook } = this.generateQueryHelper(config, isSuspense);
|
|
63
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
64
|
+
return generateBaseQueryHook({
|
|
65
|
+
implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)`,
|
|
66
|
+
});
|
|
87
67
|
}
|
|
88
|
-
generateMutationHook(
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return `export const use${operationName} = <
|
|
95
|
-
TError = ${this.visitor.config.errorType},
|
|
96
|
-
TContext = unknown
|
|
97
|
-
>(${options}) =>
|
|
98
|
-
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
99
|
-
${(0, variables_generator_js_1.generateMutationKey)(node)},
|
|
100
|
-
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)(),
|
|
101
|
-
options
|
|
102
|
-
);`;
|
|
68
|
+
generateMutationHook(config) {
|
|
69
|
+
const { generateBaseMutationHook, variables } = this.generateMutationHelper(config);
|
|
70
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
71
|
+
return generateBaseMutationHook({
|
|
72
|
+
implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)()`,
|
|
73
|
+
});
|
|
103
74
|
}
|
|
104
|
-
generateFetcherFetch(
|
|
105
|
-
const
|
|
75
|
+
generateFetcherFetch(config) {
|
|
76
|
+
const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
|
|
77
|
+
const variables = this.generateQueryVariablesSignature(config);
|
|
106
78
|
return `\nuse${operationName}.fetcher = (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables);`;
|
|
107
79
|
}
|
|
108
80
|
}
|
package/cjs/fetcher-fetch.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FetchFetcher = void 0;
|
|
4
|
-
const
|
|
5
|
-
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
6
|
+
const fetcher_js_1 = require("./fetcher.js");
|
|
7
|
+
class FetchFetcher extends fetcher_js_1.FetcherRenderer {
|
|
6
8
|
constructor(visitor) {
|
|
9
|
+
super(visitor);
|
|
7
10
|
this.visitor = visitor;
|
|
11
|
+
(0, auto_bind_1.default)(this);
|
|
8
12
|
}
|
|
9
|
-
|
|
13
|
+
generateFetcherImplementation() {
|
|
10
14
|
return `
|
|
11
15
|
function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit, query: string, variables?: TVariables) {
|
|
12
16
|
return async (): Promise<TData> => {
|
|
@@ -28,67 +32,44 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
|
|
|
28
32
|
}
|
|
29
33
|
}`;
|
|
30
34
|
}
|
|
31
|
-
generateInfiniteQueryHook(
|
|
32
|
-
const variables =
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
37
|
-
return `export const useInfinite${operationName} = <
|
|
38
|
-
TData = ${operationResultType},
|
|
39
|
-
TError = ${this.visitor.config.errorType}
|
|
40
|
-
>(
|
|
35
|
+
generateInfiniteQueryHook(config, isSuspense = false) {
|
|
36
|
+
const { generateBaseInfiniteQueryHook, variables, options } = this.generateInfiniteQueryHelper(config, isSuspense);
|
|
37
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
38
|
+
return generateBaseInfiniteQueryHook({
|
|
39
|
+
implArguments: `
|
|
41
40
|
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
42
41
|
${variables},
|
|
43
42
|
${options}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})(),
|
|
48
|
-
options
|
|
49
|
-
);`;
|
|
43
|
+
`,
|
|
44
|
+
implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`,
|
|
45
|
+
});
|
|
50
46
|
}
|
|
51
|
-
generateQueryHook(
|
|
52
|
-
const variables =
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
57
|
-
return `export const use${operationName} = <
|
|
58
|
-
TData = ${operationResultType},
|
|
59
|
-
TError = ${this.visitor.config.errorType}
|
|
60
|
-
>(
|
|
47
|
+
generateQueryHook(config, isSuspense = false) {
|
|
48
|
+
const { generateBaseQueryHook, variables, options } = this.generateQueryHelper(config, isSuspense);
|
|
49
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
50
|
+
return generateBaseQueryHook({
|
|
51
|
+
implArguments: `
|
|
61
52
|
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
62
53
|
${variables},
|
|
63
54
|
${options}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables),
|
|
68
|
-
options
|
|
69
|
-
);`;
|
|
55
|
+
`,
|
|
56
|
+
implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)`,
|
|
57
|
+
});
|
|
70
58
|
}
|
|
71
|
-
generateMutationHook(
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
77
|
-
return `export const use${operationName} = <
|
|
78
|
-
TError = ${this.visitor.config.errorType},
|
|
79
|
-
TContext = unknown
|
|
80
|
-
>(
|
|
59
|
+
generateMutationHook(config) {
|
|
60
|
+
const { generateBaseMutationHook, variables, options } = this.generateMutationHelper(config);
|
|
61
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
62
|
+
return generateBaseMutationHook({
|
|
63
|
+
implArguments: `
|
|
81
64
|
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
82
65
|
${options}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)(),
|
|
87
|
-
options
|
|
88
|
-
);`;
|
|
66
|
+
`,
|
|
67
|
+
implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)()`,
|
|
68
|
+
});
|
|
89
69
|
}
|
|
90
|
-
generateFetcherFetch(
|
|
91
|
-
const
|
|
70
|
+
generateFetcherFetch(config) {
|
|
71
|
+
const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
|
|
72
|
+
const variables = this.generateQueryVariablesSignature(config);
|
|
92
73
|
return `\nuse${operationName}.fetcher = (dataSource: { endpoint: string, fetchParams?: RequestInit }, ${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables);`;
|
|
93
74
|
}
|
|
94
75
|
}
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GraphQLRequestClientFetcher = void 0;
|
|
4
|
-
const
|
|
5
|
-
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
6
|
+
const fetcher_js_1 = require("./fetcher.js");
|
|
7
|
+
class GraphQLRequestClientFetcher extends fetcher_js_1.FetcherRenderer {
|
|
6
8
|
constructor(visitor, config) {
|
|
9
|
+
super(visitor);
|
|
7
10
|
this.visitor = visitor;
|
|
8
11
|
this.clientPath = typeof config === 'object' ? config.clientImportPath : null;
|
|
12
|
+
(0, auto_bind_1.default)(this);
|
|
9
13
|
}
|
|
10
|
-
|
|
14
|
+
generateFetcherImplementation() {
|
|
11
15
|
return this.clientPath
|
|
12
16
|
? `
|
|
13
17
|
function fetcher<TData, TVariables extends { [key: string]: any }>(query: string, variables?: TVariables, requestHeaders?: RequestInit['headers']) {
|
|
@@ -26,125 +30,87 @@ function fetcher<TData, TVariables extends { [key: string]: any }>(client: Graph
|
|
|
26
30
|
});
|
|
27
31
|
}`;
|
|
28
32
|
}
|
|
29
|
-
generateInfiniteQueryHook(
|
|
30
|
-
const variables = (0, variables_generator_js_1.generateQueryVariablesSignature)(hasRequiredVariables, operationVariablesTypes);
|
|
33
|
+
generateInfiniteQueryHook(config, isSuspense = false) {
|
|
31
34
|
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
32
35
|
if (this.clientPath)
|
|
33
36
|
this.visitor.imports.add(this.clientPath);
|
|
34
37
|
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
38
|
-
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
38
|
+
const { generateBaseInfiniteQueryHook, variables, options } = this.generateInfiniteQueryHelper(config, isSuspense);
|
|
39
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
39
40
|
return this.clientPath
|
|
40
|
-
?
|
|
41
|
-
|
|
42
|
-
TError = ${this.visitor.config.errorType}
|
|
43
|
-
>(
|
|
41
|
+
? generateBaseInfiniteQueryHook({
|
|
42
|
+
implArguments: `
|
|
44
43
|
pageParamKey: keyof ${operationVariablesTypes},
|
|
45
44
|
${variables},
|
|
46
45
|
${options},
|
|
47
46
|
headers?: RequestInit['headers']
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
);`
|
|
54
|
-
: `export const useInfinite${operationName} = <
|
|
55
|
-
TData = ${operationResultType},
|
|
56
|
-
TError = ${this.visitor.config.errorType}
|
|
57
|
-
>(
|
|
47
|
+
`,
|
|
48
|
+
implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, [pageParamKey]: metaData.pageParam}, headers)()`,
|
|
49
|
+
})
|
|
50
|
+
: generateBaseInfiniteQueryHook({
|
|
51
|
+
implArguments: `
|
|
58
52
|
client: GraphQLClient,
|
|
59
53
|
${variables},
|
|
60
54
|
${options},
|
|
61
55
|
headers?: RequestInit['headers']
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})}, headers)(),
|
|
66
|
-
options
|
|
67
|
-
);`;
|
|
56
|
+
`,
|
|
57
|
+
implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})}, headers)()`,
|
|
58
|
+
});
|
|
68
59
|
}
|
|
69
|
-
generateQueryHook(
|
|
70
|
-
const variables = (0, variables_generator_js_1.generateQueryVariablesSignature)(hasRequiredVariables, operationVariablesTypes);
|
|
60
|
+
generateQueryHook(config, isSuspense = false) {
|
|
71
61
|
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
72
62
|
if (this.clientPath)
|
|
73
63
|
this.visitor.imports.add(this.clientPath);
|
|
74
64
|
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
75
65
|
this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
79
|
-
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
66
|
+
const { generateBaseQueryHook, variables, options } = this.generateQueryHelper(config, isSuspense);
|
|
67
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
80
68
|
return this.clientPath
|
|
81
|
-
?
|
|
82
|
-
|
|
83
|
-
TError = ${this.visitor.config.errorType}
|
|
84
|
-
>(
|
|
69
|
+
? generateBaseQueryHook({
|
|
70
|
+
implArguments: `
|
|
85
71
|
${variables},
|
|
86
72
|
${options},
|
|
87
73
|
headers?: RequestInit['headers']
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
);`
|
|
94
|
-
: `export const use${operationName} = <
|
|
95
|
-
TData = ${operationResultType},
|
|
96
|
-
TError = ${this.visitor.config.errorType}
|
|
97
|
-
>(
|
|
74
|
+
`,
|
|
75
|
+
implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers)`,
|
|
76
|
+
})
|
|
77
|
+
: generateBaseQueryHook({
|
|
78
|
+
implArguments: `
|
|
98
79
|
client: GraphQLClient,
|
|
99
80
|
${variables},
|
|
100
81
|
${options},
|
|
101
82
|
headers?: RequestInit['headers']
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers),
|
|
106
|
-
options
|
|
107
|
-
);`;
|
|
83
|
+
`,
|
|
84
|
+
implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)`,
|
|
85
|
+
});
|
|
108
86
|
}
|
|
109
|
-
generateMutationHook(
|
|
110
|
-
const variables = `variables?: ${operationVariablesTypes}`;
|
|
87
|
+
generateMutationHook(config) {
|
|
111
88
|
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
112
89
|
if (this.clientPath)
|
|
113
90
|
this.visitor.imports.add(this.clientPath);
|
|
114
91
|
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
118
|
-
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
92
|
+
const { generateBaseMutationHook, variables, options } = this.generateMutationHelper(config);
|
|
93
|
+
const { documentVariableName, operationResultType, operationVariablesTypes } = config;
|
|
119
94
|
return this.clientPath
|
|
120
|
-
?
|
|
121
|
-
|
|
122
|
-
TContext = unknown
|
|
123
|
-
>(
|
|
95
|
+
? generateBaseMutationHook({
|
|
96
|
+
implArguments: `
|
|
124
97
|
${options},
|
|
125
98
|
headers?: RequestInit['headers']
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
);`
|
|
132
|
-
: `export const use${operationName} = <
|
|
133
|
-
TError = ${this.visitor.config.errorType},
|
|
134
|
-
TContext = unknown
|
|
135
|
-
>(
|
|
99
|
+
`,
|
|
100
|
+
implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers)()`,
|
|
101
|
+
})
|
|
102
|
+
: generateBaseMutationHook({
|
|
103
|
+
implArguments: `
|
|
136
104
|
client: GraphQLClient,
|
|
137
105
|
${options},
|
|
138
106
|
headers?: RequestInit['headers']
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)(),
|
|
143
|
-
options
|
|
144
|
-
);`;
|
|
107
|
+
`,
|
|
108
|
+
implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)()`,
|
|
109
|
+
});
|
|
145
110
|
}
|
|
146
|
-
generateFetcherFetch(
|
|
147
|
-
const
|
|
111
|
+
generateFetcherFetch(config) {
|
|
112
|
+
const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
|
|
113
|
+
const variables = this.generateQueryVariablesSignature(config);
|
|
148
114
|
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
149
115
|
if (this.clientPath)
|
|
150
116
|
this.visitor.imports.add(this.clientPath);
|