@@ -15,6 +15,7 @@ export interface SWRPluginConfig extends ClientSideBasePluginConfig {
1515 rawRequest : boolean
1616 excludeQueries : string | string [ ]
1717 useSWRInfinite : string | string [ ]
18+ autogenSWRKey : boolean
1819}
1920
2021export class SWRVisitor extends ClientSideBaseVisitor <
@@ -39,6 +40,7 @@ export class SWRVisitor extends ClientSideBaseVisitor<
3940 super ( schema , fragments , rawConfig , {
4041 excludeQueries : rawConfig . excludeQueries || null ,
4142 useSWRInfinite : rawConfig . useSWRInfinite || null ,
43+ autogenSWRKey : rawConfig . autogenSWRKey || false ,
4244 } )
4345
4446 this . _enabledInfinite =
@@ -93,7 +95,7 @@ export class SWRVisitor extends ClientSideBaseVisitor<
9395 }
9496
9597 public get sdkContent ( ) : string {
96- const { excludeQueries } = this . config
98+ const { excludeQueries, autogenSWRKey } = this . config
9799 const disabledexcludeQueries =
98100 ! excludeQueries ||
99101 ( Array . isArray ( excludeQueries ) && ! excludeQueries . length )
@@ -116,22 +118,23 @@ export class SWRVisitor extends ClientSideBaseVisitor<
116118 ( v ) => v . type . kind !== Kind . NON_NULL_TYPE || v . defaultValue
117119 )
118120 const name = o . node . name . value
121+ const pascalName = pascalCase ( o . node . name . value )
119122 const enabledInfinite =
120123 this . _enabledInfinite &&
121124 glob . isMatch ( name , this . config . useSWRInfinite )
122125 const codes : string [ ] = [ ]
123126
124127 if ( this . config . rawRequest ) {
125- codes . push ( `use${ pascalCase (
126- o . node . name . value
127- ) } (key: SWRKeyInterface, variables${ optionalVariables ? '?' : '' } : ${
128+ codes . push ( `use${ pascalCase ( o . node . name . value ) } ( ${
129+ autogenSWRKey ? '' : 'key: SWRKeyInterface, '
130+ } variables${ optionalVariables ? '?' : '' } : ${
128131 o . operationVariablesTypes
129132 } , config?: SWRConfigInterface<SWRRawResponse<${
130133 o . operationResultType
131134 } >}>) {
132- return useSWR<SWRRawResponse<${
133- o . operationResultType
134- } >>(key , () => sdk.${ o . node . name . value } (variables), config);
135+ return useSWR<SWRRawResponse<${ o . operationResultType } >>( ${
136+ autogenSWRKey ? `genKey(' ${ pascalName } ', variables)` : 'key'
137+ } , () => sdk.${ o . node . name . value } (variables), config);
135138 }` )
136139
137140 if ( enabledInfinite ) {
@@ -152,14 +155,14 @@ export class SWRVisitor extends ClientSideBaseVisitor<
152155 return codes
153156 }
154157
155- codes . push ( `use${ pascalCase (
156- o . node . name . value
157- ) } (key: SWRKeyInterface, variables${ optionalVariables ? '?' : '' } : ${
158+ codes . push ( `use${ pascalName } ( ${
159+ autogenSWRKey ? '' : 'key: SWRKeyInterface, '
160+ } variables${ optionalVariables ? '?' : '' } : ${
158161 o . operationVariablesTypes
159162 } , config?: SWRConfigInterface<${ o . operationResultType } >) {
160- return useSWR<${ o . operationResultType } >(key, () => sdk. ${
161- o . node . name . value
162- } (variables), config);
163+ return useSWR<${ o . operationResultType } >(${
164+ autogenSWRKey ? `genKey(' ${ pascalName } ', variables)` : 'key'
165+ } , () => sdk. ${ o . node . name . value } (variables), config);
163166}` )
164167
165168 if ( enabledInfinite ) {
@@ -197,7 +200,11 @@ export class SWRVisitor extends ClientSideBaseVisitor<
197200 return `${ types . join ( '\n' ) }
198201export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
199202 const sdk = getSdk(client, withWrapper);
200- return {
203+ ${
204+ autogenSWRKey
205+ ? ' const genKey = <V extends Record<string, unknown> = Record<string, unknown>>(name: string, object?: V): SWRKeyInterface => [name, ...Object.keys(object || {}).sort().map(key => object[key])];\n'
206+ : ''
207+ } return {
201208 ...sdk,
202209${ allPossibleActions . join ( ',\n' ) }
203210 };
0 commit comments