-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
index.ts
142 lines (133 loc) · 3.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import {
GraphQLSchema,
parse,
extendSchema,
printIntrospectionSchema,
printSchema,
visit,
buildASTSchema,
print,
} from 'graphql';
import {
PluginFunction,
PluginValidateFn,
Types,
removeFederation,
getCachedDocumentNodeFromSchema,
} from '@graphql-codegen/plugin-helpers';
import { extname } from 'path';
/**
* @description This plugin prints the merged schema as string. If multiple schemas are provided, they will be merged and printed as one schema.
*/
export interface SchemaASTConfig {
/**
* @description Include directives to Schema output.
* @default false
*
* @exampleMarkdown
* ```yml
* schema:
* - './src/schema.graphql'
* generates:
* path/to/file.graphql:
* plugins:
* - schema-ast
* config:
* includeDirectives: true
* ```
*/
includeDirectives?: boolean;
/**
* @description Include introspection types to Schema output.
* @default false
*
* @exampleMarkdown
* ```yml
* schema:
* - './src/schema.graphql'
* generates:
* path/to/file.graphql:
* plugins:
* - schema-ast
* config:
* includeIntrospectionTypes: true
* ```
*/
includeIntrospectionTypes?: boolean;
/**
* @description Set to true in order to print description as comments (using # instead of """)
* @default false
*
* @exampleMarkdown
* ```yml
* schema: http://localhost:3000/graphql
* generates:
* schema.graphql:
* plugins:
* - schema-ast
* config:
* commentDescriptions: true
* ```
*/
commentDescriptions?: boolean;
/**
* @description Set to true in order get the schema lexicographically sorted before printed.
* @default false
*/
sort?: boolean;
federation?: boolean;
}
export const plugin: PluginFunction<SchemaASTConfig> = async (
schema: GraphQLSchema,
_documents,
{ commentDescriptions = false, includeDirectives = false, includeIntrospectionTypes = false, sort = false, federation }
): Promise<string> => {
const transformedSchemaAndAst = transformSchemaAST(schema, { sort, federation, includeIntrospectionTypes });
return [
includeIntrospectionTypes ? printIntrospectionSchema(transformedSchemaAndAst.schema) : null,
includeDirectives
? print(transformedSchemaAndAst.ast)
: (printSchema as any)(transformedSchemaAndAst.schema, { commentDescriptions }),
]
.filter(Boolean)
.join('\n');
};
export const validate: PluginValidateFn<any> = async (
_schema: GraphQLSchema,
_documents: Types.DocumentFile[],
_config: SchemaASTConfig,
outputFile: string,
allPlugins: Types.ConfiguredPlugin[]
) => {
const singlePlugin = allPlugins.length === 1;
if (singlePlugin && extname(outputFile) !== '.graphql') {
throw new Error(`Plugin "schema-ast" requires extension to be ".graphql"!`);
}
};
export function transformSchemaAST(schema: GraphQLSchema, config: { [key: string]: any }) {
schema = config.federation ? removeFederation(schema) : schema;
if (config.includeIntrospectionTypes) {
// See: https://spec.graphql.org/June2018/#sec-Schema-Introspection
const introspectionAST = parse(`
extend type Query {
__schema: __Schema!
__type(name: String!): __Type
}
`);
schema = extendSchema(schema, introspectionAST);
}
let ast = getCachedDocumentNodeFromSchema(schema);
ast = config.disableDescriptions
? visit(ast, {
leave: node => ({
...node,
description: undefined,
}),
})
: ast;
schema = config.disableDescriptions ? buildASTSchema(ast) : schema;
return {
schema,
ast,
};
}