graphql-import-loader icon indicating copy to clipboard operation
graphql-import-loader copied to clipboard

Template string interpolation in comment breaks loaded graphql asset

Open vctr-dev opened this issue 4 years ago • 0 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

If there is a comment in the .graphql file e.g. ${FIELD} ${OPERATOR}..., this breaks the generated file as it would treat them as a string interpolation due to backticks being used to wrap the export.

My solution is to replace ${ with \${, since $ is a valid symbol in graphql.

Here is the diff that solved my problem:

diff --git a/node_modules/graphql-import-loader/dist/src/index.js b/node_modules/graphql-import-loader/dist/src/index.js
index be4fa7b..ae741f9 100644
--- a/node_modules/graphql-import-loader/dist/src/index.js
+++ b/node_modules/graphql-import-loader/dist/src/index.js
@@ -4,7 +4,7 @@ var graphql_import_1 = require("graphql-import");
 function default_1(source) {
     var callback = this.async();
     this.cacheable();
-    callback(null, "module.exports = `" + graphql_import_1.importSchema(source).replace(/`/g, '\\`') + "`");
+    callback(null, "module.exports = `" + graphql_import_1.importSchema(source).replace(/`/g, '\\`').replace(/\${/g, '\\${') + "`");
 }
 exports.default = default_1;
 //# sourceMappingURL=index.js.map
\ No newline at end of file

This issue body was partially generated by patch-package.

vctr-dev avatar Oct 15 '21 02:10 vctr-dev