From b2cd7643bb074ca6b4486b67601df5858ca97813 Mon Sep 17 00:00:00 2001 From: croutonn Date: Mon, 19 Oct 2020 21:42:29 +0900 Subject: [PATCH] Add an option to exclude queries with the glob patterns --- package.json | 2 ++ src/config.ts | 1 + src/visitor.ts | 18 ++++++++++++++++-- tests/swr.spec.ts | 28 ++++++++++++++++++++++++++++ yarn.lock | 12 ++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index cd890c4..d28cd3f 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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", diff --git a/src/config.ts b/src/config.ts index 9ba0907..92929e2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -23,4 +23,5 @@ export interface RawSWRPluginConfig extends RawClientSideBasePluginConfig { * ``` */ rawRequest?: boolean + exclude?: string | string[] } diff --git a/src/visitor.ts b/src/visitor.ts index 607feaf..1c083be 100644 --- a/src/visitor.ts +++ b/src/visitor.ts @@ -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< @@ -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) @@ -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 || diff --git a/tests/swr.spec.ts b/tests/swr.spec.ts index 4764e6f..36bbd82 100644 --- a/tests/swr.spec.ts +++ b/tests/swr.spec.ts @@ -235,6 +235,34 @@ export type SdkWithHooks = ReturnType;`) } }; } +export type SdkWithHooks = ReturnType;`) + }) + + 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;export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { + const sdk = getSdk(client, withWrapper); + return { + ...sdk, + useFeed(key: SWRKeyInterface, variables?: FeedQueryVariables, config?: SWRConfigInterface) { + return useSWR(key, () => sdk.feed(variables), config); + }, + useFeed4(key: SWRKeyInterface, variables?: Feed4QueryVariables, config?: SWRConfigInterface) { + return useSWR(key, () => sdk.feed4(variables), config); + } + }; +} export type SdkWithHooks = ReturnType;`) }) }) diff --git a/yarn.lock b/yarn.lock index 8b885f9..3be7c91 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"