Gratos builds a GraphQL schema from the TypeScript you already write: you mark classes, types and functions with docblock tags, and the gratos CLI writes the schema. Resolvers may return an Effect.Effect or, for a subscription, a Stream.Stream, and gratos-server serves the schema over HTTP, with @defer, @stream and subscriptions.

Gratos is Grats (its last TypeScript release, v0.0.36) on TypeScript 7's compiler API, taught to read Effect. If you know Grats, you know most of Gratos: the docblock tags, the type rules and the schema it generates are the same, and the CLI page says what the command left out.

Install

Add gratos as a development dependency: it is the compiler and the gratos CLI, and nothing of it runs in production. Add gratos-server, graphql and effect as dependencies. Both packages need Node 26.13 or later, and they share one version.

The compiler's peers are the Effect and GraphQL versions it was built against:

packages/gratos/package.json
"peerDependencies": {
  "@effect/platform-node": "^4.1.5",
  "effect": "^4.1.5",
  "graphql": "^17.1.0"
},

gratos-server needs graphql 17. Its Effect layer, gratos-server/effect, needs effect 4, and graphql-jit is optional, for the jit() plugin only:

packages/gratos-server/package.json
"peerDependencies": {
  "effect": "^4.1.5",
  "graphql": "^17.1.0",
  "graphql-jit": "^0.10.1"
},

Configure

Gratos reads its options from the grats key of a tsconfig.json: where to write the GraphQL schema (graphqlSchema) and the executable schema (tsSchema), and how the generated file imports your modules. The model samples on these pages each have one:

tsconfig.json
"include": ["*.ts"],
"grats": {
  "graphqlSchema": "./generated/schema.graphql",
  "tsSchema": "./generated/schema.ts",
  "importModuleSpecifierEnding": ".ts",
  "schemaHeader": null
}

Every option, its default included, is on the Configuration page.

Write a type

A class marked @gqlType is a GraphQL object type, each property or method marked @gqlField is one of its fields, and a function marked @gqlQueryField is a field of Query. Docblock text becomes the schema's descriptions. Here is a whole model, and the schema the CLI writes from it:

User.ts
import type { Int } from "gratos";

/** A person using the app. @gqlType */
export class User {
  /** @gqlField */
  readonly name: string;
  /** @gqlField */
  readonly age: Int;

  constructor(name: string, age: Int) {
    this.name = name;
    this.age = age;
  }

  /** Greets them by name. @gqlField */
  greeting(): string {
    return `Hello, ${this.name}!`;
  }
}

/** The signed-in user. @gqlQueryField */
export function viewer(): User {
  return new User("Ada", 36);
}

A field's type comes from its TypeScript type: string is String, and Int, Float and ID are type aliases you import from gratos. By default every field is nullable, as in Grats; with strictSemanticNullability on, a field that never returns null is marked @semanticNonNull instead (this site's own schema does that).

The same schema can come from functions instead of a class: a type alias for the data, and a derived field as an exported function whose first argument is its parent. Use whichever style suits your code, or both:

User.ts
import type { Int } from "gratos";

/** A person using the app. @gqlType */
export type User = {
  /** @gqlField */
  readonly name: string;
  /** @gqlField */
  readonly age: Int;
};

/** Greets them by name. @gqlField */
export function greeting(user: User): string {
  return `Hello, ${user.name}!`;
}

/** The signed-in user. @gqlQueryField */
export function viewer(): User {
  return { name: "Ada", age: 36 };
}

Generate the schema

Run gratos to write the schema, and gratos --check in CI: it writes nothing, and fails when a generated file is missing or out of date. --tsconfig names the config when it is not the nearest tsconfig.json. This site runs both from its package scripts:

package.json
"codegen": "gratos --tsconfig src/schema/tsconfig.json && node scripts/samples.ts",
"codegen:check": "gratos --check --tsconfig src/schema/tsconfig.json && node scripts/samples.ts --check",

The CLI writes two files: the schema as SDL, which clients and tools read, and schema.ts, which exports getSchema(), the executable schema with every resolver wired to your functions. You never edit either: a change to your models is a change to them on the next run.

Serve it

gratos-server is a GraphQL-over-HTTP server for Web Request and Response. createServer takes the executable schema and a list of plugins; when resolvers return Effects, effectSchema wraps the schema first, with the mapper from your typed failures to GraphQL errors. This site's server is a good first example:

src/graphql.ts
export const makeServer = (
  schema: GraphQLSchema,
  services: Context.Context<Services>,
  options: ServerOptions,
): Server => {
  const report =
    options.report ??
    ((error: GraphQLError) => {
      Effect.runForkWith(services)(Effect.logError("graphql error", error));
    });
  return createServer({
    schema: effectSchema(schema, { failure, isFailure: isAppError }),
    plugins: [
      incremental(),
      trustedDocuments({ documents: options.documents }),
      // `context`: resolvers find the app's services also when the server is used without a
      // route (makeServer(...).handle(request) in a test); a route forks with its own context.
      effect({ context: services }),
      reportErrors((error) => {
        report(error);
      }),
      maskErrors(),
    ],
  });
};

With Effect's HTTP platform, effectRouteLayer serves the server at /graphql, as a Layer with the shutdown drain built in:

src/host/app.ts
return Layer.mergeAll(
  effectRouteLayer(server),

The Effect pages cover the resolvers and the layer; the Server pages cover createServer, its plugins and what it answers over HTTP; and Relay shows how this site, a Relay app, reads a Gratos schema.

How it works

Gratos is a build step: it statically analyzes your TypeScript code, and generates a TypeScript module that defines an executable schema, and a .graphql schema file for clients and other tools.

To tell Gratos which parts of your code the schema exposes, annotate them with docblock tags like /** @gqlField */. Gratos finds these tags in your code and builds your schema from them.

Tip

Docblock tags generally correspond one to one with GraphQL schema constructs, so you should be able to add Gratos annotations to your code without learning a new DSL. If you guess wrong, Gratos tells you, with a helpful error message.

Sometimes Gratos asks you for a more explicit type annotation, so that it can "see" all the type information it needs.

For a deep dive into Gratos' implementation, see How Gratos works.

Next steps