Skip to content

GraphQL

Add GraphQL to a NestForge application and understand how resolvers interact with the framework container.

Enable GraphQL through the graphql feature:

nestforge = { version = "1", features = ["graphql"] }

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?;

The current helpers mount:

  • /graphql
  • /graphiql

Use GraphQlConfig and graphql_router_with_config(...) when you want custom routes.

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()
}

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