Pothos GraphQL Schema Builder
repository·main·Indexed 11 days ago
https://github.com/hayes/pothosA 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.
What's inside Pothos
- This example demonstrates how to build a GraphQL API using Prisma that supports subscriptions for basic CRUD mutations. It showcases the integration between Pothos, Prisma, and a GraphQL server to handle real-time updates when data is created, updated, or deleted.
Overview of the Dataloader Plugin
mainThe 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.Overview of the Pothos GraphQL SDL Converter
mainThepackages/convertertool is a utility designed to automate the generation of PothosSchemaBuildercode 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.Overview of the Pothos Errors Plugin
mainThe Errors plugin for Pothos allows you to easily include error types in your GraphQL schema and hook them up to your resolvers. This enables a more structured way of handling and returning errors as part of your GraphQL response types.Overview of the Prisma Smart Subscriptions Example
mainThis example demonstrates how to build a GraphQL API using Prisma that automatically creates subscriptions based on database queries. It showcases the integration between Pothos core, the Prisma plugin, and the Smart Subscriptions plugin to handle basic CRUD mutations and real-time updates.Explore Pothos Plugins
mainPothos 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.
Fullstack Next.js architecture with Pothos
mainThis example demonstrates a fullstack GraphQL implementation using Next.js. The architecture is composed of the following layers:
- Schema Construction:
@pothos/coreis used to define the GraphQL schema. - Server-side Execution:
graphql-helixhandles the execution of GraphQL queries triggered by incoming HTTP requests. - Frontend Client:
@apollo/clientis used to perform GraphQL queries from the Next.js client-side. - Type Generation:
graphql-codegen/cligenerates theschema.graphqlfile and TypeScript types for client-side queries, utilizing@boost/moduleto load the TypeScript schema files.
- Schema Construction:
Use the Relay plugin to build Relay-compatible schemas
mainThe Relay plugin provides builder methods and helper functions to simplify the creation of a GraphQL schema that adheres to Relay specifications. It automates common Relay patterns such as Global IDs, Node interfaces, and Connection/Edge structures.Use the Pothos Prisma plugin
mainThe 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.
Overview of the Pothos Relay plugin
mainThe 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.Overview of the Errors plugin
mainThe 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.Understand how auth scopes are executed and cached
mainThe 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.
authScopesfunctions on fields: Runs every time the field is resolved. This allows access to all arguments passed to the resolver.authScopesfunctions 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.
grantScopeson a field: Runs after the field is resolved and is not cached.grantScopeson 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.