Pothos GraphQL Schema Builder

repository·main·Indexed 11 days ago

https://github.com/hayes/pothos

A plugin-based, strongly typed code-first schema builder for GraphQL and TypeScript. Pothos prioritizes type-safety and developer experience without requiring code generation. It features a modular architecture with plugins for Prisma, Relay, Apollo Federation 2, and smart subscriptions, and integrates with servers like Fastify, Nest.js, and Apollo Server.

Tokens
166.3K
Snippets
476
Records
618
Agent score
82%

What's inside Pothos

  1. Overview of the Dataloader Plugin

    main
    The Dataloader Plugin for Pothos provides a streamlined way to implement the DataLoader pattern within your GraphQL schema. It allows you to define fields and types that are automatically optimized using a dataloader, preventing the N+1 query problem by batching and caching requests for data.
  2. Overview of the Pothos GraphQL SDL Converter

    main
    The packages/converter tool is a utility designed to automate the generation of Pothos SchemaBuilder code from an existing GraphQL Schema Definition Language (SDL) file. Instead of manually writing Pothos type definitions, you can use this converter to bridge the gap between a raw GraphQL schema and the Pothos programmatic API.
  3. Explore Pothos Plugins

    main

    Pothos provides a wide range of plugins to extend its core functionality. These plugins allow you to integrate with existing GraphQL types, implement authorization, manage query complexity, optimize database queries (via Dataloader, Prisma, or Drizzle), and support specific protocols like Relay or Subscriptions.

    Available plugins include:

    • Add GraphQL: Add existing GraphQL types to your schema.
    • Auth: Add global, type level, or field level authorization checks.
    • Complexity: Define and limit the complexity of queries.
    • Dataloader: Define data-loaders to avoid N+1 query issues.
    • Directives: Integrate with existing schema GraphQL directives in a type-safe way.
    • Drizzle: Support efficient queries through Drizzle's relational query builder API.
    • Errors: Include error types in your schema and hook them up to resolvers.
    • Grafast: Use Grafast plans instead of resolvers.
    • Mocks: Add mock resolvers for testing.
    • Prisma: Efficient integration with Prisma to solve N+1 issues and optimize queries.
    • Relay: Define Relay-style nodes, connections, and cursor-based pagination.
    • Simple Objects: Define simple object types without manual resolvers or type definitions.
    • Smart Subscriptions: Make parts of your graph subscribable for live updates.
    • Sub-Graph: Build subsets of your graph to share code between internal and external APIs.
    • Tracing: Add resolver execution tracing (supports OpenTelemetry, New Relic, Sentry, etc.).
    • Validation: Use StandardSchemaV1 compatible libraries like Zod, Valibot, or ArkType.
    • With-Input: Define fields with inline input objects.
  4. Fullstack Next.js architecture with Pothos

    main

    This example demonstrates a fullstack GraphQL implementation using Next.js. The architecture is composed of the following layers:

    • Schema Construction: @pothos/core is used to define the GraphQL schema.
    • Server-side Execution: graphql-helix handles the execution of GraphQL queries triggered by incoming HTTP requests.
    • Frontend Client: @apollo/client is used to perform GraphQL queries from the Next.js client-side.
    • Type Generation: graphql-codegen/cli generates the schema.graphql file and TypeScript types for client-side queries, utilizing @boost/module to load the TypeScript schema files.
  5. Use the Pothos Prisma plugin

    main

    The Prisma plugin provides tight integration with Prisma, enabling efficient definition of GraphQL object types based on Prisma models and solving N+1 query issues through automatic query optimization.

    Key capabilities include:

    • Automatic Relationship Resolution: Automatically resolves database relationships.
    • Query Optimization: Automatically optimizes queries to load only the specific data needed, mitigating N+1 problems.
    • Relay Integration: Efficiently defines Relay nodes and connections using Prisma's cursor-based pagination.
    • Decoupled Schema: GraphQL types and fields are not strictly tied to database column names or types.
    • Multi-model Support: Allows defining multiple GraphQL models from a single database model.
    • Count Fields: Easy addition of count fields to objects and connections.
  6. Overview of the Pothos Relay plugin

    main
    The Relay plugin for Pothos provides builder methods and helper functions designed to simplify the creation of a GraphQL schema that is compatible with the Relay specification. It automates common Relay patterns such as Global IDs, Node interfaces, and Connection/Edge structures.
  7. Overview of the Errors plugin

    main
    The Errors plugin for Pothos allows you to easily include error types in your GraphQL schema and hook up these error types directly to your resolvers. This facilitates a pattern where errors are treated as first-class citizens in your schema, often used for Union types or specific error handling patterns.
  8. Understand how auth scopes are executed and cached

    main

    The Pothos Auth plugin uses different execution and caching strategies depending on where the scope is defined:

    • Scope Initializer: Runs once the first time a field protected by auth scopes is resolved. The result is cached for the current request.
    • authScopes functions on fields: Runs every time the field is resolved. This allows access to all arguments passed to the resolver.
    • authScopes functions on types: Runs once for each instance of that type in the response. It runs lazily when the first field for that object is resolved, and the result is cached and reused for all other fields on that same instance.
    • Scope loaders: Run whenever a field requires a scope with a unique parameter. Results are cached per request based on the combination of the scope name and its parameter.
    • grantScopes on a field: Runs after the field is resolved and is not cached.
    • grantScopes on a type (object or interface): Runs when the first field on the type is resolved. The result is cached and reused for each field of the same instance of the type.