Skip to content

Commit b2cd764

Browse files
committed
Add an option to exclude queries with the glob patterns
1 parent 248efa8 commit b2cd764

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"auto-bind": "^4.0.0",
3636
"graphql": "^15.3.0",
3737
"graphql-request": "^3.1.0",
38+
"micromatch": "^4.0.2",
3839
"pascal-case": "^3.1.1",
3940
"swr": "^0.3.5",
4041
"tslib": "^2.0.3"
@@ -49,6 +50,7 @@
4950
"@graphql-codegen/typescript-graphql-request": "^2.0.1",
5051
"@graphql-codegen/typescript-operations": "^1.17.8",
5152
"@types/jest": "^26.0.14",
53+
"@types/micromatch": "^4.0.1",
5254
"@types/node": "^14.11.8",
5355
"@typescript-eslint/eslint-plugin": "^4.4.1",
5456
"eslint": "^7.11.0",

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ export interface RawSWRPluginConfig extends RawClientSideBasePluginConfig {
2323
* ```
2424
*/
2525
rawRequest?: boolean
26+
exclude?: string | string[]
2627
}

src/visitor.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import {
66
} from '@graphql-codegen/visitor-plugin-common'
77
import autoBind from 'auto-bind'
88
import { GraphQLSchema, Kind, OperationDefinitionNode } from 'graphql'
9+
import glob from 'micromatch'
910
import { pascalCase } from 'pascal-case'
1011

1112
import { RawSWRPluginConfig } from './config'
1213

1314
export interface SWRPluginConfig extends ClientSideBasePluginConfig {
1415
rawRequest: boolean
16+
exclude: string | string[]
1517
}
1618

1719
export class SWRVisitor extends ClientSideBaseVisitor<
@@ -31,7 +33,9 @@ export class SWRVisitor extends ClientSideBaseVisitor<
3133
fragments: LoadedFragment[],
3234
rawConfig: RawSWRPluginConfig
3335
) {
34-
super(schema, fragments, rawConfig, {})
36+
super(schema, fragments, rawConfig, {
37+
exclude: rawConfig.exclude || null,
38+
})
3539

3640
autoBind(this)
3741

@@ -67,7 +71,17 @@ export class SWRVisitor extends ClientSideBaseVisitor<
6771

6872
public get sdkContent(): string {
6973
const allPossibleActions = this._operationsToInclude
70-
.filter((o) => o.operationType === 'Query')
74+
.filter((o) => {
75+
if (o.operationType !== 'Query') {
76+
return false
77+
}
78+
const { exclude } = this.config
79+
if (!exclude || (Array.isArray(exclude) && !exclude.length)) {
80+
return true
81+
}
82+
const name = o.node.name.value
83+
return !glob.isMatch(name, exclude)
84+
})
7185
.map((o) => {
7286
const optionalVariables =
7387
!o.node.variableDefinitions ||

tests/swr.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,34 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`)
235235
}
236236
};
237237
}
238+
export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`)
239+
})
240+
241+
it('Should work `exclude` option correctly', async () => {
242+
const config: PluginsConfig = {
243+
exclude: ['feed[2-3]', 'hoge', 'foo'],
244+
}
245+
const docs = [{ location: '', document: basicDoc }]
246+
247+
const content = (await plugin(schema, docs, config, {
248+
outputFile: 'graphql.ts',
249+
})) as Types.ComplexPluginOutput
250+
251+
const usage = basicUsage
252+
const output = await validate(content, config, docs, schema, usage)
253+
expect(output)
254+
.toContain(`export type Sdk = ReturnType<typeof getSdk>;export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
255+
const sdk = getSdk(client, withWrapper);
256+
return {
257+
...sdk,
258+
useFeed(key: SWRKeyInterface, variables?: FeedQueryVariables, config?: SWRConfigInterface<FeedQuery>) {
259+
return useSWR<FeedQuery>(key, () => sdk.feed(variables), config);
260+
},
261+
useFeed4(key: SWRKeyInterface, variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query>) {
262+
return useSWR<Feed4Query>(key, () => sdk.feed4(variables), config);
263+
}
264+
};
265+
}
238266
export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`)
239267
})
240268
})

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,11 @@
10471047
dependencies:
10481048
"@babel/types" "^7.3.0"
10491049

1050+
"@types/braces@*":
1051+
version "3.0.0"
1052+
resolved "https://registry.yarnpkg.com/@types/braces/-/braces-3.0.0.tgz#7da1c0d44ff1c7eb660a36ec078ea61ba7eb42cb"
1053+
integrity sha512-TbH79tcyi9FHwbyboOKeRachRq63mSuWYXOflsNO9ZyE5ClQ/JaozNKl+aWUq87qPNsXasXxi2AbgfwIJ+8GQw==
1054+
10501055
"@types/graceful-fs@^4.1.2":
10511056
version "4.1.3"
10521057
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.3.tgz#039af35fe26bec35003e8d86d2ee9c586354348f"
@@ -1099,6 +1104,13 @@
10991104
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
11001105
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=
11011106

1107+
"@types/micromatch@^4.0.1":
1108+
version "4.0.1"
1109+
resolved "https://registry.yarnpkg.com/@types/micromatch/-/micromatch-4.0.1.tgz#9381449dd659fc3823fd2a4190ceacc985083bc7"
1110+
integrity sha512-my6fLBvpY70KattTNzYOK6KU1oR1+UCz9ug/JbcF5UrEmeCt9P7DV2t7L8+t18mMPINqGQCE4O8PLOPbI84gxw==
1111+
dependencies:
1112+
"@types/braces" "*"
1113+
11021114
"@types/node@*", "@types/node@^14.11.8":
11031115
version "14.11.8"
11041116
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.11.8.tgz#fe2012f2355e4ce08bca44aeb3abbb21cf88d33f"

0 commit comments

Comments
 (0)