Skip to content

Instantly share code, notes, and snippets.

@intrnl
Created January 21, 2023 11:42
Show Gist options
  • Save intrnl/f328e9b4fe31388662077d8e71ba4fb8 to your computer and use it in GitHub Desktop.
Save intrnl/f328e9b4fe31388662077d8e71ba4fb8 to your computer and use it in GitHub Desktop.
/* eslint-env node */
import ts from 'typescript'
import {TypeGenAddonFactory} from 'ts-graphql-plugin'
import {astf} from 'ts-graphql-plugin/lib/ts-ast-util'
const typedQueryDocumentAddon: TypeGenAddonFactory = ({source}) => ({
operationDefinition({tsResultNode, tsVariableNode}) {
const lhs = astf.createIdentifier(`${tsResultNode.name.text}Document`)
const rhs = astf.createTypeReferenceNode(
astf.createIdentifier('TypedDocumentNode'),
[
astf.createTypeReferenceNode(
astf.createIdentifier(tsResultNode.name.text)
),
astf.createTypeReferenceNode(
astf.createIdentifier(tsVariableNode.name.text)
),
]
)
const modifiers = [astf.createModifier(ts.SyntaxKind.ExportKeyword)]
const typeAliasDeclaration = astf.createTypeAliasDeclaration(
modifiers,
lhs,
undefined,
rhs
)
source.pushNamedImportIfNeeded(
'TypedDocumentNode',
'@graphql-typed-document-node/core'
)
source.pushStatement(typeAliasDeclaration)
},
})
export default typedQueryDocumentAddon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment