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

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

There is a newer version: 3.3.1
Show newest version
import * as fs from 'fs';

import { buildASTSchema, graphql, parse, DocumentNode, GraphQLSchema } from 'graphql';
import { introspectionQuery } from 'graphql/utilities';

import { ToolError } from 'apollo-codegen-core/lib/errors'

declare module "graphql/utilities/buildASTSchema" {
  function buildASTSchema(
    ast: DocumentNode,
    options?: { assumeValid?: boolean, commentDescriptions?: boolean },
  ): GraphQLSchema;
}

export async function introspect(schemaContents: string) {
  const schema = buildASTSchema(parse(schemaContents), { commentDescriptions: true });
  return await graphql(schema, introspectionQuery);
}

export default async function introspectSchema(schemaPath: string, outputPath: string) {
  if (!fs.existsSync(schemaPath)) {
    throw new ToolError(`Cannot find GraphQL schema file: ${schemaPath}`);
  }

  const schemaContents = fs.readFileSync(schemaPath).toString();
  const result = await introspect(schemaContents);

  if (result.errors) {
    throw new ToolError(`Errors in introspection query result: ${result.errors}`);
  }

  fs.writeFileSync(outputPath, JSON.stringify(result, null, 2));
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy