@graphql-codegen/schema-ast 2.2.1-alpha-e04750ad3.0 → 2.3.1-alpha-9e27900cc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -21,6 +21,23 @@ export interface SchemaASTConfig {
21
21
  * ```
22
22
  */
23
23
  includeDirectives?: boolean;
24
+ /**
25
+ * @description Include introspection types to Schema output.
26
+ * @default false
27
+ *
28
+ * @exampleMarkdown
29
+ * ```yml
30
+ * schema:
31
+ * - './src/schema.graphql'
32
+ * generates:
33
+ * path/to/file.graphql:
34
+ * plugins:
35
+ * - schema-ast
36
+ * config:
37
+ * includeIntrospectionTypes: true
38
+ * ```
39
+ */
40
+ includeIntrospectionTypes?: boolean;
24
41
  /**
25
42
  * @description Set to true in order to print description as comments (using # instead of """)
26
43
  * @default false
package/index.js CHANGED
@@ -6,12 +6,16 @@ const graphql = require('graphql');
6
6
  const pluginHelpers = require('@graphql-codegen/plugin-helpers');
7
7
  const path = require('path');
8
8
 
9
- const plugin = async (schema, _documents, { commentDescriptions = false, includeDirectives = false, sort = false, federation }) => {
10
- const transformedSchemaAndAst = transformSchemaAST(schema, { sort, federation });
11
- if (includeDirectives) {
12
- return graphql.print(transformedSchemaAndAst.ast);
13
- }
14
- return graphql.printSchema(transformedSchemaAndAst.schema, { commentDescriptions: commentDescriptions });
9
+ const plugin = async (schema, _documents, { commentDescriptions = false, includeDirectives = false, includeIntrospectionTypes = false, sort = false, federation }) => {
10
+ const transformedSchemaAndAst = transformSchemaAST(schema, { sort, federation, includeIntrospectionTypes });
11
+ return [
12
+ includeIntrospectionTypes ? graphql.printIntrospectionSchema(transformedSchemaAndAst.schema) : null,
13
+ includeDirectives
14
+ ? graphql.print(transformedSchemaAndAst.ast)
15
+ : graphql.printSchema(transformedSchemaAndAst.schema, { commentDescriptions }),
16
+ ]
17
+ .filter(Boolean)
18
+ .join('\n');
15
19
  };
16
20
  const validate = async (_schema, _documents, _config, outputFile, allPlugins) => {
17
21
  const singlePlugin = allPlugins.length === 1;
@@ -21,6 +25,16 @@ const validate = async (_schema, _documents, _config, outputFile, allPlugins) =>
21
25
  };
22
26
  function transformSchemaAST(schema, config) {
23
27
  schema = config.federation ? pluginHelpers.removeFederation(schema) : schema;
28
+ if (config.includeIntrospectionTypes) {
29
+ // See: https://spec.graphql.org/June2018/#sec-Schema-Introspection
30
+ const introspectionAST = graphql.parse(`
31
+ extend type Query {
32
+ __schema: __Schema!
33
+ __type(name: String!): __Type
34
+ }
35
+ `);
36
+ schema = graphql.extendSchema(schema, introspectionAST);
37
+ }
24
38
  let ast = pluginHelpers.getCachedDocumentNodeFromSchema(schema);
25
39
  ast = config.disableDescriptions
26
40
  ? graphql.visit(ast, {
package/index.mjs CHANGED
@@ -1,13 +1,17 @@
1
- import { print, printSchema, visit, buildASTSchema } from 'graphql';
1
+ import { printIntrospectionSchema, print, printSchema, parse, extendSchema, visit, buildASTSchema } from 'graphql';
2
2
  import { removeFederation, getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers';
3
3
  import { extname } from 'path';
4
4
 
5
- const plugin = async (schema, _documents, { commentDescriptions = false, includeDirectives = false, sort = false, federation }) => {
6
- const transformedSchemaAndAst = transformSchemaAST(schema, { sort, federation });
7
- if (includeDirectives) {
8
- return print(transformedSchemaAndAst.ast);
9
- }
10
- return printSchema(transformedSchemaAndAst.schema, { commentDescriptions: commentDescriptions });
5
+ const plugin = async (schema, _documents, { commentDescriptions = false, includeDirectives = false, includeIntrospectionTypes = false, sort = false, federation }) => {
6
+ const transformedSchemaAndAst = transformSchemaAST(schema, { sort, federation, includeIntrospectionTypes });
7
+ return [
8
+ includeIntrospectionTypes ? printIntrospectionSchema(transformedSchemaAndAst.schema) : null,
9
+ includeDirectives
10
+ ? print(transformedSchemaAndAst.ast)
11
+ : printSchema(transformedSchemaAndAst.schema, { commentDescriptions }),
12
+ ]
13
+ .filter(Boolean)
14
+ .join('\n');
11
15
  };
12
16
  const validate = async (_schema, _documents, _config, outputFile, allPlugins) => {
13
17
  const singlePlugin = allPlugins.length === 1;
@@ -17,6 +21,16 @@ const validate = async (_schema, _documents, _config, outputFile, allPlugins) =>
17
21
  };
18
22
  function transformSchemaAST(schema, config) {
19
23
  schema = config.federation ? removeFederation(schema) : schema;
24
+ if (config.includeIntrospectionTypes) {
25
+ // See: https://spec.graphql.org/June2018/#sec-Schema-Introspection
26
+ const introspectionAST = parse(`
27
+ extend type Query {
28
+ __schema: __Schema!
29
+ __type(name: String!): __Type
30
+ }
31
+ `);
32
+ schema = extendSchema(schema, introspectionAST);
33
+ }
20
34
  let ast = getCachedDocumentNodeFromSchema(schema);
21
35
  ast = config.disableDescriptions
22
36
  ? visit(ast, {
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@graphql-codegen/schema-ast",
3
- "version": "2.2.1-alpha-e04750ad3.0",
3
+ "version": "2.3.1-alpha-9e27900cc.0",
4
4
  "description": "GraphQL Code Generator plugin for generating a .graphql file from a given schema",
5
5
  "peerDependencies": {
6
- "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0"
6
+ "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
7
7
  },
8
8
  "dependencies": {
9
- "@graphql-codegen/plugin-helpers": "2.2.0-alpha-e04750ad3.0",
9
+ "@graphql-codegen/plugin-helpers": "^2.3.0",
10
10
  "@graphql-tools/utils": "^8.1.1",
11
11
  "tslib": "~2.3.0"
12
12
  },