Skip to content

Commit eed74fd

Browse files
committed
fix: Fix problem with wrapper function for useSWRInfinite not working
fix #69
1 parent 1fbfa71 commit eed74fd

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A [GraphQL code generator](https://graphql-code-generator.com/) plug-in that automatically generates utility functions for [SWR](https://swr.vercel.app/).
44

5-
# Example
5+
## Example
66

77
```yaml
88
# codegen.yml
@@ -56,11 +56,12 @@ export default sdk
5656

5757
```tsx
5858
// pages/posts/[slug].tsx
59-
import { GetStaticProps, GetStaticPropsResult } from 'next'
59+
import { GetStaticProps, NextPage } from 'next'
6060
import ErrorPage from 'next/error'
6161
import { useRouter } from 'next/router'
6262
import Article from '../components/Article'
6363
import sdk from '../sdk'
64+
import { GetArticleQuery } from '../graphql'
6465

6566
type StaticParams = { slug: string }
6667
type StaticProps = StaticParams & {
@@ -105,7 +106,7 @@ export const getStaticProps: GetStaticProps<StaticProps, StaticParams> = async (
105106
}
106107
})
107108

108-
export const ArticlePage = ({ slug, initialData, preview }: ArticlePageProps): JSX.Element => {
109+
export const ArticlePage: NextPage<ArticlePageProps> = ({ slug, initialData, preview }) => {
109110
const router = useRouter()
110111
const { data: { article }, mutate: mutateArticle } = sdk().useGetArticle(
111112
`UniqueKeyForTheRequest/${slug}`, { slug }, { initialData }
@@ -132,3 +133,20 @@ export const ArticlePage = ({ slug, initialData, preview }: ArticlePageProps): J
132133
)
133134
}
134135
```
136+
137+
### Example for useSWRInfinite
138+
139+
```tsx
140+
const { data, size, setSize } = sdk.useMyQueryInfinite(
141+
(pageIndex, previousPageData) => {
142+
if (previousPageData && !previousPageData.posts.length) {
143+
return null // reached the end
144+
}
145+
return {
146+
page: pageIndex,
147+
}
148+
},
149+
variables, // GraphQL Query Variables
150+
config // Configuration of useSWRInfinite
151+
)
152+
```

src/visitor.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,19 @@ export class SWRVisitor extends ClientSideBaseVisitor<
172172
if (enabledInfinite) {
173173
codes.push(`use${pascalCase(
174174
o.node.name.value
175-
)}Infinite(getKey: SWRInfiniteKeyLoader<${
176-
o.operationResultType
175+
)}Infinite(getKey: SWRInfiniteKeyLoader<${o.operationResultType}, ${
176+
o.operationVariablesTypes
177177
}>, variables${optionalVariables ? '?' : ''}: ${
178178
o.operationVariablesTypes
179179
}, config?: SWRInfiniteConfigInterface<${o.operationResultType}>) {
180-
return useSWRInfinite<${o.operationResultType}>(getKey, () => sdk.${
180+
return useSWRInfinite<${o.operationResultType}>(
181+
utilsForInfinite.generateGetKey<${o.operationResultType}, ${
182+
o.operationVariablesTypes
183+
}>(getKey),
184+
utilsForInfinite.generateFetcher<${o.operationVariablesTypes}>(sdk.${
181185
o.node.name.value
182-
}(variables), config);
186+
}, variables),
187+
config);
183188
}`)
184189
}
185190

@@ -195,20 +200,30 @@ export class SWRVisitor extends ClientSideBaseVisitor<
195200
)
196201
}
197202
if (this._enabledInfinite) {
198-
types.push(`export type SWRInfiniteKeyLoader<Data = any> = (
203+
types.push(`export type SWRInfiniteKeyLoader<Data = unknown, Variables = unknown> = (
199204
index: number,
200205
previousPageData: Data | null
201-
) => string | any[] | null;`)
206+
) => Variables | null;`)
202207
}
203208

204209
return `${types.join('\n')}
205210
export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
206211
const sdk = getSdk(client, withWrapper);
207212
${
208-
autogenSWRKey
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'
213+
this._enabledInfinite
214+
? ` const utilsForInfinite = {
215+
generateGetKey: <Data = unknown, Variables = unknown>(getKey: SWRInfiniteKeyLoader<Data, Variables>) => (pageIndex: number, previousData: Data | null) => {
216+
const key = getKey(pageIndex, previousData)
217+
return key ? [key] : key
218+
},
219+
generateFetcher: <Variables = unknown>(query: (...params: unknown[]) => unknown, variables?: Variables) => (...params) => query(Object.assign({}, variables, ...params))
220+
}\n`
210221
: ''
211-
} return {
222+
}${
223+
autogenSWRKey
224+
? ' 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'
225+
: ''
226+
} return {
212227
...sdk,
213228
${allPossibleActions.join(',\n')}
214229
};

tests/swr.spec.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,19 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
285285
`import useSWR, { useSWRInfinite, ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface, SWRInfiniteConfigInterface } from 'swr';`
286286
)
287287
expect(output).toContain(
288-
`export type SWRInfiniteKeyLoader<Data = any> = (
288+
`export type SWRInfiniteKeyLoader<Data = unknown, Variables = unknown> = (
289289
index: number,
290290
previousPageData: Data | null
291-
) => string | any[] | null;
291+
) => Variables | null;
292292
export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
293293
const sdk = getSdk(client, withWrapper);
294+
const utilsForInfinite = {
295+
generateGetKey: <Data = unknown, Variables = unknown>(getKey: SWRInfiniteKeyLoader<Data, Variables>) => (pageIndex: number, previousData: Data | null) => {
296+
const key = getKey(pageIndex, previousData)
297+
return key ? [key] : key
298+
},
299+
generateFetcher: <Variables = unknown>(query: (...params: unknown[]) => unknown, variables?: Variables) => (...params) => query(Object.assign({}, variables, ...params))
300+
}
294301
return {
295302
...sdk,
296303
useFeed(key: SWRKeyInterface, variables?: FeedQueryVariables, config?: SWRConfigInterface<FeedQuery>) {
@@ -299,17 +306,23 @@ export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionW
299306
useFeed2(key: SWRKeyInterface, variables: Feed2QueryVariables, config?: SWRConfigInterface<Feed2Query>) {
300307
return useSWR<Feed2Query>(key, () => sdk.feed2(variables), config);
301308
},
302-
useFeed2Infinite(getKey: SWRInfiniteKeyLoader<Feed2Query>, variables: Feed2QueryVariables, config?: SWRInfiniteConfigInterface<Feed2Query>) {
303-
return useSWRInfinite<Feed2Query>(getKey, () => sdk.feed2(variables), config);
309+
useFeed2Infinite(getKey: SWRInfiniteKeyLoader<Feed2Query, Feed2QueryVariables>, variables: Feed2QueryVariables, config?: SWRInfiniteConfigInterface<Feed2Query>) {
310+
return useSWRInfinite<Feed2Query>(
311+
utilsForInfinite.generateGetKey<Feed2Query, Feed2QueryVariables>(getKey),
312+
utilsForInfinite.generateFetcher<Feed2QueryVariables>(sdk.feed2, variables),
313+
config);
304314
},
305315
useFeed3(key: SWRKeyInterface, variables?: Feed3QueryVariables, config?: SWRConfigInterface<Feed3Query>) {
306316
return useSWR<Feed3Query>(key, () => sdk.feed3(variables), config);
307317
},
308318
useFeed4(key: SWRKeyInterface, variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query>) {
309319
return useSWR<Feed4Query>(key, () => sdk.feed4(variables), config);
310320
},
311-
useFeed4Infinite(getKey: SWRInfiniteKeyLoader<Feed4Query>, variables?: Feed4QueryVariables, config?: SWRInfiniteConfigInterface<Feed4Query>) {
312-
return useSWRInfinite<Feed4Query>(getKey, () => sdk.feed4(variables), config);
321+
useFeed4Infinite(getKey: SWRInfiniteKeyLoader<Feed4Query, Feed4QueryVariables>, variables?: Feed4QueryVariables, config?: SWRInfiniteConfigInterface<Feed4Query>) {
322+
return useSWRInfinite<Feed4Query>(
323+
utilsForInfinite.generateGetKey<Feed4Query, Feed4QueryVariables>(getKey),
324+
utilsForInfinite.generateFetcher<Feed4QueryVariables>(sdk.feed4, variables),
325+
config);
313326
}
314327
};
315328
}

0 commit comments

Comments
 (0)