A technical deep dive, for the curious, into how Gratos works under the hood. You don't need to read it to use Gratos. For a user's view of how Gratos works, see How it works in Getting started.

For a mental model of how Gratos is implemented, here is a high-level overview. For its values and aspirations, see Design principles; for the thinking behind its API, see Why use comments?

At build time

Extraction

To infer a GraphQL schema from your code, Gratos first finds your TypeScript config. From it, it asks the TypeScript compiler for every TypeScript file of your project, and for the options to pass to the compiler. Gratos runs on TypeScript 7's compiler API.

Gratos then checks each file with a regular expression to see whether it contains any @gql* tags. If it does, Gratos parses the file and visits every @gql* tag. For each tag that maps to a top-level GraphQL construct (a type, an interface, and so on), it asks the TypeScript compiler for the AST node the comment is attached to. With an AST node, and an expectation of which GraphQL construct to infer, it tries each of its inference strategies. If none infers a GraphQL construct, it reports a diagnostic. That sounds fancier than the code: a series of switch and if statements.

When it infers a GraphQL construct, it builds a GraphQL AST node for the schema definition. For @gqlType and the like, this means inspecting the node's children for child constructs, like @gqlField, recursively. These GraphQL AST nodes are the shape graphql-js builds when it parses a GraphQL SDL file, so they work with graphql-js' utilities. But a few clever tricks are played:

The location information of each GraphQL AST node, which would usually hold the line and column in the Schema Definition Language (SDL) text it was parsed from, holds the location of the TypeScript AST node instead: its file path, line and column.

With these AST nodes, Gratos validates the schema it inferred with the same code graphql-js uses to validate a GraphQL schema. And since their location information is the TypeScript node's, graphql-js' diagnostics "point" at the TypeScript source code that is the source of truth for that AST node. You can read more about this technique in this note.

One last trick: diagnostics use TypeScript's representation of a diagnostic, so TypeScript's error printer comes for free.

In a few cases, like a field whose name differs from its property or method's, Gratos records the fact by annotating AST objects with additional properties, which later phases read. Once every transformation is done, the remaining metadata is collected into a single Metadata object, which is passed to code generation. An experimental option, EXPERIMENTAL__emitMetadata, also writes it as a JSON file for other tools.

Effect return types

Where a resolver returns Effect.Effect<A, E, R>, Gratos reads the field's type from A, as it reads Promise<A>, and where a subscription returns Stream.Stream<A, E, R>, from A. The error and service types stay out of the schema. The metadata records which fields return Effects, so code generation can mark them (see Effect resolvers).

TypeScript code generation

With the GraphQL AST in hand, Gratos generates the TypeScript code that constructs your GraphQLSchema at runtime. It uses the AST to construct a GraphQLSchema object in memory. This normalized representation of the schema, with every extension merged and every type in a flat list, is passed to a code generator, which walks the schema recursively and generates TypeScript code for each type.

The resolve function of each field is synthesized from what the Metadata object holds about that field. Sometimes that means importing your resolver functions.

Code generation also follows the design principle of a few dependencies well leveraged: it uses TypeScript's AST construction utilities, then TypeScript's printer to write a formatted TypeScript file. Constructing a TypeScript AST, rather than concatenating strings, brings:

  1. type validation that the generated code is syntactically valid TypeScript;
  2. automatic formatting of the generated code.

GraphQL SDL code generation

The GraphQL SDL file is printed from the GraphQL AST, not from the GraphQLSchema object: printGratsSDL drops the definitions of the built-in scalars, prints the rest of the transformed and sorted document with graphql-js' print, and adds the file's header. Printing the AST keeps the directives applied in the schema, such as your directive annotations, which graphql-js' printSchema would drop.

At runtime

At runtime, the compiler is not involved at all. The generated TypeScript code is just a module that returns a GraphQLSchema object: it imports graphql and your modules, and no Gratos code (its only import from gratos is a type). It also avoids parsing GraphQL SDL at runtime, which can be a significant performance improvement, especially at the edge or in the browser.

A resolver that returns an Effect returns it as it is: effectSchema, from gratos-server/effect, runs it in the request's context (see The Effect layer).