Skip to content

Commit b657baf

Browse files
committed
feat: add autogenSWRKey option
re #29
1 parent 6190438 commit b657baf

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ config:
2424
useSWRInfinite:
2525
- hoge
2626
- bar{1,3}
27+
# generate keys automatically.
28+
# but, ​the cache may not work unless you separate the variables object into an external file and use it,
29+
# or use a primitive type for the value of each field.
30+
autogenSWRKey: true #(default: false)
2731
```
2832
2933
```typescript

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export interface RawSWRPluginConfig extends RawClientSideBasePluginConfig {
2525
rawRequest?: boolean
2626
excludeQueries?: string | string[]
2727
useSWRInfinite?: string | string[]
28+
autogenSWRKey?: boolean
2829
}

src/visitor.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2021
export 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')}
198201
export 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
};

tests/swr.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,40 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
317317
)
318318
})
319319
})
320+
321+
it('Should work `autogenSWRKey` option correctly', async () => {
322+
const config: PluginsConfig = {
323+
autogenSWRKey: true,
324+
}
325+
const docs = [{ location: '', document: basicDoc }]
326+
327+
const content = (await plugin(schema, docs, config, {
328+
outputFile: 'graphql.ts',
329+
})) as Types.ComplexPluginOutput
330+
331+
const usage = basicUsage
332+
const output = await validate(content, config, docs, schema, usage)
333+
expect(output).toContain(
334+
`export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
335+
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])];
337+
return {
338+
...sdk,
339+
useFeed(variables?: FeedQueryVariables, config?: SWRConfigInterface<FeedQuery>) {
340+
return useSWR<FeedQuery>(genKey('Feed', variables), () => sdk.feed(variables), config);
341+
},
342+
useFeed2(variables: Feed2QueryVariables, config?: SWRConfigInterface<Feed2Query>) {
343+
return useSWR<Feed2Query>(genKey('Feed2', variables), () => sdk.feed2(variables), config);
344+
},
345+
useFeed3(variables?: Feed3QueryVariables, config?: SWRConfigInterface<Feed3Query>) {
346+
return useSWR<Feed3Query>(genKey('Feed3', variables), () => sdk.feed3(variables), config);
347+
},
348+
useFeed4(variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query>) {
349+
return useSWR<Feed4Query>(genKey('Feed4', variables), () => sdk.feed4(variables), config);
350+
}
351+
};
352+
}
353+
export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
354+
)
355+
})
320356
})

0 commit comments

Comments
 (0)