-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypescript-typedefs.js
51 lines (42 loc) · 1.17 KB
/
typescript-typedefs.js
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
/**
* {@link https://github.com/dotansimha/graphql-code-generator/issues/3899}
*/
const { printSchemaWithDirectives } = require('@graphql-tools/utils');
const { Kind } = require('graphql');
/**
* @param {import('graphql').GraphQLSchema} schema
*/
const forceBlockDescriptionsOnDirectives = (schema) => {
const directives = schema.getDirectives();
for (const directive of directives) {
if (directive.astNode || !directive.description) {
continue;
}
directive.astNode = {
kind: Kind.DIRECTIVE_DEFINITION,
// Use block styling to ensure that our hack in `printTypeScriptTypeDefs`
// to escape backticks still outputs valid GraphQL type defs.
description: {
kind: Kind.STRING,
value: directive.description,
block: true,
},
};
}
};
/**
* @param {string} source
*/
const printTypeScriptTypeDefs = (source) => `
import gql from 'graphql-tag';
export const typeDefs = gql\`
${source.replace(/`/g, '\\`')}
\`;
`;
module.exports = {
plugin: (schema) => {
forceBlockDescriptionsOnDirectives(schema);
const source = printSchemaWithDirectives(schema);
return printTypeScriptTypeDefs(source);
},
};