-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (32 loc) · 958 Bytes
/
index.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
const os = require('os');
const gqlLoader = require('graphql-tag/loader');
const { print, stripIgnoredCharacters } = require('graphql');
const safeEval = require('eval');
/**
* Transforms a GQL source file to stringified queries file.
* @param {string} src
*/
function GraphQLTagPrintedLoader(src) {
const oldSource = gqlLoader.call(this, src);
const doc = safeEval(oldSource);
let newSource = '';
for (const op of doc.definitions) {
if (op.kind !== 'OperationDefinition') {
continue;
}
if (!op.name) {
continue;
}
const opName = op.name.value;
const opCode = stripIgnoredCharacters(print(doc[opName]));
newSource += `
module.exports['${opName}'] = \`${opCode}\`;
`;
}
return newSource + os.EOL;
}
// Makes it work with Jest as well
GraphQLTagPrintedLoader.process = src => {
return GraphQLTagPrintedLoader.call({ cacheable() {} }, src);
};
module.exports = GraphQLTagPrintedLoader;