Skip to content

Commit 7f6c0a7

Browse files
committed
feat: Support the types prefix and suffix
1 parent 50a7f57 commit 7f6c0a7

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/visitor.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ClientSideBaseVisitor,
44
indentMultiline,
55
LoadedFragment,
6+
ParsedConfig,
67
} from '@graphql-codegen/visitor-plugin-common'
78
import autoBind from 'auto-bind'
89
import { GraphQLSchema, Kind, OperationDefinitionNode } from 'graphql'
@@ -27,9 +28,11 @@ export interface Operation {
2728
}
2829

2930
export 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

3538
const 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
}

tests/swr.spec.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ type PluginsConfig = Partial<
2525
RawSWRPluginConfig
2626
>
2727

28-
const readOutput = (name: string): string => {
29-
return fs.readFileSync(resolve(__dirname, `./outputs/${name}.ts`), 'utf-8')
30-
}
28+
const readOutput = (name: string): string =>
29+
fs.readFileSync(resolve(__dirname, `./outputs/${name}.ts`), 'utf-8')
3130

3231
describe('SWR', () => {
3332
const schema = buildClientSchema(require('../dev-test/githunt/schema.json'))
@@ -274,4 +273,31 @@ async function test() {
274273
)
275274
expect(output).toContain(readOutput('rawRequest'))
276275
})
276+
277+
it('Should work `typesPrefix` and `typesSuffix` option correctly', async () => {
278+
const typesPrefix = 'P'
279+
const typesSuffix = 'S'
280+
const config: PluginsConfig = {
281+
typesPrefix,
282+
typesSuffix,
283+
useSWRInfinite: ['feed'],
284+
}
285+
const docs = [{ location: '', document: basicDoc }]
286+
287+
const content = (await plugin(schema, docs, config, {
288+
outputFile: 'graphql.ts',
289+
})) as Types.ComplexPluginOutput
290+
291+
const usage = basicUsage
292+
const output = await validate(content, config, docs, schema, usage)
293+
expect(output).toContain(
294+
`export type ${typesPrefix}SWRInfiniteKeyLoader${typesSuffix}<Data = unknown, Variables = unknown>`
295+
)
296+
expect(output).toContain(
297+
`getKey: ${typesPrefix}SWRInfiniteKeyLoader${typesSuffix}<${typesPrefix}FeedQuery${typesSuffix}, ${typesPrefix}FeedQueryVariables${typesSuffix}>`
298+
)
299+
expect(output).toContain(
300+
`export type ${typesPrefix}SdkWithHooks${typesSuffix} =`
301+
)
302+
})
277303
})

0 commit comments

Comments
 (0)