Resolvers
Generics
On this page
This page is the reference for how generics behave in Gratos. For how to think about generics, and practical examples of their use, see the Generics guide.
Generally, the types Gratos reads as field types and union members must be scalars, or references to declarations explicitly marked with an @gql* docblock tag. But sometimes it is useful to use types in a more flexible, "generic" way.
So the types of fields and the members of unions may also reference one more kind of declaration: the type parameters of the parent declaration itself.
When a @gql* type declaration references its type parameters in positions that Gratos reads as GraphQL types, the declaration becomes a generic template. The declaration is not itself added to the schema: it is a template. Wherever the TypeScript type is used in a GraphQL position, its type arguments are filled into the template, and the resulting type is added to the schema.
Examples
Two templates, an Edge<T> type and a Result<T> union, each used once:
/** @gqlType */
type Edge<T> = {
/** @gqlField */
node: T;
/** @gqlField */
cursor: string;
};
/** @gqlUnion */
type Result<T> = T | GqlError;
/** @gqlType */
type GqlError = {
__typename: "GqlError";
/** @gqlField */
message: string;
};
/** @gqlType */
type Upload = {
__typename: "Upload";
/** @gqlField */
url: string;
};
/** @gqlQueryField */
export function lastUpload(): Result<Upload> {
return { __typename: "Upload", url: "https://example.com/cat.png" };
}
/** @gqlQueryField */
export function firstUpload(): Edge<Upload> {
return {
node: { __typename: "Upload", url: "https://example.com/dog.png" },
cursor: "0",
};
}union UploadResult = GqlError | Upload
type GqlError {
message: String
}
type Query {
firstUpload: UploadEdge
lastUpload: UploadResult
}
type Upload {
url: String
}
type UploadEdge {
cursor: String
node: Upload
}Type parameters
A @gql* declaration may use type parameters freely, and by default Gratos ignores them. But if one of those type parameters is used in a position that Gratos reads as a GraphQL type, the declaration is a generic template.
When the generic type is then itself used in a position that Gratos reads as a GraphQL type, the type parameters used in GraphQL positions must be passed as type arguments, and those must themselves be @gql* types (or type parameters of the containing declaration).
Naming
The name of a materialized generic type is the names of its GraphQL type arguments (in order), followed by the name of the generic template.
For example:
Result<Upload>isUploadResultConnection<User>isUserConnectionReview<User, Product>isUserProductReview
Each name is added to the schema once, even if it is used in many places.
Note
Each materialized type inherits the description and the @deprecated marking of its generic type.
Nesting
Generics nest in two ways. A type parameter of a declaration can be passed as a type argument to another generic type, as Connection<T> passes its T to Edge<T> below. And a generic type can be passed a type argument that is itself a generic type with its own type arguments, as Counted<Connection<Review>> is:
import type { Int } from "gratos";
/** @gqlType */
type Connection<T> = {
/** @gqlField */
edges: Edge<T>[];
/** @gqlField */
pageInfo: PageInfo;
};
/** @gqlType */
type Edge<T> = {
/** @gqlField */
node: T;
/** @gqlField */
cursor: string;
};
/** @gqlType */
type PageInfo = {
/** @gqlField */
hasNextPage: boolean;
};
/** @gqlType */
type Counted<T> = {
/** @gqlField */
items: T;
/** @gqlField */
total: Int;
};
/** @gqlType */
type Review = {
/** @gqlField */
body: string;
};
/** @gqlType */
type User = {
/** @gqlField */
reviews: Counted<Connection<Review>>;
};type PageInfo {
hasNextPage: Boolean
}
type Review {
body: String
}
type ReviewConnection {
edges: [ReviewEdge!]
pageInfo: PageInfo
}
type ReviewConnectionCounted {
items: ReviewConnection
total: Int
}
type ReviewEdge {
cursor: String
node: Review
}
type User {
reviews: ReviewConnectionCounted
}Limitations
There are some limitations to how generics can be used:
Only unadorned type names may be passed as GraphQL type parameters
A type argument must name a GraphQL type: an object type, a union or a scalar, say, including a @gqlScalar of your own and Gratos' Int, Float and ID. Gratos derives each materialized type's name from it: Wrapper<Custom> becomes CustomWrapper, and Wrapper<Int> becomes IntWrapper. A TypeScript primitive (string, number, boolean), an array (User[]) or an inline union (User | Post) names no GraphQL type, so passing one is an error.
Parameterized types may not implement interfaces or be members of unions
For a type to be part of an abstract type, the executor must be able to determine its concrete type at runtime. Gratos enforces this by requiring every type that is part of an abstract type either to define a __typename property, typed as or set to its name as a string literal, or to be an exported class (see Unions). A parameterized type is one declaration for several GraphQL types, each with its own name, so it can do neither: it may not implement an interface or be a member of a union. Result<Connection<Review>>, a union with a generic member, is an error.