Skip to content

Commit

Permalink
Add an option to exclude queries with the glob patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
croutonn committed Oct 19, 2020
1 parent 248efa8 commit b2cd764
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"auto-bind": "^4.0.0",
"graphql": "^15.3.0",
"graphql-request": "^3.1.0",
"micromatch": "^4.0.2",
"pascal-case": "^3.1.1",
"swr": "^0.3.5",
"tslib": "^2.0.3"
Expand All @@ -49,6 +50,7 @@
"@graphql-codegen/typescript-graphql-request": "^2.0.1",
"@graphql-codegen/typescript-operations": "^1.17.8",
"@types/jest": "^26.0.14",
"@types/micromatch": "^4.0.1",
"@types/node": "^14.11.8",
"@typescript-eslint/eslint-plugin": "^4.4.1",
"eslint": "^7.11.0",
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export interface RawSWRPluginConfig extends RawClientSideBasePluginConfig {
* ```
*/
rawRequest?: boolean
exclude?: string | string[]
}
18 changes: 16 additions & 2 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
} from '@graphql-codegen/visitor-plugin-common'
import autoBind from 'auto-bind'
import { GraphQLSchema, Kind, OperationDefinitionNode } from 'graphql'
import glob from 'micromatch'
import { pascalCase } from 'pascal-case'

import { RawSWRPluginConfig } from './config'

export interface SWRPluginConfig extends ClientSideBasePluginConfig {
rawRequest: boolean
exclude: string | string[]
}

export class SWRVisitor extends ClientSideBaseVisitor<
Expand All @@ -31,7 +33,9 @@ export class SWRVisitor extends ClientSideBaseVisitor<
fragments: LoadedFragment[],
rawConfig: RawSWRPluginConfig
) {
super(schema, fragments, rawConfig, {})
super(schema, fragments, rawConfig, {
exclude: rawConfig.exclude || null,
})

autoBind(this)

Expand Down Expand Up @@ -67,7 +71,17 @@ export class SWRVisitor extends ClientSideBaseVisitor<

public get sdkContent(): string {
const allPossibleActions = this._operationsToInclude
.filter((o) => o.operationType === 'Query')
.filter((o) => {
if (o.operationType !== 'Query') {
return false
}
const { exclude } = this.config
if (!exclude || (Array.isArray(exclude) && !exclude.length)) {
return true
}
const name = o.node.name.value
return !glob.isMatch(name, exclude)
})
.map((o) => {
const optionalVariables =
!o.node.variableDefinitions ||
Expand Down
28 changes: 28 additions & 0 deletions tests/swr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,34 @@ export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`)
}
};
}
export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`)
})

it('Should work `exclude` option correctly', async () => {
const config: PluginsConfig = {
exclude: ['feed[2-3]', 'hoge', 'foo'],
}
const docs = [{ location: '', document: basicDoc }]

const content = (await plugin(schema, docs, config, {
outputFile: 'graphql.ts',
})) as Types.ComplexPluginOutput

const usage = basicUsage
const output = await validate(content, config, docs, schema, usage)
expect(output)
.toContain(`export type Sdk = ReturnType<typeof getSdk>;export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
const sdk = getSdk(client, withWrapper);
return {
...sdk,
useFeed(key: SWRKeyInterface, variables?: FeedQueryVariables, config?: SWRConfigInterface<FeedQuery>) {
return useSWR<FeedQuery>(key, () => sdk.feed(variables), config);
},
useFeed4(key: SWRKeyInterface, variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query>) {
return useSWR<Feed4Query>(key, () => sdk.feed4(variables), config);
}
};
}
export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;`)
})
})
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,11 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/braces@*":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/braces/-/braces-3.0.0.tgz#7da1c0d44ff1c7eb660a36ec078ea61ba7eb42cb"
integrity sha512-TbH79tcyi9FHwbyboOKeRachRq63mSuWYXOflsNO9ZyE5ClQ/JaozNKl+aWUq87qPNsXasXxi2AbgfwIJ+8GQw==

"@types/graceful-fs@^4.1.2":
version "4.1.3"
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.3.tgz#039af35fe26bec35003e8d86d2ee9c586354348f"
Expand Down Expand Up @@ -1099,6 +1104,13 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/micromatch@^4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@types/micromatch/-/micromatch-4.0.1.tgz#9381449dd659fc3823fd2a4190ceacc985083bc7"
integrity sha512-my6fLBvpY70KattTNzYOK6KU1oR1+UCz9ug/JbcF5UrEmeCt9P7DV2t7L8+t18mMPINqGQCE4O8PLOPbI84gxw==
dependencies:
"@types/braces" "*"

"@types/node@*", "@types/node@^14.11.8":
version "14.11.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.11.8.tgz#fe2012f2355e4ce08bca44aeb3abbb21cf88d33f"
Expand Down

0 comments on commit b2cd764

Please sign in to comment.