1 | import * as fs from 'fs';
|
2 |
|
3 | import { buildASTSchema, graphql, parse, DocumentNode, GraphQLSchema } from 'graphql';
|
4 | import { introspectionQuery } from 'graphql/utilities';
|
5 |
|
6 | import { ToolError } from 'apollo-codegen-core/lib/errors'
|
7 |
|
8 | declare module "graphql/utilities/buildASTSchema" {
|
9 | function buildASTSchema(
|
10 | ast: DocumentNode,
|
11 | options?: { assumeValid?: boolean, commentDescriptions?: boolean },
|
12 | ): GraphQLSchema;
|
13 | }
|
14 |
|
15 | export async function introspect(schemaContents: string) {
|
16 | const schema = buildASTSchema(parse(schemaContents), { commentDescriptions: true });
|
17 | return await graphql(schema, introspectionQuery);
|
18 | }
|
19 |
|
20 | export 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 | }
|