Skip to content

Commit

Permalink
feat: Support the types prefix and suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
croutonn committed Jan 21, 2021
1 parent 50a7f57 commit 7f6c0a7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
21 changes: 15 additions & 6 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ClientSideBaseVisitor,
indentMultiline,
LoadedFragment,
ParsedConfig,
} from '@graphql-codegen/visitor-plugin-common'
import autoBind from 'auto-bind'
import { GraphQLSchema, Kind, OperationDefinitionNode } from 'graphql'
Expand All @@ -27,9 +28,11 @@ export interface Operation {
}

export interface ComposeQueryHandlerConfig {
autogenKey: boolean
autogenKey: SWRPluginConfig['autogenSWRKey']
infinite: boolean
rawRequest: boolean
rawRequest: SWRPluginConfig['rawRequest']
typesPrefix: ParsedConfig['typesPrefix']
typesSuffix: ParsedConfig['typesSuffix']
}

const composeQueryHandler = (
Expand Down Expand Up @@ -66,7 +69,9 @@ const composeQueryHandler = (
if (config.infinite) {
codes.push(`use${pascalName}Infinite(${
config.autogenKey ? '' : 'id: string, '
}getKey: SWRInfiniteKeyLoader<${responseType}, ${variablesType}>, variables${optionalVariables}: ${variablesType}, config?: SWRInfiniteConfigInterface<${responseType}, ClientError>) {
}getKey: ${config.typesPrefix}SWRInfiniteKeyLoader${
config.typesSuffix
}<${responseType}, ${variablesType}>, variables${optionalVariables}: ${variablesType}, config?: SWRInfiniteConfigInterface<${responseType}, ClientError>) {
return useSWRInfinite<${responseType}, ClientError>(
utilsForInfinite.generateGetKey<${responseType}, ${variablesType}>(${
config.autogenKey
Expand Down Expand Up @@ -180,6 +185,8 @@ export class SWRVisitor extends ClientSideBaseVisitor<
this._enabledInfinite &&
glob.isMatch(o.node.name.value, config.useSWRInfinite),
rawRequest: config.rawRequest,
typesPrefix: config.typesPrefix,
typesSuffix: config.typesSuffix,
})
)
.reduce((p, c) => p.concat(c), [])
Expand All @@ -194,7 +201,7 @@ export class SWRVisitor extends ClientSideBaseVisitor<

// Add type of SWRInfiniteKeyLoader
if (this._enabledInfinite) {
codes.push(`export type SWRInfiniteKeyLoader<Data = unknown, Variables = unknown> = (
codes.push(`export type ${config.typesPrefix}SWRInfiniteKeyLoader${config.typesSuffix}<Data = unknown, Variables = unknown> = (
index: number,
previousPageData: Data | null
) => [keyof Variables, Variables[keyof Variables] | null] | null;`)
Expand All @@ -209,7 +216,7 @@ export class SWRVisitor extends ClientSideBaseVisitor<
codes.push(` const utilsForInfinite = {
generateGetKey: <Data = unknown, Variables = unknown>(
id: string,
getKey: SWRInfiniteKeyLoader<Data, Variables>
getKey: ${config.typesPrefix}SWRInfiniteKeyLoader${config.typesSuffix}<Data, Variables>
) => (pageIndex: number, previousData: Data | null) => {
const key = getKey(pageIndex, previousData)
return key ? [id, ...key] : null
Expand Down Expand Up @@ -237,7 +244,9 @@ ${allPossibleActions.join(',\n')}
}`)

// Add type of Sdk
codes.push(`export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`)
codes.push(
`export type ${config.typesPrefix}SdkWithHooks${config.typesSuffix} = ReturnType<typeof getSdkWithHooks>;`
)

return codes.join('\n')
}
Expand Down
32 changes: 29 additions & 3 deletions tests/swr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ type PluginsConfig = Partial<
RawSWRPluginConfig
>

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

describe('SWR', () => {
const schema = buildClientSchema(require('../dev-test/githunt/schema.json'))
Expand Down Expand Up @@ -274,4 +273,31 @@ async function test() {
)
expect(output).toContain(readOutput('rawRequest'))
})

it('Should work `typesPrefix` and `typesSuffix` option correctly', async () => {
const typesPrefix = 'P'
const typesSuffix = 'S'
const config: PluginsConfig = {
typesPrefix,
typesSuffix,
useSWRInfinite: ['feed'],
}
const docs = [{ location: '', document: basicDoc }]

const content = (await plugin(schema, docs, config, {
outputFile: 'graphql.ts',
})) as Types.ComplexPluginOutput

const usage = basicUsage
const output = await validate(content, config, docs, schema, usage)
expect(output).toContain(
`export type ${typesPrefix}SWRInfiniteKeyLoader${typesSuffix}<Data = unknown, Variables = unknown>`
)
expect(output).toContain(
`getKey: ${typesPrefix}SWRInfiniteKeyLoader${typesSuffix}<${typesPrefix}FeedQuery${typesSuffix}, ${typesPrefix}FeedQueryVariables${typesSuffix}>`
)
expect(output).toContain(
`export type ${typesPrefix}SdkWithHooks${typesSuffix} =`
)
})
})

0 comments on commit 7f6c0a7

Please sign in to comment.