33 ClientSideBaseVisitor ,
44 indentMultiline ,
55 LoadedFragment ,
6+ ParsedConfig ,
67} from '@graphql-codegen/visitor-plugin-common'
78import autoBind from 'auto-bind'
89import { GraphQLSchema , Kind , OperationDefinitionNode } from 'graphql'
@@ -27,9 +28,11 @@ export interface Operation {
2728}
2829
2930export interface ComposeQueryHandlerConfig {
30- autogenKey : boolean
31+ autogenKey : SWRPluginConfig [ 'autogenSWRKey' ]
3132 infinite : boolean
32- rawRequest : boolean
33+ rawRequest : SWRPluginConfig [ 'rawRequest' ]
34+ typesPrefix : ParsedConfig [ 'typesPrefix' ]
35+ typesSuffix : ParsedConfig [ 'typesSuffix' ]
3336}
3437
3538const composeQueryHandler = (
@@ -66,7 +69,9 @@ const composeQueryHandler = (
6669 if ( config . infinite ) {
6770 codes . push ( `use${ pascalName } Infinite(${
6871 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>) {
7075 return useSWRInfinite<${ responseType } , ClientError>(
7176 utilsForInfinite.generateGetKey<${ responseType } , ${ variablesType } >(${
7277 config . autogenKey
@@ -180,6 +185,8 @@ export class SWRVisitor extends ClientSideBaseVisitor<
180185 this . _enabledInfinite &&
181186 glob . isMatch ( o . node . name . value , config . useSWRInfinite ) ,
182187 rawRequest : config . rawRequest ,
188+ typesPrefix : config . typesPrefix ,
189+ typesSuffix : config . typesSuffix ,
183190 } )
184191 )
185192 . reduce ( ( p , c ) => p . concat ( c ) , [ ] )
@@ -194,7 +201,7 @@ export class SWRVisitor extends ClientSideBaseVisitor<
194201
195202 // Add type of SWRInfiniteKeyLoader
196203 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> = (
198205 index: number,
199206 previousPageData: Data | null
200207) => [keyof Variables, Variables[keyof Variables] | null] | null;` )
@@ -209,7 +216,7 @@ export class SWRVisitor extends ClientSideBaseVisitor<
209216 codes . push ( ` const utilsForInfinite = {
210217 generateGetKey: <Data = unknown, Variables = unknown>(
211218 id: string,
212- getKey: SWRInfiniteKeyLoader<Data, Variables>
219+ getKey: ${ config . typesPrefix } SWRInfiniteKeyLoader${ config . typesSuffix } <Data, Variables>
213220 ) => (pageIndex: number, previousData: Data | null) => {
214221 const key = getKey(pageIndex, previousData)
215222 return key ? [id, ...key] : null
@@ -237,7 +244,9 @@ ${allPossibleActions.join(',\n')}
237244}` )
238245
239246 // 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+ )
241250
242251 return codes . join ( '\n' )
243252 }
0 commit comments