Skip to content

Commit b4b6bbf

Browse files
authored
Replace persistgraphql with latest graphql/apollo-utils (#23)
* replace persistedgraphql with latest graphql/apollo-utils * remove formatting * add a comment * update webpack, add missing deps
1 parent 34d48c7 commit b4b6bbf

File tree

3 files changed

+2204
-2036
lines changed

3 files changed

+2204
-2036
lines changed

index.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const vm = require('vm');
22
const os = require('os');
33
const loaderUtils = require('loader-utils');
4-
const { ExtractGQL } = require('persistgraphql/lib/src/ExtractGQL');
5-
const queryTransformers = require('persistgraphql/lib/src/queryTransformers');
4+
const { print, parse, separateOperations } = require('graphql');
5+
const { addTypenameToDocument } = require('apollo-utilities');
66

77
module.exports = function graphQLPersistedDocumentLoader(content) {
88
const deps = [];
@@ -73,31 +73,27 @@ module.exports = function graphQLPersistedDocumentLoader(content) {
7373
};
7474

7575
function tryAddDocumentId(options, content, querySource) {
76-
const queryMap = new ExtractGQL({
77-
queryTransformers: [options.addTypename && queryTransformers.addTypenameTransformer].filter(Boolean)
78-
}).createOutputMapFromString(querySource);
76+
// Every file may contain multiple operations
77+
const operations = separateOperations(parse(querySource));
7978

80-
const queries = Object.keys(queryMap);
81-
if (queries.length > 1) {
82-
queries
83-
.map(query => {
84-
const matched = query.match(/^(mutation|query)\ ([^\ \(\{]*)/)
85-
if (!matched) {
86-
return false
87-
}
88-
return {
89-
operationName: matched[2],
90-
id: generateIdForQuery(options, query)
91-
}
92-
})
93-
.filter(isValid => !!isValid)
94-
.forEach(({ id, operationName }) => {
95-
content += `${os.EOL}module.exports["${operationName}"].documentId = ${JSON.stringify(id)};`
96-
})
97-
} else if (queries.length === 1) {
98-
const queryId = generateIdForQuery(options, Object.keys(queryMap)[0]);
99-
content += `${os.EOL}doc.documentId = ${JSON.stringify(queryId)}`;
100-
}
79+
Object.keys(operations).map((operation) => {
80+
const document = options.addTypename
81+
? addTypenameToDocument(operations[operation])
82+
: operations[operation];
83+
const query = print(document);
84+
85+
const queryId = generateIdForQuery(options, query);
86+
87+
// Add them as exports to the final file
88+
// If there is only one operation, it will be the default export
89+
if (Object.keys(operations).length > 1) {
90+
content += `${
91+
os.EOL
92+
}module.exports["${operation}"].documentId = ${JSON.stringify(queryId)};`;
93+
} else {
94+
content += `${os.EOL}doc.documentId = ${JSON.stringify(queryId)}`;
95+
}
96+
});
10197

10298
return content;
10399
}

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
"author": "Leonardo Andres Garcia Crespo <[email protected]>",
88
"license": "MIT",
99
"scripts": {
10-
"start": "webpack-dev-server --config ./example/webpack.config.js"
10+
"start": "webpack serve --config ./example/webpack.config.js"
1111
},
1212
"devDependencies": {
1313
"graphql-tag": "^2.9.2",
14-
"webpack": "^3.8.1",
15-
"webpack-dev-server": "^3.1.11"
14+
"webpack": "4.29.6",
15+
"webpack-cli": "^4.2.0",
16+
"webpack-dev-server": "^3.11.0"
1617
},
1718
"dependencies": {
18-
"loader-utils": "^1.1.0",
19-
"persistgraphql": "^0.3.11"
19+
"apollo-utilities": "^1.3.4",
20+
"graphql": "^15.4.0",
21+
"loader-utils": "^1.1.0"
2022
},
2123
"peerDependencies": {
2224
"graphql-tag": "^2.8.0"

0 commit comments

Comments
 (0)