FAQ
Why use comments?
On this page
Depending on the capabilities of the target language, there are several ways to implement an implementation-first GraphQL server like Gratos. Some libraries use introspection, others use macros. So why does Gratos use comments instead? Let's explore why these other approaches are not a good fit for TypeScript.
Why not use decorators?
Other tools, like Strawberry for Python, use decorators to annotate types and fields. This is probably the ideal API for an implementation-first GraphQL server: it uses existing language syntax and, with introspection, can derive the GraphQL schema at runtime, without an additional build step.
But there are a few reasons it is not feasible in TypeScript. First, decorators are runtime constructs, so they cannot be attached to types, which would rule out defining GraphQL input and output types with TypeScript types.
Second, TypeScript types are stripped at runtime, so introspection fundamentally cannot "see" the types of fields, arguments and so on. Introspection cannot derive a GraphQL schema from TypeScript types at runtime: deriving GraphQL types from type annotations must be done statically, at build time.
Introspection can still derive pieces of a GraphQL schema: the names of decorated types or classes, and field names, for example. TypeGraphQL is a library similar to Gratos in many ways, which uses decorators and introspection instead of comments. Since types are not visible at runtime, it falls back to a builder-like API for field return types and argument types. That avoids a build step, at the cost of a more complex API.
Gratos optimizes for the simplest possible API.
Why not use macros?
TypeScript does not support macros, and seems unlikely to add them.
Then why comments?
With these limitations, Gratos falls back to a lowest common denominator: static analysis. It infers GraphQL types from TypeScript at build time, by analyzing the code rather than running it.
Docblocks convey that the code inside them does not affect the runtime, while having a well-defined structure that can be parsed. In particular, Gratos uses the TypeScript compiler's JSDoc parsing, in line with the design principle "A few dependencies well leveraged".
Summary
Ideally, introspection or macros would derive the GraphQL schema: both are very nice approaches to implementation-first GraphQL. But TypeScript's limitations rule them out. So Gratos infers a GraphQL schema from TypeScript types by static analysis, and docblocks strike a nice balance: structured enough to be machine readable, while clearly not part of the runtime.