I have seen lots of questions regarding this, but the answers don’t seem to provide a complete picture.
I have a modern vue3 application which uses a graphql backend. I’m using vscode. I want to easily test my endpoints without doing any funky browser/frontend development. In my project root, I created a folder called api_test.
I plan to test by running the root.js file in the node repl in the terminal coupled to vscode. I execute it like this:
PS M:sourceAdvancedStorageSite> node ./api_test/root.js
My root.js
file looks like this:
//root.js
// import {queries} from import('../src/graphql/custom-queries')
// let {queries} = await import('../src/graphql/custom-queries')
// let queries = require('../src/graphql/custom-queries')
console.log(queries)
I have 3 different strategies I’m trying to import my modules. My modules use imports in them. This seems important because I have found other SO answers that seem to break based on the import chain. Here is the result I get with each strategy:
// code
import {queries} from import('../src/graphql/custom-queries')
console.log(queries)
// output
M:sourceAdvancedStorageSiteapi_testroot.js:1
let {queries} = await import('../src/graphql/custom-queries')
^^^^^
SyntaxError: await is only valid in async functions and the top level bodies of modules
// code
let {queries} = await import('../src/graphql/custom-queries')
console.log(queries)
// output
M:sourceAdvancedStorageSiteapi_testroot.js:1
let {queries} = await import('../src/graphql/custom-queries')
^^^^^
SyntaxError: await is only valid in async functions and the top level bodies of modules
// code
let queries = require('../src/graphql/custom-queries')
console.log(queries)
// output
M:sourceAdvancedStorageSitesrcgraphqlcustom-queries.js:1
export const listLeasesFull = /* GraphQL */ `
^^^^^^
SyntaxError: Unexpected token 'export'
Extra info:
I do a lot more backend development with python, in which case it is easy to immediately highlight a section of code and run it to test it. I want to find the fastest/most efficient way to test client side logic (written in js), which I can then copy and paste to my front end. If there is a faster way to write logic and test it against an API than this, I am open to that solution as well.