1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.CustomMapperFetcher = void 0;
|
4 | const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
5 | const variables_generator_js_1 = require("./variables-generator.js");
|
6 | class CustomMapperFetcher {
|
7 | constructor(visitor, customFetcher) {
|
8 | this.visitor = visitor;
|
9 | if (typeof customFetcher === 'string') {
|
10 | customFetcher = { func: customFetcher };
|
11 | }
|
12 | this._mapper = (0, visitor_plugin_common_1.parseMapper)(customFetcher.func);
|
13 | this._isReactHook = customFetcher.isReactHook;
|
14 | }
|
15 | getFetcherFnName(operationResultType, operationVariablesTypes) {
|
16 | return `${this._mapper.type}<${operationResultType}, ${operationVariablesTypes}>`;
|
17 | }
|
18 | generateFetcherImplementaion() {
|
19 | if (this._mapper.isExternal) {
|
20 | return (0, visitor_plugin_common_1.buildMapperImport)(this._mapper.source, [
|
21 | {
|
22 | identifier: this._mapper.type,
|
23 | asDefault: this._mapper.default,
|
24 | },
|
25 | ], this.visitor.config.useTypeImports);
|
26 | }
|
27 | return null;
|
28 | }
|
29 | generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
30 | const pageParamKey = `pageParamKey: keyof ${operationVariablesTypes}`;
|
31 | const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
32 | const hookConfig = this.visitor.queryMethodMap;
|
33 | this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
34 | this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
35 | const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
36 | const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
37 | const implHookOuter = this._isReactHook ? `const query = ${typedFetcher}(${documentVariableName})` : '';
|
38 | const impl = this._isReactHook
|
39 | ? `(metaData) => query({...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})})`
|
40 | : `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})})()`;
|
41 | return `export const useInfinite${operationName} = <
|
42 | TData = ${operationResultType},
|
43 | TError = ${this.visitor.config.errorType}
|
44 | >(
|
45 | ${pageParamKey},
|
46 | ${variables},
|
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 | )};`;
|
55 | }
|
56 | generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
57 | const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
58 | const hookConfig = this.visitor.queryMethodMap;
|
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>`;
|
62 | const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
63 | const impl = this._isReactHook
|
64 | ? `${typedFetcher}(${documentVariableName}).bind(null, variables)`
|
65 | : `${typedFetcher}(${documentVariableName}, variables)`;
|
66 | return `export const use${operationName} = <
|
67 | TData = ${operationResultType},
|
68 | TError = ${this.visitor.config.errorType}
|
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 | );`;
|
78 | }
|
79 | generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
80 | const variables = `variables?: ${operationVariablesTypes}`;
|
81 | const hookConfig = this.visitor.queryMethodMap;
|
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>`;
|
85 | const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
86 | const impl = this._isReactHook
|
87 | ? `${typedFetcher}(${documentVariableName})`
|
88 | : `(${variables}) => ${typedFetcher}(${documentVariableName}, variables)()`;
|
89 | return `export const use${operationName} = <
|
90 | TError = ${this.visitor.config.errorType},
|
91 | TContext = unknown
|
92 | >(${options}) =>
|
93 | ${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
94 | ${(0, variables_generator_js_1.generateMutationKey)(node)},
|
95 | ${impl},
|
96 | options
|
97 | );`;
|
98 | }
|
99 | generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
100 |
|
101 |
|
102 | if (this._isReactHook)
|
103 | return '';
|
104 | const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
105 | const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
106 | const impl = `${typedFetcher}(${documentVariableName}, variables, options)`;
|
107 | return `\nuse${operationName}.fetcher = (${variables}, options?: RequestInit['headers']) => ${impl};`;
|
108 | }
|
109 | }
|
110 | exports.CustomMapperFetcher = CustomMapperFetcher;
|