1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.transformSchemaAST = exports.validate = exports.plugin = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
6 | const path_1 = require("path");
|
7 | const plugin = async (schema, _documents, { commentDescriptions = false, includeDirectives = false, includeIntrospectionTypes = false, sort = false, federation, }) => {
|
8 | const transformedSchemaAndAst = transformSchemaAST(schema, { sort, federation, includeIntrospectionTypes });
|
9 | return [
|
10 | includeIntrospectionTypes ? (0, graphql_1.printIntrospectionSchema)(transformedSchemaAndAst.schema) : null,
|
11 | includeDirectives
|
12 | ? (0, graphql_1.print)(transformedSchemaAndAst.ast)
|
13 | : graphql_1.printSchema(transformedSchemaAndAst.schema, { commentDescriptions }),
|
14 | ]
|
15 | .filter(Boolean)
|
16 | .join('\n');
|
17 | };
|
18 | exports.plugin = plugin;
|
19 | const validate = async (_schema, _documents, _config, outputFile, allPlugins) => {
|
20 | const singlePlugin = allPlugins.length === 1;
|
21 | if (singlePlugin && (0, path_1.extname)(outputFile) !== '.graphql') {
|
22 | throw new Error(`Plugin "schema-ast" requires extension to be ".graphql"!`);
|
23 | }
|
24 | };
|
25 | exports.validate = validate;
|
26 | function transformSchemaAST(schema, config) {
|
27 | schema = config.federation ? (0, plugin_helpers_1.removeFederation)(schema) : schema;
|
28 | if (config.includeIntrospectionTypes) {
|
29 |
|
30 | const introspectionAST = (0, graphql_1.parse)(`
|
31 | extend type Query {
|
32 | __schema: __Schema!
|
33 | __type(name: String!): __Type
|
34 | }
|
35 | `);
|
36 | schema = (0, graphql_1.extendSchema)(schema, introspectionAST);
|
37 | }
|
38 | let ast = (0, plugin_helpers_1.getCachedDocumentNodeFromSchema)(schema);
|
39 | ast = config.disableDescriptions
|
40 | ? (0, graphql_1.visit)(ast, {
|
41 | leave: node => ({
|
42 | ...node,
|
43 | description: undefined,
|
44 | }),
|
45 | })
|
46 | : ast;
|
47 | schema = config.disableDescriptions ? (0, graphql_1.buildASTSchema)(ast) : schema;
|
48 | return {
|
49 | schema,
|
50 | ast,
|
51 | };
|
52 | }
|
53 | exports.transformSchemaAST = transformSchemaAST;
|