All Downloads are FREE. Search and download functionalities are using the official Maven repository.

node_modules.apollo-codegen.src.printSchema.ts Maven / Gradle / Ivy

There is a newer version: 3.3.1
Show newest version
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);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy