GraphQL
Building powerful GraphQL APIs with NestForge and async-graphql.
NestForge provides a first-class integration with async-graphql, the most
popular GraphQL library in the Rust ecosystem. This allows you to combine the
power of schema-first development with the dependency injection model used
throughout NestForge.
Feature activation
Section titled “Feature activation”First, enable the graphql feature in your Cargo.toml.
[dependencies]nestforge = { version = "1", features = ["graphql"] }Basic Setup
Section titled “Basic Setup”To add GraphQL to your NestForge application, build your async-graphql schema
and mount it during bootstrap using the with_graphql or
with_graphql_config extension.
The maintained GraphQL example uses GraphQlConfig:
use nestforge::{GraphQlConfig, NestForgeFactory, NestForgeFactoryGraphQlExt};
async fn bootstrap() -> anyhow::Result<()> { let factory = NestForgeFactory::<AppModule>::create()?; let config = factory.container().resolve::<AppConfig>()?; let schema = build_schema(config.app_name.clone());
factory .with_graphql_config(schema, GraphQlConfig::new("/graphql").with_graphiql("/")) .listen(3001) .await}By default, NestForge mounts:
POST /graphql: The GraphQL API endpoint.GET /graphiql: The interactive playground.
Dependency Injection in Resolvers
Section titled “Dependency Injection in Resolvers”Every GraphQL request carries the NestForge Container. This means your resolvers can access services registered in your modules.
Use the resolve_graphql helper inside your resolver methods:
use nestforge::{async_graphql::Context, resolve_graphql};use crate::users::services::UsersService;
#[Object]impl QueryRoot { async fn users(&self, ctx: &Context<'_>) -> Vec<UserDto> { let users_service = resolve_graphql::<UsersService>(ctx) .expect("UsersService should be registered");
users_service.find_all().await.unwrap_or_default() }}Custom Configuration
Section titled “Custom Configuration”If you need to change the default paths or request limits, use GraphQlConfig:
use nestforge::GraphQlConfig;
NestForgeFactory::<AppModule>::create()? .with_graphql_config( schema, GraphQlConfig::new("/graphql") .with_graphiql("/play") .with_max_request_bytes(2 * 1024 * 1024) ) .listen(3000) .await?;Modularity
Section titled “Modularity”Just like the rest of NestForge, we recommend organizing your GraphQL logic into Feature Modules.
#[module( providers = [UsersService, UsersResolver], exports = [UsersService])]pub struct UsersModule;While NestForge does not auto-discover resolvers, the modular patterns keep your DI clean and manageable as your schema grows.