Use a schema.graphql file
The very first thing that I wanted so badly from the very beginning was the ability to separate my schema from TS code. So here is how you can do it:
-
I told esbuild to copy my graphql file to the where it should. Note that my first attempt was OK, I just did not know one more thing.
NOTE, esbuild might copy your schema to the wrong place, so to prevent a runtime error saying that it cannot not find
schema.graphqlmake sure it is copying it to the right place. E.g. in theserver-statisticsapp I learned thatNxgenerate amain.jsand in that file it is importing yourmain.ts(of course the transpiled version which is also calledmain.js). Thus the reason for this in myproject.json:{ "input": "{projectRoot}/src/", "glob": "schema.graphql", "output": "apps/server-statistics/src" } - Then I read it with
fs.readFileSync:const schema = readFileSync(join(__dirname, 'schema.graphql'), { encoding: 'utf8', }); - Lastly I passed it as my schema.
You can see a working example here: