Some advice on setting up your project and your processes to make the most of Gratos.

Make it easy to run Gratos

Add a script that runs gratos to your project's package.json. The example app's:

examples/orders/package.json
"codegen": "gratos --tsconfig src/tsconfig.json",

Any contributor can then regenerate the schema by running npm run codegen.

Info

You can pass more arguments to gratos after --: npm run codegen -- --check, for example.

To regenerate the schema as you work, restart your app on every change with node --watch-path, and boot it through the gratos/dev entry, which regenerates the schema before it loads the app (see The development loop). The example app's dev script:

examples/orders/package.json
"dev": "GRATS_DEV=1 node --watch-path=src/models --watch-path=src/services --watch-preserve-output src/server.ts",

--watch-path watches only the paths it lists, and turns off watching of the entry point and its imports. This script restarts on a change under src/models or src/services, but not on an edit of src/server.ts. To restart on that too, add the server's file to the watched paths: --watch-path=src/server.ts.

Check in your schema

Include the GraphQL and TypeScript schemas Gratos generates in your repository. This has several advantages:

  1. You can easily see the changes to your schema during code reviews.
  2. Inspecting the schema is as simple as opening a file.
  3. Other tools, such as GraphiQL, client codegen, and editor integrations can easily read the schema.
  4. Other code tools can see that your GraphQL code is used (not "dead code").

Managing autoformatting and generated files

Disable autoformatting for the generated files, for example by listing their paths in your formatter's ignore file. These paths depend on where your configuration puts the files. This repository's formatter, oxfmt, ignores them so:

.oxfmtrc.json
"**/src/generated/**",
"apps/docs/content/samples/*/generated/**",

Don't format them afterwards either: gratos --check (below) compares each generated file, byte for byte, with what Gratos writes, so a formatted file fails it.

Continuous integration

So that your code base never gets into a state where Gratos cannot extract types because of errors, and your schema file always matches your implementation, add a CI step that checks the checked-in schema. gratos --check does it: it writes nothing, and exits with an error when a generated file is missing or out of date. The example app's script:

examples/orders/package.json
"codegen:check": "gratos --check --tsconfig src/tsconfig.json",