-
Notifications
You must be signed in to change notification settings - Fork 132
/
fetcher-custom-mapper.ts
165 lines (142 loc) · 6.25 KB
/
fetcher-custom-mapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { ParsedMapper, buildMapperImport, parseMapper } from '@graphql-codegen/visitor-plugin-common';
import { generateInfiniteQueryKey, generateMutationKey, generateQueryKey } from './variables-generator.js';
import { CustomFetch } from './config.js';
import { FetcherRenderer } from './fetcher.js';
import { OperationDefinitionNode } from 'graphql';
import { ReactQueryVisitor } from './visitor.js';
export class CustomMapperFetcher implements FetcherRenderer {
private _mapper: ParsedMapper;
private _isReactHook: boolean;
constructor(private visitor: ReactQueryVisitor, customFetcher: CustomFetch) {
if (typeof customFetcher === 'string') {
customFetcher = { func: customFetcher };
}
this._mapper = parseMapper(customFetcher.func);
this._isReactHook = customFetcher.isReactHook;
}
private getFetcherFnName(operationResultType: string, operationVariablesTypes: string): string {
return `${this._mapper.type}<${operationResultType}, ${operationVariablesTypes}>`;
}
generateFetcherImplementaion(): string {
if (this._mapper.isExternal) {
return buildMapperImport(
this._mapper.source,
[
{
identifier: this._mapper.type,
asDefault: this._mapper.default,
},
],
this.visitor.config.useTypeImports
);
}
return null;
}
generateInfiniteQueryHook(
node: OperationDefinitionNode,
documentVariableName: string,
operationName: string,
operationResultType: string,
operationVariablesTypes: string,
hasRequiredVariables: boolean
): string {
const pageParamKey = `pageParamKey: keyof ${operationVariablesTypes}`;
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
const hookConfig = this.visitor.queryMethodMap;
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
const implHookOuter = this._isReactHook ? `const query = ${typedFetcher}(${documentVariableName})` : '';
const impl = this._isReactHook
? `(metaData) => query({...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})})`
: `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, ...(metaData.pageParam ? {[pageParamKey]: metaData.pageParam} : {})})()`;
return `export const useInfinite${operationName} = <
TData = ${operationResultType},
TError = ${this.visitor.config.errorType}
>(
${pageParamKey},
${variables},
${options}
) =>{
${implHookOuter}
return ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
${generateInfiniteQueryKey(node, hasRequiredVariables)},
${impl},
options
)};`;
}
generateQueryHook(
node: OperationDefinitionNode,
documentVariableName: string,
operationName: string,
operationResultType: string,
operationVariablesTypes: string,
hasRequiredVariables: boolean
): string {
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
const hookConfig = this.visitor.queryMethodMap;
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
const impl = this._isReactHook
? `${typedFetcher}(${documentVariableName}).bind(null, variables)`
: `${typedFetcher}(${documentVariableName}, variables)`;
return `export const use${operationName} = <
TData = ${operationResultType},
TError = ${this.visitor.config.errorType}
>(
${variables},
${options}
) =>
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
${generateQueryKey(node, hasRequiredVariables)},
${impl},
options
);`;
}
generateMutationHook(
node: OperationDefinitionNode,
documentVariableName: string,
operationName: string,
operationResultType: string,
operationVariablesTypes: string,
hasRequiredVariables: boolean
): string {
const variables = `variables?: ${operationVariablesTypes}`;
const hookConfig = this.visitor.queryMethodMap;
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook);
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
const impl = this._isReactHook
? `${typedFetcher}(${documentVariableName})`
: `(${variables}) => ${typedFetcher}(${documentVariableName}, variables)()`;
return `export const use${operationName} = <
TError = ${this.visitor.config.errorType},
TContext = unknown
>(${options}) =>
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
${generateMutationKey(node)},
${impl},
options
);`;
}
generateFetcherFetch(
node: OperationDefinitionNode,
documentVariableName: string,
operationName: string,
operationResultType: string,
operationVariablesTypes: string,
hasRequiredVariables: boolean
): string {
// We can't generate a fetcher field since we can't call react hooks outside of a React Fucntion Component
// Related: https://reactjs.org/docs/hooks-rules.html
if (this._isReactHook) return '';
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
const impl = `${typedFetcher}(${documentVariableName}, variables, options)`;
return `\nuse${operationName}.fetcher = (${variables}, options?: RequestInit['headers']) => ${impl};`;
}
}