-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
105 lines (90 loc) · 3.38 KB
/
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
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
const vm = require('vm');
const os = require('os');
const loaderUtils = require('loader-utils');
const { print, parse, separateOperations } = require('graphql');
const { addTypenameToDocument } = require('apollo-utilities');
module.exports = function graphQLPersistedDocumentLoader(content) {
const deps = [];
const context = this;
const options = loaderUtils.getOptions(this) || {};
// Create a fake sandbox to intercept the query dependencies when
// executing the generated code from the graphql-tag loader.
const sandbox = {
require(file) {
deps.push(new Promise((resolve, reject) => {
context.loadModule(file, (err, source, sourceMap, module) => {
if (err) {
reject(err);
} else {
resolve({ source, sourceMap, module });
}
});
}));
return { definitions: [] };
},
module: {
exports: null
}
};
// Run the graphql-tag loader generated code to capture the dependencies.
vm.runInNewContext(content, sandbox);
// Get the query document source from the exported module, and
// save it so that we can use it from other modules that may depend on
// this one.
const doc = sandbox.module.exports;
this._module._graphQLQuerySource = doc.loc.source.body;
if (deps.length === 0) {
// If no deps, just try to generate the document id from the
// source returned from graphql-tag loader result. This will
// add an id only if the source contains an operation.
content = tryAddDocumentId(options, content, this._module._graphQLQuerySource);
return content;
}
const callback = this.async();
// If we have dependencies, we retrieve their query sources and
// concatenate them with the one from this module, to create the
// full query source.
Promise.all(deps).then((modules) => {
modules.forEach((mod, index) => {
this._module._graphQLQuerySource += mod.module._graphQLQuerySource;
});
try {
// Now that we have all our dependencies' sources concatenated
// with this module's query source, we can send all that to
// generate the document id, if the resulting source
// is for an operation.
content = tryAddDocumentId(options, content, this._module._graphQLQuerySource);
} catch (e) {
callback(e);
}
callback(null, content);
}).catch((err) => {
console.log('error', err);
callback(err);
});
};
function tryAddDocumentId(options, content, querySource) {
// Every file may contain multiple operations
const operations = separateOperations(parse(querySource));
Object.keys(operations).map((operation) => {
const document = options.addTypename
? addTypenameToDocument(operations[operation])
: operations[operation];
const query = print(document);
const queryId = generateIdForQuery(options, query);
// Add them as exports to the final file
// If there is only one operation, it will be the default export
if (Object.keys(operations).length > 1) {
content += `${
os.EOL
}module.exports["${operation}"].documentId = ${JSON.stringify(queryId)};`;
} else {
content += `${os.EOL}doc.documentId = ${JSON.stringify(queryId)}`;
}
});
return content;
}
function generateIdForQuery(options, query) {
if (options.generateId) return options.generateId(query);
return require('crypto').createHash('sha256').update(query).digest('hex');
}