import { GraphQLSchema , parse , extendSchema , printIntrospectionSchema , printSchema , visit , buildASTSchema , print , } from 'graphql' ; import { PluginFunction , PluginValidateFn , Types , removeFederation , getCachedDocumentNodeFromSchema , } from '@graphql-codegen/plugin-helpers' ; import { extname } from 'path' ; description export interface SchemaASTConfig { description default exampleMarkdown includeDirectives ? : boolean ; description default exampleMarkdown includeIntrospectionTypes ? : boolean ; description default exampleMarkdown commentDescriptions ? : boolean ; description default sort ? : boolean ; federation ? : boolean ; } export const plugin = async ( schema , _documents , { commentDescriptions = false , includeDirectives = false , includeIntrospectionTypes = false , sort = false , federation , } ) : Promise < string > => { const transformedSchemaAndAst = transformSchemaAST ( schema , { sort , federation , includeIntrospectionTypes } ) ; return [ includeIntrospectionTypes ? printIntrospectionSchema ( transformedSchemaAndAst . schema ) : null , includeDirectives ? print ( transformedSchemaAndAst . ast ) : ( printSchema as any ) ( transformedSchemaAndAst . schema , { commentDescriptions } ) , ] . filter ( Boolean ) . join ( '\n' ) ; } ; export const validate = async ( _schema , _documents , _config , outputFile , allPlugins ) => { const singlePlugin = allPlugins . length === 1 ; if ( singlePlugin && extname ( outputFile ) !== '.graphql' ) { throw new Error ( `Plugin "schema-ast" requires extension to be ".graphql"!` ) ; } } ; export function transformSchemaAST ( schema , config ) { schema = config . federation ? removeFederation ( schema ) : schema ; if ( config . includeIntrospectionTypes ) { const introspectionAST = parse ( `
      extend type Query {
        __schema: __Schema!
        __type(name: String!): __Type
      }
    ` ) ; schema = extendSchema ( schema , introspectionAST ) ; } let ast = getCachedDocumentNodeFromSchema ( schema ) ; ast = config . disableDescriptions ? visit ( ast , { leave : node => ( { ... node , description : undefined , } ) , } ) : ast ; schema = config . disableDescriptions ? buildASTSchema ( ast ) : schema ; return { schema , ast , } ; }