import { readFile as origReadFile , writeFile as origWriteFile } from 'fs' ; import { graphql , GraphQLSchema , printSchema , lexicographicSortSchema } from 'graphql' ; import * as GQL from 'graphql' ; import { PostGraphileOptions } from '../../interfaces' ; import { promisify } from 'util' ; const introspectionQuery = typeof GQL . getIntrospectionQuery === 'function' ? GQL . getIntrospectionQuery ( ) : ( GQL as any ) . introspectionQuery ; const readFile = promisify ( origReadFile ) ; const writeFile = promisify ( origWriteFile ) ; async function writeFileIfDiffers ( path , contents ) { let oldContents = null ; try { oldContents = await readFile ( path , 'utf8' ) ; } catch ( e ) { } if ( oldContents !== contents ) { await writeFile ( path , contents ) ; } } /**
 * Exports a PostGraphile schema by looking at a Postgres client.
 */ export default async function exportPostGraphileSchema ( schema , options = { } ) { const jsonPath = typeof options . exportJsonSchemaPath === 'string' ? options . exportJsonSchemaPath : null ; const graphqlPath = typeof options . exportGqlSchemaPath === 'string' ? options . exportGqlSchemaPath : null ; const finalSchema = options . sortExport && lexicographicSortSchema && ( jsonPath || graphqlPath ) ? lexicographicSortSchema ( schema ) : schema ; if ( jsonPath ) { const result = await graphql ( finalSchema , introspectionQuery ) ; await writeFileIfDiffers ( jsonPath , JSON . stringify ( result , null , 2 ) ) ; } if ( graphqlPath ) { await writeFileIfDiffers ( graphqlPath , printSchema ( finalSchema ) ) ; } }