Skip to content

Commit cd8fbe8

Browse files
committed
Add useSWRInfinite option
1 parent cac4a35 commit cd8fbe8

File tree

2 files changed

+93
-14
lines changed

2 files changed

+93
-14
lines changed

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ export interface RawSWRPluginConfig extends RawClientSideBasePluginConfig {
2424
*/
2525
rawRequest?: boolean
2626
exclude?: string | string[]
27+
useSWRInfinite?: string | string[]
2728
}

src/visitor.ts

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { RawSWRPluginConfig } from './config'
1414
export interface SWRPluginConfig extends ClientSideBasePluginConfig {
1515
rawRequest: boolean
1616
exclude: string | string[]
17+
useSWRInfinite: string | string[]
1718
}
1819

1920
export class SWRVisitor extends ClientSideBaseVisitor<
@@ -28,22 +29,44 @@ export class SWRVisitor extends ClientSideBaseVisitor<
2829
operationVariablesTypes: string
2930
}[] = []
3031

32+
private _enabledInfinite = false
33+
3134
constructor(
3235
schema: GraphQLSchema,
3336
fragments: LoadedFragment[],
3437
rawConfig: RawSWRPluginConfig
3538
) {
3639
super(schema, fragments, rawConfig, {
3740
exclude: rawConfig.exclude || null,
41+
useSWRInfinite: rawConfig.useSWRInfinite || null,
3842
})
3943

44+
this._enabledInfinite =
45+
(this.config.useSWRInfinite &&
46+
typeof this.config.useSWRInfinite === 'string') ||
47+
(Array.isArray(this.config.useSWRInfinite) &&
48+
this.config.useSWRInfinite.length > 0)
49+
4050
autoBind(this)
4151

4252
if (this.config.useTypeImports) {
53+
if (this._enabledInfinite) {
54+
this._additionalImports.push(
55+
`import type { ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface, SWRInfiniteConfigInterface } from 'swr';`
56+
)
57+
this._additionalImports.push(
58+
`import useSWR, { useSWRInfinite } from 'swr';`
59+
)
60+
} else {
61+
this._additionalImports.push(
62+
`import type { ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface } from 'swr';`
63+
)
64+
this._additionalImports.push(`import useSWR from 'swr';`)
65+
}
66+
} else if (this._enabledInfinite) {
4367
this._additionalImports.push(
44-
`import type { ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface } from 'swr';`
68+
`import useSWR, { useSWRInfinite, ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface, SWRInfiniteConfigInterface } from 'swr';`
4569
)
46-
this._additionalImports.push(`import useSWR from 'swr';`)
4770
} else {
4871
this._additionalImports.push(
4972
`import useSWR, { ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface } from 'swr';`
@@ -70,13 +93,15 @@ export class SWRVisitor extends ClientSideBaseVisitor<
7093
}
7194

7295
public get sdkContent(): string {
96+
const { exclude } = this.config
97+
const disabledExclude =
98+
!exclude || (Array.isArray(exclude) && !exclude.length)
7399
const allPossibleActions = this._operationsToInclude
74100
.filter((o) => {
75101
if (o.operationType !== 'Query') {
76102
return false
77103
}
78-
const { exclude } = this.config
79-
if (!exclude || (Array.isArray(exclude) && !exclude.length)) {
104+
if (disabledExclude) {
80105
return true
81106
}
82107
const name = o.node.name.value
@@ -89,34 +114,87 @@ export class SWRVisitor extends ClientSideBaseVisitor<
89114
o.node.variableDefinitions.every(
90115
(v) => v.type.kind !== Kind.NON_NULL_TYPE || v.defaultValue
91116
)
117+
const name = o.node.name.value
118+
const enabledInfinite =
119+
this._enabledInfinite &&
120+
glob.isMatch(name, this.config.useSWRInfinite)
121+
const codes: string[] = []
92122

93123
if (this.config.rawRequest) {
94-
return `use${pascalCase(
124+
codes.push(`use${pascalCase(
95125
o.node.name.value
96126
)}(key: SWRKeyInterface, variables${optionalVariables ? '?' : ''}: ${
97127
o.operationVariablesTypes
98-
}, config?: SWRConfigInterface<${o.operationResultType}>) {
99-
return useSWR<{ data?: ${
128+
}, config?: SWRConfigInterface<SWRRawResponse<${
129+
o.operationResultType
130+
}>}>) {
131+
return useSWR<SWRRawResponse<${
100132
o.operationResultType
101-
} | undefined; extensions?: any; headers: Headers; status: number; errors?: GraphQLError[] | undefined; }>(key, () => sdk.${
102-
o.node.name.value
103-
}(variables), config);
104-
}`
133+
}>>(key, () => sdk.${o.node.name.value}(variables), config);
134+
}`)
135+
136+
if (enabledInfinite) {
137+
codes.push(`use${pascalCase(
138+
o.node.name.value
139+
)}Infinite(getKey: SWRInfiniteKeyLoader<SWRRawResponse<${
140+
o.operationResultType
141+
}>>, variables${optionalVariables ? '?' : ''}: ${
142+
o.operationVariablesTypes
143+
}, config?: SWRInfiniteConfigInterface<SWRRawResponse<${
144+
o.operationResultType
145+
}>>) {
146+
return useSWRInfinite<SWRRawResponse<${
147+
o.operationResultType
148+
}>>(getKey, () => sdk.${o.node.name.value}(variables), config);
149+
}`)
150+
}
151+
return codes
105152
}
106153

107-
return `use${pascalCase(
154+
codes.push(`use${pascalCase(
108155
o.node.name.value
109156
)}(key: SWRKeyInterface, variables${optionalVariables ? '?' : ''}: ${
110157
o.operationVariablesTypes
111158
}, config?: SWRConfigInterface<${o.operationResultType}>) {
112159
return useSWR<${o.operationResultType}>(key, () => sdk.${
113160
o.node.name.value
114161
}(variables), config);
115-
}`
162+
}`)
163+
164+
if (enabledInfinite) {
165+
codes.push(`use${pascalCase(
166+
o.node.name.value
167+
)}Infinite(getKey: SWRInfiniteKeyLoader<${
168+
o.operationResultType
169+
}>, variables${optionalVariables ? '?' : ''}: ${
170+
o.operationVariablesTypes
171+
}, config?: SWRInfiniteConfigInterface<${o.operationResultType}>) {
172+
return useSWRInfinite<${o.operationResultType}>(getKey, () => sdk.${
173+
o.node.name.value
174+
}(variables), config);
175+
}`)
176+
}
177+
178+
return codes
116179
})
180+
.reduce((p, c) => p.concat(c), [])
117181
.map((s) => indentMultiline(s, 2))
118182

119-
return `export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
183+
const types: string[] = []
184+
if (this.config.rawRequest) {
185+
types.push(
186+
`type SWRRawResponse<Data = any> = { data?: Data | undefined; extensions?: any; headers: Headers; status: number; errors?: GraphQLError[] | undefined; };`
187+
)
188+
}
189+
if (this._enabledInfinite) {
190+
types.push(`export type SWRInfiniteKeyLoader<Data = any> = (
191+
index: number,
192+
previousPageData: Data | null
193+
) => string | any[] | null;`)
194+
}
195+
196+
return `${types.join('\n')}
197+
export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
120198
const sdk = getSdk(client, withWrapper);
121199
return {
122200
...sdk,

0 commit comments

Comments
 (0)