UNPKG

1.13 kBPlain TextView Raw
1import * as fs from 'fs';
2
3import { buildASTSchema, graphql, parse, DocumentNode, GraphQLSchema } from 'graphql';
4import { introspectionQuery } from 'graphql/utilities';
5
6import { ToolError } from 'apollo-codegen-core/lib/errors'
7
8declare module "graphql/utilities/buildASTSchema" {
9 function buildASTSchema(
10 ast: DocumentNode,
11 options?: { assumeValid?: boolean, commentDescriptions?: boolean },
12 ): GraphQLSchema;
13}
14
15export async function introspect(schemaContents: string) {
16 const schema = buildASTSchema(parse(schemaContents), { commentDescriptions: true });
17 return await graphql(schema, introspectionQuery);
18}
19
20export default async function introspectSchema(schemaPath: string, outputPath: string) {
21 if (!fs.existsSync(schemaPath)) {
22 throw new ToolError(`Cannot find GraphQL schema file: ${schemaPath}`);
23 }
24
25 const schemaContents = fs.readFileSync(schemaPath).toString();
26 const result = await introspect(schemaContents);
27
28 if (result.errors) {
29 throw new ToolError(`Errors in introspection query result: ${result.errors}`);
30 }
31
32 fs.writeFileSync(outputPath, JSON.stringify(result, null, 2));
33}