import { oldVisit , PluginFunction , PluginValidateFn , Types } from '@graphql-codegen/plugin-helpers' ; import { NamingConvention , LoadedFragment , RawClientSideBasePluginConfig , } from '@graphql-codegen/visitor-plugin-common' ; import { GraphQLSchema , concatAST , FragmentDefinitionNode , Kind } from 'graphql' ; import { TypeScriptDocumentNodesVisitor } from './visitor' ; description export interface TypeScriptDocumentNodesRawPluginConfig extends RawClientSideBasePluginConfig { default description exampleMarkdown namingConvention ? : NamingConvention ; default description exampleMarkdown namePrefix ? : string ; default description exampleMarkdown nameSuffix ? : string ; default description fragmentPrefix ? : string ; default description fragmentSuffix ? : string ; } export const plugin = ( schema , documents , config ) => { const allAst = concatAST ( documents . map ( v => v . document ) ) ; const allFragments = [ ... ( allAst . definitions . filter ( d => d . kind === Kind . FRAGMENT_DEFINITION ) as FragmentDefinitionNode [ ] ) . map ( fragmentDef => ( { node : fragmentDef , name : fragmentDef . name . value , onType : fragmentDef . typeCondition . name . value , isExternal : false , } ) ) , ... ( config . externalFragments || [ ] ) , ] ; const visitor = new TypeScriptDocumentNodesVisitor ( schema , allFragments , config , documents ) ; const visitorResult = oldVisit ( allAst , { leave : visitor } ) ; return { prepend : visitor . getImports ( ) , content : [ visitor . fragments , ... visitorResult . definitions . filter ( t => typeof t === 'string' ) ] . join ( '\n' ) , } ; } ; export const validate = async ( schema , documents , config , outputFile ) => { if ( ! outputFile . endsWith ( '.ts' ) ) { throw new Error ( `Plugin "typescript-document-nodes" requires extension to be ".ts"!` ) ; } } ;