From df3235b8122e4c0e1ddfa682e09e25827d6a6bb2 Mon Sep 17 00:00:00 2001 From: Ezeki Zibzibadze Date: Fri, 10 Jan 2020 09:37:14 +0400 Subject: [PATCH] feat: generate all query object types enum --- package.json | 2 +- .../graphql-client/generator/generateProxy.ts | 12 ++++++++--- .../generator/render/renderProxy.ts | 4 ++++ .../generator/render/renderQueryTypesEnum.ts | 21 +++++++++++++++++++ tslint.json | 1 - yarn.lock | 2 +- 6 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 src/commands/graphql-client/generator/render/renderQueryTypesEnum.ts diff --git a/package.json b/package.json index 9131a6d..c7a608a 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "@types/commander": "2.12.2", "@types/execa": "^0.9.0", "@types/fs-extra": "^5.0.4", - "@types/graphql": "^14.0.3", + "@types/graphql": "^14.5.0", "@types/jest": "24.0.10", "@types/mkdirp": "^0.5.2", "@types/ms": "^0.7.30", diff --git a/src/commands/graphql-client/generator/generateProxy.ts b/src/commands/graphql-client/generator/generateProxy.ts index fd6a685..6fc1f7c 100644 --- a/src/commands/graphql-client/generator/generateProxy.ts +++ b/src/commands/graphql-client/generator/generateProxy.ts @@ -3,6 +3,7 @@ import { RootType } from '../utils/rootType' import generateRootType from './generateRootType' import generateType from './helper/generateType' import renderProxy from './render/renderProxy' +import renderQueryTypesEnum from './render/renderQueryTypesEnum' export default function ( introspectionSchema: IntrospectionSchema, @@ -21,9 +22,9 @@ export default function ( ? introspectionSchema.subscriptionType.name : null - const queryType = types.find(x => x.name === queryTypeName) - const mutationType = types.find(x => x.name === mutationTypeName) - const subscriptionType = types.find(x => x.name === subscriptionTypeName) + const queryType = types.find(x => x.name === queryTypeName) + const mutationType = types.find(x => x.name === mutationTypeName) + const subscriptionType = types.find(x => x.name === subscriptionTypeName) const otherTypes = types.filter(x => (x !== queryType) && @@ -31,6 +32,8 @@ export default function ( (x !== subscriptionType), ) + const objectTypes = otherTypes.filter(x => x.kind === 'OBJECT') + // start generation const generatedQuery = generateRootType(RootType.Query, otherTypes, generateDefaultFragments)(queryType) const generatedWatchQuery = generateRootType('watchQuery', otherTypes, generateDefaultFragments)(queryType) @@ -45,6 +48,8 @@ export default function ( .filter(x => !!x) .join('\n') + const generatedQueryTypesEnum = renderQueryTypesEnum(objectTypes) + return renderProxy({ generatedQuery, generatedWatchQuery, @@ -52,6 +57,7 @@ export default function ( generatedMutation, generatedSubscription, generatedOtherTypes, + generatedQueryTypesEnum, }) } diff --git a/src/commands/graphql-client/generator/render/renderProxy.ts b/src/commands/graphql-client/generator/render/renderProxy.ts index 26a672b..ac3c41d 100644 --- a/src/commands/graphql-client/generator/render/renderProxy.ts +++ b/src/commands/graphql-client/generator/render/renderProxy.ts @@ -7,11 +7,15 @@ export default function ({ generatedRefetchQuery, generatedMutation, generatedSubscription, + generatedQueryTypesEnum, }) { return `${renderImports()} // tslint:disable +// types enum +${generatedQueryTypesEnum || ''} + // types ${generatedOtherTypes} diff --git a/src/commands/graphql-client/generator/render/renderQueryTypesEnum.ts b/src/commands/graphql-client/generator/render/renderQueryTypesEnum.ts new file mode 100644 index 0000000..3695723 --- /dev/null +++ b/src/commands/graphql-client/generator/render/renderQueryTypesEnum.ts @@ -0,0 +1,21 @@ +import { IntrospectionType } from 'graphql' +import capitalizeFirstLetter from '../../utils/capitalizeFirstLetter' + +export default function (types: IntrospectionType[]) { + + if (!types.length) { + return null + } + + const renderedFields = types + .filter(x => !x.name.startsWith('__')) + .map(x => capitalizeFirstLetter(x.name)) + .map(x => ` ${x} = '${x}',`) + .join('\n') + + return ` +export enum QueryObjectTypes { +${renderedFields} +} + ` +} diff --git a/tslint.json b/tslint.json index 3919468..3d57f9b 100644 --- a/tslint.json +++ b/tslint.json @@ -132,7 +132,6 @@ "check-separator", "check-rest-spread", "check-type", - "check-typecast", "check-type-operator", "check-preblock" ], diff --git a/yarn.lock b/yarn.lock index d268889..8c81be7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -352,7 +352,7 @@ dependencies: "@types/node" "*" -"@types/graphql@^14.0.3": +"@types/graphql@^14.5.0": version "14.5.0" resolved "https://registry.yarnpkg.com/@types/graphql/-/graphql-14.5.0.tgz#a545fb3bc8013a3547cf2f07f5e13a33642b75d6" integrity sha512-MOkzsEp1Jk5bXuAsHsUi6BVv0zCO+7/2PTiZMXWDSsMXvNU6w/PLMQT2vHn8hy2i0JqojPz1Sz6rsFjHtsU0lA==