1
+ import fs from 'fs'
2
+ import { resolve } from 'path'
3
+
1
4
import { Types , mergeOutputs } from '@graphql-codegen/plugin-helpers'
2
5
import { validateTs } from '@graphql-codegen/testing'
3
6
import {
@@ -22,6 +25,10 @@ type PluginsConfig = Partial<
22
25
RawSWRPluginConfig
23
26
>
24
27
28
+ const readOutput = ( name : string ) : string => {
29
+ return fs . readFileSync ( resolve ( __dirname , `./outputs/${ name } .ts` ) , 'utf-8' )
30
+ }
31
+
25
32
describe ( 'SWR' , ( ) => {
26
33
const schema = buildClientSchema ( require ( '../dev-test/githunt/schema.json' ) )
27
34
@@ -136,27 +143,7 @@ async function test() {
136
143
137
144
const usage = basicUsage
138
145
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' ) )
160
147
} )
161
148
162
149
it ( 'Should generate the output from which mutation operation has been removed' , async ( ) => {
@@ -217,27 +204,7 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
217
204
218
205
const usage = basicUsage
219
206
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' ) )
241
208
} )
242
209
243
210
it ( 'Should work `excludeQueries` option correctly' , async ( ) => {
@@ -252,21 +219,7 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
252
219
253
220
const usage = basicUsage
254
221
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' ) )
270
223
} )
271
224
272
225
it ( 'Should work `useSWRInfinite` option correctly' , async ( ) => {
@@ -284,63 +237,14 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
284
237
expect ( content . prepend ) . toContain (
285
238
`import useSWR, { useSWRInfinite, ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface, SWRInfiniteConfigInterface } from 'swr';`
286
239
)
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' ) )
338
241
} )
339
242
} )
340
243
341
244
it ( 'Should work `autogenSWRKey` option correctly' , async ( ) => {
342
245
const config : PluginsConfig = {
343
246
autogenSWRKey : true ,
247
+ useSWRInfinite : [ 'feed[24]' ] ,
344
248
}
345
249
const docs = [ { location : '' , document : basicDoc } ]
346
250
@@ -350,27 +254,6 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`
350
254
351
255
const usage = basicUsage
352
256
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' ) )
375
258
} )
376
259
} )
0 commit comments