apollo-codegen
Version:
Generate API code or type annotations based on a GraphQL schema and query documents
32 lines (24 loc) • 872 B
text/typescript
import * as fs from 'fs';
import { buildClientSchema, printSchema } from 'graphql';
import { ToolError } from 'apollo-codegen-core/lib/errors'
import { PrinterOptions } from 'graphql/utilities/schemaPrinter';
export default async function printSchemaFromIntrospectionResult(
schemaPath: string,
outputPath: string,
options?: PrinterOptions
) {
if (!fs.existsSync(schemaPath)) {
throw new ToolError(`Cannot find GraphQL schema file: ${schemaPath}`);
}
const schemaJSON = JSON.parse(fs.readFileSync(schemaPath, 'utf8'));
if (!schemaJSON.data) {
throw new ToolError(`No introspection query result data found in: ${schemaPath}`);
}
const schema = buildClientSchema(schemaJSON.data);
const schemaIDL = printSchema(schema, options);
if (outputPath) {
fs.writeFileSync(outputPath, schemaIDL);
} else {
console.log(schemaIDL);
}
}