Overview

Build fully type-safe GraphQL schemas in TypeScript, without writing your types twice.

Pothos

Pothos is a library for building GraphQL schemas in TypeScript. You define your types, fields, and resolvers with a builder, and the whole schema is fully type-safe without writing your types twice. The builder keeps track of your data's types wherever they are used, so there is no SDL to keep in sync and no code-generation step.

builder.toSchema() hands you a standard graphql-js GraphQLSchema at startup, so any server can run it, whether that's GraphQL Yoga, Apollo, or another implementation. Pothos does its work in the type system and while the schema builds; your resolvers stay ordinary functions. Core depends only on graphql, and everything past it (Relay, scope-auth, errors, validation, dataloaders, Prisma, Drizzle, federation) is a plugin that extends the same builder.

import SchemaBuilder from '@pothos/core';

const builder = new SchemaBuilder({});

// Character is backed by the TypeScript shape in the generic
const Character = builder.objectRef<{ id: string; name: string }>('Character');

Character.implement({
  fields: (t) => ({
    id: t.exposeID('id'),
    name: t.exposeString('name'),
  }),
});

builder.queryType({
  fields: (t) => ({
    frodo: t.field({
      type: Character,
      resolve: () => ({ id: '1', name: 'Frodo Baggins' }),
    }),
  }),
});

export const schema = builder.toSchema();

Open the example in the playground to run it, edit the resolver, and watch the SDL update. The Introduction has the full narrative: how Pothos compares to schema-first tools and the mental model the guide builds on.

How it fits together

The example above is the whole pipeline in miniature:

  • The backing model ({ id: string; name: string }) is a plain TypeScript type describing the data. builder.objectRef<T>() creates a GraphQL type based on it.
  • The frodo resolver needs to return that shape, and TypeScript will report an error if it doesn't.
  • builder.toSchema() produces a normal graphql-js GraphQLSchema that works with any GraphQL server.

Plugins add methods to the same builder (t.connection, authScopes, t.prismaField) with the same type safety as the built-in ones.

Explore the docs