This guide explains the principles of generic types, with practical examples of their use. For the reference of how Gratos supports generics, see Generics.

Generic types are types parameterized by other types, which makes them reusable. In GraphQL, the list type can be thought of as a generic type: you can make a list of any type T without defining a new named type.

But GraphQL does not support user-defined generic types.

Gratos derives your GraphQL schema, so it can support generic types: types, interfaces, unions and input types can all be generic. When you use a generic type with a type argument, Gratos materializes a concrete type for it in the generated schema.

Example 1: Result type

A common pain point: a field that you know will fail sometimes, and you want your consumers to handle that case by modeling it in your schema.

Done by hand, it looks like this:

MyValueResult.ts
/** @gqlUnion */
export type MyValueResult = MyValue | MyError;

/** @gqlQueryField */
export function myValue(): MyValueResult {
  try {
    return getMyValue();
  } catch (e) {
    const message = e instanceof Error ? e.message : String(e);
    return { message, __typename: "MyError" };
  }
}

/** @gqlType */
export type MyValue = {
  __typename: "MyValue";
  /** @gqlField */
  value: string;
};

/** @gqlType */
export type MyError = {
  __typename: "MyError";
  /** @gqlField */
  message: string;
};

function getMyValue(): MyValue {
  return { value: "Hello, World!", __typename: "MyValue" };
}

But with many fields like this, we would define a *Result type by hand for every type of value we return. A reusable, generic Result<T> type makes this simpler:

Result.ts
/** @gqlUnion */
export type Result<T> = T | MyError;

/** @gqlQueryField */
export function myValue(): Result<MyValue> {
  try {
    return getMyValue();
  } catch (e) {
    const message = e instanceof Error ? e.message : String(e);
    return { message, __typename: "MyError" };
  }
}

/** @gqlQueryField */
export function myOtherValue(): Result<MyOtherValue> {
  try {
    return getMyOtherValue();
  } catch (e) {
    const message = e instanceof Error ? e.message : String(e);
    return { message, __typename: "MyError" };
  }
}

/** @gqlType */
export type MyValue = {
  __typename: "MyValue";
  /** @gqlField */
  value: string;
};

/** @gqlType */
export type MyOtherValue = {
  __typename: "MyOtherValue";
  /** @gqlField */
  value: string;
};

/** @gqlType */
export type MyError = {
  __typename: "MyError";
  /** @gqlField */
  message: string;
};

function getMyValue(): MyValue {
  return { value: "Hello, World!", __typename: "MyValue" };
}

function getMyOtherValue(): MyOtherValue {
  return { value: "Hello, World!", __typename: "MyOtherValue" };
}

The schema beside each example shows the types Gratos generates from it.

Example 2: Connection type

Another common GraphQL idiom that suits generics is the Connection type, a paginated list of items. GraphQL APIs often use it for lists too large to return in a single response.

The Connection spec guide shows a spec-compliant Connection type defined with generics.

Further reading

To read more about generics in TypeScript, see Generics in the TypeScript Handbook.

Strawberry, an excellent implementation-first GraphQL library for Python, supports deriving schema from generics. It was a significant inspiration for this implementation.