Getting started
Comment syntax
On this page
Gratos uses JSDoc docblock comments to mark the declarations of your code that your GraphQL schema exposes. Annotating code with comments is unusual, and can be confusing at first. This page explains how to use docblocks correctly, but Gratos also aims to help: it warns you when it finds a docblock it doesn't understand, or one used improperly.
Info
To learn why Gratos uses docblock comments, see Why use comments?
In broad strokes, Gratos looks for docblocks with special @gql tags, and uses each docblock's position to infer the declaration it applies to.
JSDoc
Gratos looks for tags in JSDoc docblocks only. JSDoc is a standard syntax for annotating JavaScript code with metadata, used by many tools, TypeScript included.
A JSDoc docblock is a block comment that begins with /**.
Each comment must start with a
/**sequence in order to be recognized by the JSDoc parser. Comments beginning with/*,/***, or more than 3 stars will be ignored. This is a feature to allow you to suppress parsing of comment blocks.
So Gratos does not recognize the comments // @gqlType or /* @gqlType */, but it reports an error for them.
Info
Gratos does not support non-JSDoc comments because it relies on TypeScript to determine which declaration a comment applies to, and TypeScript only recognizes JSDoc comments. See Attachment below.
Tag syntax
A tag in a JSDoc comment is an @ followed by a name. A value may follow it, which may span several lines. A tag may begin its own line, as is usual in a docblock of several lines:
/**
* A description of my enum.
* @gqlEnum
*/Or it may follow the description on the same line, as in this one-line docblock from Getting started:
/** A person using the app. @gqlType */Any free text in a Gratos docblock is the declaration's description.
Note
Because a tag's value may span several lines, put the description at the top of the docblock, before any tag.
See Descriptions for more about descriptions.
Attachment
Gratos uses the TypeScript compiler to determine "comment attachment": which declaration a comment refers to. Comment attachment is a surprisingly complex problem, with edge cases that don't always feel intuitive. But in general, the TypeScript compiler attaches a comment to the declaration that directly follows it.
When Gratos finds a @gql comment that does not seem to be attached to any declaration, it reports an error. Likewise when a @gql comment is attached to a declaration that its tag cannot apply to.