UNPKG

2.27 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.transformSchemaAST = exports.validate = exports.plugin = void 0;
4const graphql_1 = require("graphql");
5const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
6const path_1 = require("path");
7const 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};
18exports.plugin = plugin;
19const 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};
25exports.validate = validate;
26function transformSchemaAST(schema, config) {
27 schema = config.federation ? (0, plugin_helpers_1.removeFederation)(schema) : schema;
28 if (config.includeIntrospectionTypes) {
29 // See: https://spec.graphql.org/June2018/#sec-Schema-Introspection
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}
53exports.transformSchemaAST = transformSchemaAST;