1

I have a nodejs application where I am using graphql queries and mutations which are in template literal format.

I want to store all these template literals in one single file and access it from anywhere in the application.

The template literals should be immutable so that it doesn't get changed.

What would be the best way to do this nodejs.

A solution I found in stackoverflow is something like this.


class GraphqlLiteral {
  constructor() {
  }

  static #query1 =`string literal template` ;

  static getQuery1() {
    return this.#query1;
  }
}

I tried the above solution in my nodejs app in glicth but its says unexpected token #.

regShank
  • 215
  • 2
  • 12
  • The `#` is not a legal way to start a variable name. Try `const myVar = whatever` The `static` keyword is for methods, not constants. – jmargolisvt Aug 06 '20 at 14:36
  • @jmargolisvt Well actually it is valid way to declare private field. Try it in latest chrome console. – Yury Tarabanko Aug 06 '20 at 14:43
  • 1
    "nodejs app in glicth but its says unexpected token #." private field syntax is quite new and your node version might not support it yet https://node.green/#ESNEXT-candidate--stage-3--static-class-fields You'd need node 12.4 at least – Yury Tarabanko Aug 06 '20 at 14:45
  • @Yury Tarbanko whats is the best way to do it then without updating the node version – regShank Aug 07 '20 at 17:33
  • 1
    @regShank Just use a normal module way of encapsulating things. `exports.query1 = \`string literal template\``. I don't think you need a class for it. Or if you can use ES-modules `export const query1 = ...` – Yury Tarabanko Aug 07 '20 at 17:39

2 Answers2

1

you could create a simple js file. Have an object, don't export it. Export the getter.

Another common pratice is to use .ENV file.

https://www.npmjs.com/package/dotenv

const queryStrings = {

 "query1":"query1"
}
export default (key) => return queryStrings[key] ;
AnonyMouze
  • 416
  • 2
  • 7
  • I am getting an error saying export is an unexpected token. I just added the your code and used const queryStrings = require("../const/queryStrings.js"); in the js file I am planning to use the constant file – regShank Aug 07 '20 at 17:30
  • Your env doesnt suport es6 then u can use module.export instead – AnonyMouze Aug 08 '20 at 02:00
0

What characters are valid for JavaScript variable names?

You can't start a variable with #, which is why it is an unexpected token.

Below is how I do it. I don't see a reason to make a class and a function to retrieve values, it adds complexity. Just declare constant values and export them (Unless you want to add some unique functionality - like a static method that builds queries based on parameters).

Just make a query.js file:

    export const query1 = `this is query 1`;
    export const query2 = `this is query 2`;

Then in a new file:

    import query1 from './query';
    
    console.log(query1);  // "this is query 1"
Diesel
  • 5,099
  • 7
  • 43
  • 81
  • I got the following error when I added your code: export const QUE_VENDOR_BY_PHONE = `query fetchVendor($phoneNumber: String!) { ^^^^^^ SyntaxError: Unexpected token export – regShank Aug 07 '20 at 17:32