Skip to content

Commit 1b9e6e9

Browse files
committed
fix(autogenSWRKey): fix type error
1 parent fc41627 commit 1b9e6e9

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/visitor.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ export class SWRVisitor extends ClientSideBaseVisitor<
133133
o.operationResultType
134134
}>}>) {
135135
return useSWR<SWRRawResponse<${o.operationResultType}>>(${
136-
autogenSWRKey ? `genKey('${pascalName}', variables)` : 'key'
136+
autogenSWRKey
137+
? `genKey<${o.operationVariablesTypes}>('${pascalName}', variables)`
138+
: 'key'
137139
}, () => sdk.${o.node.name.value}(variables), config);
138140
}`)
139141

@@ -161,7 +163,9 @@ export class SWRVisitor extends ClientSideBaseVisitor<
161163
o.operationVariablesTypes
162164
}, config?: SWRConfigInterface<${o.operationResultType}>) {
163165
return useSWR<${o.operationResultType}>(${
164-
autogenSWRKey ? `genKey('${pascalName}', variables)` : 'key'
166+
autogenSWRKey
167+
? `genKey<${o.operationVariablesTypes}>('${pascalName}', variables)`
168+
: 'key'
165169
}, () => sdk.${o.node.name.value}(variables), config);
166170
}`)
167171

@@ -202,7 +206,7 @@ export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionW
202206
const sdk = getSdk(client, withWrapper);
203207
${
204208
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'
209+
? ' const genKey = <V extends Record<string, unknown> = Record<string, unknown>>(name: string, object: V = {} as V): SWRKeyInterface => [name, ...Object.keys(object).sort().map(key => object[key])];\n'
206210
: ''
207211
} return {
208212
...sdk,

tests/swr.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,20 +333,20 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
333333
expect(output).toContain(
334334
`export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
335335
const sdk = getSdk(client, withWrapper);
336-
const genKey = <V extends Record<string, unknown> = Record<string, unknown>>(name: string, object?: V): SWRKeyInterface => [name, ...Object.keys(object || {}).sort().map(key => object[key])];
336+
const genKey = <V extends Record<string, unknown> = Record<string, unknown>>(name: string, object: V = {} as V): SWRKeyInterface => [name, ...Object.keys(object).sort().map(key => object[key])];
337337
return {
338338
...sdk,
339339
useFeed(variables?: FeedQueryVariables, config?: SWRConfigInterface<FeedQuery>) {
340-
return useSWR<FeedQuery>(genKey('Feed', variables), () => sdk.feed(variables), config);
340+
return useSWR<FeedQuery>(genKey<FeedQueryVariables>('Feed', variables), () => sdk.feed(variables), config);
341341
},
342342
useFeed2(variables: Feed2QueryVariables, config?: SWRConfigInterface<Feed2Query>) {
343-
return useSWR<Feed2Query>(genKey('Feed2', variables), () => sdk.feed2(variables), config);
343+
return useSWR<Feed2Query>(genKey<Feed2QueryVariables>('Feed2', variables), () => sdk.feed2(variables), config);
344344
},
345345
useFeed3(variables?: Feed3QueryVariables, config?: SWRConfigInterface<Feed3Query>) {
346-
return useSWR<Feed3Query>(genKey('Feed3', variables), () => sdk.feed3(variables), config);
346+
return useSWR<Feed3Query>(genKey<Feed3QueryVariables>('Feed3', variables), () => sdk.feed3(variables), config);
347347
},
348348
useFeed4(variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query>) {
349-
return useSWR<Feed4Query>(genKey('Feed4', variables), () => sdk.feed4(variables), config);
349+
return useSWR<Feed4Query>(genKey<Feed4QueryVariables>('Feed4', variables), () => sdk.feed4(variables), config);
350350
}
351351
};
352352
}

0 commit comments

Comments
 (0)