3
3
ClientSideBaseVisitor ,
4
4
indentMultiline ,
5
5
LoadedFragment ,
6
+ ParsedConfig ,
6
7
} from '@graphql-codegen/visitor-plugin-common'
7
8
import autoBind from 'auto-bind'
8
9
import { GraphQLSchema , Kind , OperationDefinitionNode } from 'graphql'
@@ -27,9 +28,11 @@ export interface Operation {
27
28
}
28
29
29
30
export interface ComposeQueryHandlerConfig {
30
- autogenKey : boolean
31
+ autogenKey : SWRPluginConfig [ 'autogenSWRKey' ]
31
32
infinite : boolean
32
- rawRequest : boolean
33
+ rawRequest : SWRPluginConfig [ 'rawRequest' ]
34
+ typesPrefix : ParsedConfig [ 'typesPrefix' ]
35
+ typesSuffix : ParsedConfig [ 'typesSuffix' ]
33
36
}
34
37
35
38
const composeQueryHandler = (
@@ -66,7 +69,9 @@ const composeQueryHandler = (
66
69
if ( config . infinite ) {
67
70
codes . push ( `use${ pascalName } Infinite(${
68
71
config . autogenKey ? '' : 'id: string, '
69
- } getKey: SWRInfiniteKeyLoader<${ responseType } , ${ variablesType } >, variables${ optionalVariables } : ${ variablesType } , config?: SWRInfiniteConfigInterface<${ responseType } , ClientError>) {
72
+ } getKey: ${ config . typesPrefix } SWRInfiniteKeyLoader${
73
+ config . typesSuffix
74
+ } <${ responseType } , ${ variablesType } >, variables${ optionalVariables } : ${ variablesType } , config?: SWRInfiniteConfigInterface<${ responseType } , ClientError>) {
70
75
return useSWRInfinite<${ responseType } , ClientError>(
71
76
utilsForInfinite.generateGetKey<${ responseType } , ${ variablesType } >(${
72
77
config . autogenKey
@@ -180,6 +185,8 @@ export class SWRVisitor extends ClientSideBaseVisitor<
180
185
this . _enabledInfinite &&
181
186
glob . isMatch ( o . node . name . value , config . useSWRInfinite ) ,
182
187
rawRequest : config . rawRequest ,
188
+ typesPrefix : config . typesPrefix ,
189
+ typesSuffix : config . typesSuffix ,
183
190
} )
184
191
)
185
192
. reduce ( ( p , c ) => p . concat ( c ) , [ ] )
@@ -194,7 +201,7 @@ export class SWRVisitor extends ClientSideBaseVisitor<
194
201
195
202
// Add type of SWRInfiniteKeyLoader
196
203
if ( this . _enabledInfinite ) {
197
- codes . push ( `export type SWRInfiniteKeyLoader<Data = unknown, Variables = unknown> = (
204
+ codes . push ( `export type ${ config . typesPrefix } SWRInfiniteKeyLoader${ config . typesSuffix } <Data = unknown, Variables = unknown> = (
198
205
index: number,
199
206
previousPageData: Data | null
200
207
) => [keyof Variables, Variables[keyof Variables] | null] | null;` )
@@ -209,7 +216,7 @@ export class SWRVisitor extends ClientSideBaseVisitor<
209
216
codes . push ( ` const utilsForInfinite = {
210
217
generateGetKey: <Data = unknown, Variables = unknown>(
211
218
id: string,
212
- getKey: SWRInfiniteKeyLoader<Data, Variables>
219
+ getKey: ${ config . typesPrefix } SWRInfiniteKeyLoader${ config . typesSuffix } <Data, Variables>
213
220
) => (pageIndex: number, previousData: Data | null) => {
214
221
const key = getKey(pageIndex, previousData)
215
222
return key ? [id, ...key] : null
@@ -237,7 +244,9 @@ ${allPossibleActions.join(',\n')}
237
244
}` )
238
245
239
246
// Add type of Sdk
240
- codes . push ( `export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;` )
247
+ codes . push (
248
+ `export type ${ config . typesPrefix } SdkWithHooks${ config . typesSuffix } = ReturnType<typeof getSdkWithHooks>;`
249
+ )
241
250
242
251
return codes . join ( '\n' )
243
252
}
0 commit comments