Docblock tags
Types
On this page
To define a GraphQL type, put a @gqlType docblock directly before a:
- class declaration;
- interface declaration;
- type alias of a literal type or
unknown.
If you model your GraphQL resolvers with classes, put a @gqlType docblock before the class that holds that type's resolvers:
/** @gqlType */
class MyClass {
/** @gqlField */
someField: string;
constructor(someField: string) {
this.someField = someField;
}
}type MyClass {
someField: String
}If your types are TypeScript interfaces, put a @gqlType docblock before the interface that describes the type. With @gqlType on an interface, Gratos treats it as a GraphQL type, not a GraphQL interface:
/** @gqlType */
export interface MyType {
/** @gqlField */
someField: string;
}type MyType {
someField: String
}Finally, if your types are type aliases, put a @gqlType docblock before the type alias that describes the type:
/** @gqlType */
export type MyType = {
/** @gqlField */
someField: string;
};type MyType {
someField: String
}Renaming types
To give your type a different name in GraphQL than in your code, write the name after the @gqlType docblock tag. See Renaming.
/** @gqlType User */
class UserModel {
/** @gqlField */
someField: string;
constructor(someField: string) {
this.someField = someField;
}
}type User {
someField: String
}Operation types
You rarely need to define the three operation types, Query, Mutation and Subscription, yourself: the root field tags add fields to them:
@gqlQueryField@gqlMutationField@gqlSubscriptionField
But if you do want to define one of these types explicitly (to add a description, say), it must be a type alias of unknown: type Query = unknown;. Any other definition is an error. The FAQ explains why: Why prohibit concrete root values?
/**
* # Welcome to GenericCorp's GraphQL Schema!
*
* This is root type of our system. Everything you need can be access from here.
* @gqlType
*/
type Query = unknown;
/** @gqlQueryField */
export function greet(): string {
return "Hello world";
}"""
# Welcome to GenericCorp's GraphQL Schema!
This is root type of our system. Everything you need can be access from here.
"""
type Query {
greet: String
}Implementing interfaces
Note
As in GraphQL's schema definition language, each type that implements an interface must define every field the interface requires, each with its own /** @gqlField */ tag. Gratos does not inherit an interface's fields. If you omit a field, or its type does not match, Gratos reports an error.
Classes
If you model your GraphQL resolvers with classes, declare that a type implements a GraphQL interface by declaring that its class implements an interface annotated with @gqlInterface:
/** @gqlInterface */
interface Person {
/** @gqlField */
name: string;
}
/** @gqlType */
export class User implements Person {
/** @gqlField */
name: string;
constructor(name: string) {
this.name = name;
}
}interface Person {
name: String
}
type User implements Person {
name: String
}TypeScript interfaces
If you model your GraphQL types with interfaces, declare that a type implements a GraphQL interface by declaring that its TypeScript interface extends an interface annotated with @gqlInterface:
/** @gqlInterface */
interface Person {
/** @gqlField */
name: string;
}
/** @gqlType */
export interface User extends Person {
__typename: "User";
/** @gqlField */
name: string;
}interface Person {
name: String
}
type User implements Person {
name: String
}Type aliases
A type declared with a type alias may not implement a GraphQL interface. Model such a GraphQL type with a TypeScript interface instead.
Info
See Interfaces for more about defining interfaces.
Note
Gratos must be able to determine the typename of every type that implements an interface. So it checks that every implementor either defines a __typename property, typed as a string literal (__typename: "MyType") or set to one (__typename = "MyType" as const), or is an exported class. It uses either one to determine the typename at runtime.