Grats provides an implementation-first, code-first approach to building GraphQL servers in TypeScript, enabling developers to define their GraphQL schema directly from their existing TypeScript types and functions using JSDoc-style annotations. This library leverages static analysis of your TypeScript code to automatically generate an executable GraphQL schema, eliminating the need for schema duplication or synchronization between SDL and resolver implementations. The current stable version, 0.0.36, reflects its active development phase, with frequent iterative updates and potentially breaking changes in minor `0.0.x` increments. Key differentiators include zero runtime overhead as the schema is extracted at build time, and a developer experience focused on reducing mental overhead by making the TypeScript implementation the single source of truth for the GraphQL schema, similar to approaches seen in Python (Strawberry) or C# (Hot Chocolate) but adapted for TypeScript's static nature.
npm install gratsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up a basic Grats project with `graphql-yoga`. It defines a `User` type and `Context` using docblock annotations, generates the schema, and runs a GraphQL server.
Set `"type": "module"` in your `package.json` and update all Grats-related imports from `require()` to `import`. Adjust `tsconfig.json`'s `module` and `moduleResolution` to `esnext` or `nodenext` respectively.
Add `/** @gqlContext */` to your context type definition. If using the `info` argument in a resolver, add `import { GqlInfo } from 'grats';` and type the argument as `info: GqlInfo`.Ensure that the call to the generated `getSchema` function in your server setup (often found in `schema.ts`) includes a configuration object, especially if you define custom scalars. Consult the Grats documentation for detailed custom scalar configuration.
Always use `/** @tagName */` in JSDoc comments preceding the relevant TypeScript class, type, or function. Do not attempt to import these as runtime constructs.
Refer to the 'Limitations of Grats' documentation. Avoid wrapping methods in functions that obscure their type signatures. For enums, consider using TypeScript `enum`s or direct GraphQL SDL if these features are critical.
Ensure your project's `typescript` dependency in `package.json` meets the `>=5.5` requirement. Upgrade TypeScript if necessary.
Update `package.json` to include `"type": "module"`. Change all `require()` statements to `import` statements. Ensure `tsconfig.json` has `"module": "esnext"` and `"moduleResolution": "bundler"` or `"nodenext"`.
Add the `/** @gqlContext */` JSDoc comment immediately preceding your TypeScript class or type definition that serves as the GraphQL context.
Ensure that any class, type, or function annotated with `/** @gqlType */`, `/** @gqlField */`, `/** @gqlQueryField */`, etc., is explicitly exported using `export` (e.g., `export class MyType { ... }`).Update the Grats configuration object in your generated `schema.ts` (or equivalent) to include `scalarSerializers` for each custom scalar you've defined, providing `serialize`, `parseValue`, and `parseLiteral` functions.