1+ import fs from 'fs'
2+ import { resolve } from 'path'
3+
14import { Types , mergeOutputs } from '@graphql-codegen/plugin-helpers'
25import { validateTs } from '@graphql-codegen/testing'
36import {
@@ -22,6 +25,10 @@ type PluginsConfig = Partial<
2225 RawSWRPluginConfig
2326>
2427
28+ const readOutput = ( name : string ) : string => {
29+ return fs . readFileSync ( resolve ( __dirname , `./outputs/${ name } .ts` ) , 'utf-8' )
30+ }
31+
2532describe ( 'SWR' , ( ) => {
2633 const schema = buildClientSchema ( require ( '../dev-test/githunt/schema.json' ) )
2734
@@ -136,27 +143,7 @@ async function test() {
136143
137144 const usage = basicUsage
138145 const output = await validate ( content , config , docs , schema , usage )
139- expect ( output ) . toContain (
140- `export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
141- const sdk = getSdk(client, withWrapper);
142- return {
143- ...sdk,
144- useFeed(key: SWRKeyInterface, variables?: FeedQueryVariables, config?: SWRConfigInterface<FeedQuery>) {
145- return useSWR<FeedQuery>(key, () => sdk.feed(variables), config);
146- },
147- useFeed2(key: SWRKeyInterface, variables: Feed2QueryVariables, config?: SWRConfigInterface<Feed2Query>) {
148- return useSWR<Feed2Query>(key, () => sdk.feed2(variables), config);
149- },
150- useFeed3(key: SWRKeyInterface, variables?: Feed3QueryVariables, config?: SWRConfigInterface<Feed3Query>) {
151- return useSWR<Feed3Query>(key, () => sdk.feed3(variables), config);
152- },
153- useFeed4(key: SWRKeyInterface, variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query>) {
154- return useSWR<Feed4Query>(key, () => sdk.feed4(variables), config);
155- }
156- };
157- }
158- export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
159- )
146+ expect ( output ) . toContain ( readOutput ( 'straight' ) )
160147 } )
161148
162149 it ( 'Should generate the output from which mutation operation has been removed' , async ( ) => {
@@ -217,27 +204,7 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
217204
218205 const usage = basicUsage
219206 const output = await validate ( content , config , docs , schema , usage )
220- expect ( output ) . toContain (
221- `export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
222- const sdk = getSdk(client, withWrapper);
223- return {
224- ...sdk,
225- useFeed(key: SWRKeyInterface, variables?: FeedQueryVariables, config?: SWRConfigInterface<FeedQuery>) {
226- return useSWR<FeedQuery>(key, () => sdk.feed(variables), config);
227- },
228- useFeed2(key: SWRKeyInterface, variables: Feed2QueryVariables, config?: SWRConfigInterface<Feed2Query>) {
229- return useSWR<Feed2Query>(key, () => sdk.feed2(variables), config);
230- },
231- useFeed3(key: SWRKeyInterface, variables?: Feed3QueryVariables, config?: SWRConfigInterface<Feed3Query>) {
232- return useSWR<Feed3Query>(key, () => sdk.feed3(variables), config);
233- },
234- useFeed4(key: SWRKeyInterface, variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query>) {
235- return useSWR<Feed4Query>(key, () => sdk.feed4(variables), config);
236- }
237- };
238- }
239- export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
240- )
207+ expect ( output ) . toContain ( readOutput ( 'mutations' ) )
241208 } )
242209
243210 it ( 'Should work `excludeQueries` option correctly' , async ( ) => {
@@ -252,21 +219,7 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
252219
253220 const usage = basicUsage
254221 const output = await validate ( content , config , docs , schema , usage )
255- expect ( output ) . toContain (
256- `export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
257- const sdk = getSdk(client, withWrapper);
258- return {
259- ...sdk,
260- useFeed(key: SWRKeyInterface, variables?: FeedQueryVariables, config?: SWRConfigInterface<FeedQuery>) {
261- return useSWR<FeedQuery>(key, () => sdk.feed(variables), config);
262- },
263- useFeed4(key: SWRKeyInterface, variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query>) {
264- return useSWR<Feed4Query>(key, () => sdk.feed4(variables), config);
265- }
266- };
267- }
268- export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
269- )
222+ expect ( output ) . toContain ( readOutput ( 'excludeQueries' ) )
270223 } )
271224
272225 it ( 'Should work `useSWRInfinite` option correctly' , async ( ) => {
@@ -284,63 +237,14 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
284237 expect ( content . prepend ) . toContain (
285238 `import useSWR, { useSWRInfinite, ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface, SWRInfiniteConfigInterface } from 'swr';`
286239 )
287- expect ( output ) . toContain (
288- `export type SWRInfiniteKeyLoader<Data = unknown, Variables = unknown> = (
289- index: number,
290- previousPageData: Data | null
291- ) => [keyof Variables, Variables[keyof Variables] | null] | null;
292- export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
293- const sdk = getSdk(client, withWrapper);
294- const utilsForInfinite = {
295- generateGetKey: <Data = unknown, Variables = unknown>(
296- id: string,
297- getKey: SWRInfiniteKeyLoader<Data, Variables>
298- ) => (pageIndex: number, previousData: Data | null) => {
299- const key = getKey(pageIndex, previousData)
300- return key ? [id, ...key] : null
301- },
302- generateFetcher: <Query = unknown, Variables = unknown>(query: (variables: Variables) => Promise<Query>, variables?: Variables) => (
303- id: string,
304- fieldName: keyof Variables,
305- fieldValue: Variables[typeof fieldName]
306- ) => query({ ...variables, [fieldName]: fieldValue } as Variables)
307- }
308- return {
309- ...sdk,
310- useFeed(key: SWRKeyInterface, variables?: FeedQueryVariables, config?: SWRConfigInterface<FeedQuery>) {
311- return useSWR<FeedQuery>(key, () => sdk.feed(variables), config);
312- },
313- useFeed2(key: SWRKeyInterface, variables: Feed2QueryVariables, config?: SWRConfigInterface<Feed2Query>) {
314- return useSWR<Feed2Query>(key, () => sdk.feed2(variables), config);
315- },
316- useFeed2Infinite(id: string, getKey: SWRInfiniteKeyLoader<Feed2Query, Feed2QueryVariables>, variables: Feed2QueryVariables, config?: SWRInfiniteConfigInterface<Feed2Query>) {
317- return useSWRInfinite<Feed2Query>(
318- utilsForInfinite.generateGetKey<Feed2Query, Feed2QueryVariables>(id, getKey),
319- utilsForInfinite.generateFetcher<Feed2Query, Feed2QueryVariables>(sdk.feed2, variables),
320- config);
321- },
322- useFeed3(key: SWRKeyInterface, variables?: Feed3QueryVariables, config?: SWRConfigInterface<Feed3Query>) {
323- return useSWR<Feed3Query>(key, () => sdk.feed3(variables), config);
324- },
325- useFeed4(key: SWRKeyInterface, variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query>) {
326- return useSWR<Feed4Query>(key, () => sdk.feed4(variables), config);
327- },
328- useFeed4Infinite(id: string, getKey: SWRInfiniteKeyLoader<Feed4Query, Feed4QueryVariables>, variables?: Feed4QueryVariables, config?: SWRInfiniteConfigInterface<Feed4Query>) {
329- return useSWRInfinite<Feed4Query>(
330- utilsForInfinite.generateGetKey<Feed4Query, Feed4QueryVariables>(id, getKey),
331- utilsForInfinite.generateFetcher<Feed4Query, Feed4QueryVariables>(sdk.feed4, variables),
332- config);
333- }
334- };
335- }
336- export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
337- )
240+ expect ( output ) . toContain ( readOutput ( 'infinite' ) )
338241 } )
339242 } )
340243
341244 it ( 'Should work `autogenSWRKey` option correctly' , async ( ) => {
342245 const config : PluginsConfig = {
343246 autogenSWRKey : true ,
247+ useSWRInfinite : [ 'feed[24]' ] ,
344248 }
345249 const docs = [ { location : '' , document : basicDoc } ]
346250
@@ -350,27 +254,6 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
350254
351255 const usage = basicUsage
352256 const output = await validate ( content , config , docs , schema , usage )
353- expect ( output ) . toContain (
354- `export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
355- const sdk = getSdk(client, withWrapper);
356- 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])];
357- return {
358- ...sdk,
359- useFeed(variables?: FeedQueryVariables, config?: SWRConfigInterface<FeedQuery>) {
360- return useSWR<FeedQuery>(genKey<FeedQueryVariables>('Feed', variables), () => sdk.feed(variables), config);
361- },
362- useFeed2(variables: Feed2QueryVariables, config?: SWRConfigInterface<Feed2Query>) {
363- return useSWR<Feed2Query>(genKey<Feed2QueryVariables>('Feed2', variables), () => sdk.feed2(variables), config);
364- },
365- useFeed3(variables?: Feed3QueryVariables, config?: SWRConfigInterface<Feed3Query>) {
366- return useSWR<Feed3Query>(genKey<Feed3QueryVariables>('Feed3', variables), () => sdk.feed3(variables), config);
367- },
368- useFeed4(variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query>) {
369- return useSWR<Feed4Query>(genKey<Feed4QueryVariables>('Feed4', variables), () => sdk.feed4(variables), config);
370- }
371- };
372- }
373- export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
374- )
257+ expect ( output ) . toContain ( readOutput ( 'autogenSWRKey' ) )
375258 } )
376259} )
0 commit comments