Created
January 21, 2023 11:42
-
-
Save intrnl/f328e9b4fe31388662077d8e71ba4fb8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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