import { Types , PluginValidateFn , PluginFunction , oldVisit } from '@graphql-codegen/plugin-helpers' ; import { GraphQLSchema , concatAST , Kind , FragmentDefinitionNode } from 'graphql' ; import { RawClientSideBasePluginConfig , LoadedFragment } from '@graphql-codegen/visitor-plugin-common' ; import { GraphQLRequestVisitor } from './visitor' ; import { extname } from 'path' ; import { RawGraphQLRequestPluginConfig } from './config' ; 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 GraphQLRequestVisitor ( schema , allFragments , config ) ; const visitorResult = oldVisit ( allAst , { leave : visitor } ) ; return { prepend : visitor . getImports ( ) , content : [ visitor . fragments , ... visitorResult . definitions . filter ( t => typeof t === 'string' ) , visitor . sdkContent , ] . join ( '\n' ) , } ; } ; export const validate = async ( schema , documents , config , outputFile ) => { if ( extname ( outputFile ) !== '.ts' ) { throw new Error ( `Plugin "typescript-graphql-request" requires extension to be ".ts"!` ) ; } } ; export { GraphQLRequestVisitor } ;