Skip to content

Commit

Permalink
feat: generate all query object types enum
Browse files Browse the repository at this point in the history
  • Loading branch information
playerx committed Jan 10, 2020
1 parent 3e45a61 commit df3235b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 9 additions & 3 deletions src/commands/graphql-client/generator/generateProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,16 +22,18 @@ export default function (
? introspectionSchema.subscriptionType.name
: null

const queryType = <IntrospectionType> types.find(x => x.name === queryTypeName)
const mutationType = <IntrospectionType> types.find(x => x.name === mutationTypeName)
const subscriptionType = <IntrospectionType> types.find(x => x.name === subscriptionTypeName)
const queryType = <IntrospectionType>types.find(x => x.name === queryTypeName)
const mutationType = <IntrospectionType>types.find(x => x.name === mutationTypeName)
const subscriptionType = <IntrospectionType>types.find(x => x.name === subscriptionTypeName)

const otherTypes = types.filter(x =>
(x !== queryType) &&
(x !== mutationType) &&
(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)
Expand All @@ -45,13 +48,16 @@ export default function (
.filter(x => !!x)
.join('\n')

const generatedQueryTypesEnum = renderQueryTypesEnum(objectTypes)

return renderProxy({
generatedQuery,
generatedWatchQuery,
generatedRefetchQuery,
generatedMutation,
generatedSubscription,
generatedOtherTypes,
generatedQueryTypesEnum,
})
}

Expand Down
4 changes: 4 additions & 0 deletions src/commands/graphql-client/generator/render/renderProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ export default function ({
generatedRefetchQuery,
generatedMutation,
generatedSubscription,
generatedQueryTypesEnum,
}) {
return `${renderImports()}
// tslint:disable
// types enum
${generatedQueryTypesEnum || ''}
// types
${generatedOtherTypes}
Expand Down
Original file line number Diff line number Diff line change
@@ -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}
}
`
}
1 change: 0 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
"check-separator",
"check-rest-spread",
"check-type",
"check-typecast",
"check-type-operator",
"check-preblock"
],
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit df3235b

Please sign in to comment.