/
index.ts
84 lines (74 loc) · 2.41 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
import { GraphQLSchema, introspectionFromSchema } from 'graphql';
import { PluginFunction, PluginValidateFn, Types, removeFederation } from '@graphql-codegen/plugin-helpers';
import { extname } from 'path';
import { getConfigValue } from '../../visitor-plugin-common/src/utils';
/**
* @description This plugin generates a GraphQL introspection file based on your GraphQL schema.
*/
export interface IntrospectionPluginConfig {
/**
* @description Set to `true` in order to minify the JSON output.
* @default false
*
* @exampleMarkdown
* ```yml
* generates:
* introspection.json:
* plugins:
* - introspection
* config:
* minify: true
* ```
*/
minify?: boolean;
/**
* @description Whether to include descriptions in the introspection result.
* @default true
*/
descriptions?: boolean;
/**
* @description Whether to include `specifiedByUrl` in the introspection result.
* @default false
*/
specifiedByUrl?: boolean;
/**
* @description Whether to include `isRepeatable` flag on directives.
* @default true
*/
directiveIsRepeatable?: boolean;
/**
* @description Whether to include `description` field on schema.
* @default false
*/
schemaDescription?: boolean;
// Internal
federation?: boolean;
}
export const plugin: PluginFunction<IntrospectionPluginConfig> = async (
schema: GraphQLSchema,
_documents,
pluginConfig: IntrospectionPluginConfig
): Promise<string> => {
const cleanSchema = pluginConfig.federation ? removeFederation(schema) : schema;
const descriptions = getConfigValue(pluginConfig.descriptions, true);
const directiveIsRepeatable = getConfigValue(pluginConfig.directiveIsRepeatable, true);
const schemaDescription = getConfigValue(pluginConfig.schemaDescription, undefined);
const specifiedByUrl = getConfigValue(pluginConfig.specifiedByUrl, undefined);
const introspection = introspectionFromSchema(cleanSchema, {
descriptions,
directiveIsRepeatable,
schemaDescription,
specifiedByUrl,
});
return pluginConfig.minify ? JSON.stringify(introspection) : JSON.stringify(introspection, null, 2);
};
export const validate: PluginValidateFn<any> = async (
schema: GraphQLSchema,
documents: Types.DocumentFile[],
config: any,
outputFile: string
) => {
if (extname(outputFile) !== '.json') {
throw new Error(`Plugin "introspection" requires extension to be ".json"!`);
}
};