captbaritone / grats

Implementation-First GraphQL for TypeScript
https://grats.capt.dev
MIT License
308 stars 16 forks source link

Initial version of a ts-plugin #114

Closed captbaritone closed 9 months ago

captbaritone commented 9 months ago

TypeScript lets you specify a plugin in your typescript config which can add additional functionality in the editor. This is a good fit for Grats which is built around the TypeScript compiler and could report helpful errors.

I'm still working to figure out a way to efficiently (re)compute cross-file validations, but this should give us a start by reporting syntax-only issues.

Ideally we could specify grats as the plugin itself, but from what I can tell TypeScript's requirements around the way the plugin is invoked are going to be hard to get to work inside the grats module itself:

  1. The module has to use common js exports
  2. The init function has the be the sole export from the module
  3. The plugin module name must not have a slash in it

We could still explore having grats expose both a commonjs and ES Module version and have the commonjs version export the initi funciton with all the actual exports as properties on the function, but that's getting pretty crazy.

Since the logic of the plugin will be tightly coupled with Grats, I'm having the plugin extension just re-export the actual implementation from the grats module.

Example

https://github.com/captbaritone/grats/assets/162735/4d57481e-e559-472e-b5a6-5799a609cb89

Setup

Add the plugin to your tsconfig

{
  "grats": {
    "nullableByDefault": false
  },
  "compilerOptions": {
    // LIKE THIS
    "plugins": [{ "name": "grats-ts-plugin" }],
    "outDir": "dist",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "target": "esnext",
    "lib": ["esnext"],
    "strict": true
  }
}

Configure VSCode to use your local version of TypeScript. Via CMD+P "TypeScript: Select TypeScript Version..." with a .ts file open or a .vscode/settings.json for your project with:

{
    "typescript.tsdk": "node_modules/typescript/lib"
}
netlify[bot] commented 9 months ago

Deploy Preview for grats ready!

Name Link
Latest commit 21965a3f568cab4eaa70bbe5fa085e7961ea0303
Latest deploy log https://app.netlify.com/sites/grats/deploys/65b089c543e60b00082e0d38
Deploy Preview https://deploy-preview-114--grats.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.