Skip to main content
nestrs provides two optional protocol extensions: the graphql feature mounts a GraphQL endpoint on the same Axum router as your REST controllers, and the ws feature adds WebSocket gateways with the same #[injectable] provider injection and guard/pipe/interceptor cross-cutting you use everywhere else.

GraphQL

Setup

Add the graphql feature to your nestrs dependency and add async-graphql and nestrs-graphql:
The nestrs-graphql crate is re-exported as nestrs::graphql when the feature is enabled.

Defining a schema

Define your query and mutation types using async-graphql attributes and build a Schema. Then pass it to enable_graphql:
This mounts GET /graphql (GraphQL Playground) and POST /graphql (query endpoint) on the same router as your REST routes. The global prefix and URI versioning settings apply to the GraphQL path the same way they apply to REST controllers.

Custom path and options

Use enable_graphql_with_path to change the mount point:
Use enable_graphql_with_options to control Playground availability and other HTTP-surface settings:

Mutations and resolvers

Federation, schema stitching, and codegen are outside the nestrs core. Use async-graphql’s federation support and Apollo Router or GraphOS to compose subgraphs. nestrs exposes a single /graphql endpoint — treat it as one subgraph in your platform.
For DataLoader / N+1 prevention, use async-graphql’s built-in dataloader feature or application-level batching in resolvers. The ecosystem is the same as a standalone async-graphql server.

WebSockets

Setup

Add the ws feature:
nestrs-ws is re-exported as nestrs::ws when the feature is enabled.

Wire format

WebSocket frames are JSON objects with the shape { "event": "name", "data": <json> }. The server sends the same format back to clients. Unknown frames and error conditions are sent to the client on the special "error" event name (nestrs::ws::WS_ERROR_EVENT).

Defining a gateway

Use #[ws_gateway] on a struct and #[ws_routes] on its impl block. Individual message handlers are annotated with #[subscribe_message("event-name")]:

Emitting to a client

WsClient::emit serializes a value and sends it as a JSON frame:
WsClient::emit_json sends a pre-built serde_json::Value directly.

Guards, pipes, and interceptors

Apply cross-cutting to WebSocket handlers with the ws-specific attributes:
WebSocket JSON frames do not flow through NestApplication::use_global_exception_filter or HttpException. Guard and pipe failures are sent to the client on the "error" event with statusCode, message, and error fields. There is no separate WsExceptionFilter trait in core today — centralize error handling by wrapping WsGateway::on_message or using shared guard/pipe types.

Error frame shapes

All errors are delivered on WS_ERROR_EVENT ("error").