GraphQL
Add GraphQL to a NestForge application and understand how resolvers interact with the framework container.
Feature activation
Section titled “Feature activation”Enable GraphQL through the graphql feature:
nestforge = { version = "1", features = ["graphql"] }Basic setup
Section titled “Basic setup”NestForge integrates with async-graphql and provides helpers that attach a schema to
the framework runtime:
use nestforge::{ async_graphql::{EmptyMutation, EmptySubscription, Object, Schema}, NestForgeFactory, NestForgeFactoryGraphQlExt,};
struct QueryRoot;
#[Object]impl QueryRoot { async fn health(&self) -> &str { "ok" }}
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish();
NestForgeFactory::<AppModule>::create()? .with_graphql(schema) .listen(3000) .await?;Default routes
Section titled “Default routes”The current helpers mount:
/graphql/graphiql
Custom paths
Section titled “Custom paths”Use GraphQlConfig and graphql_router_with_config(...) when you want custom routes.
Resolver access to providers
Section titled “Resolver access to providers”GraphQL requests receive the NestForge container, which means resolvers can resolve application providers from the GraphQL context:
use nestforge::{async_graphql::Context, resolve_graphql};
async fn app_name(&self, ctx: &Context<'_>) -> String { let config = resolve_graphql::<AppConfig>(ctx).expect("app config should exist"); config.app_name.clone()}Good fit
Section titled “Good fit”Choose the GraphQL path when:
- your consumers benefit from schema-driven queries
- your team already uses
async-graphql - you still want the same module and provider model as the HTTP layer