GraphQL Subscriptions
Relevant source files
The following files were used as context for generating this wiki page:
GraphQL Subscriptions in nestjs-query enable real-time updates by allowing clients to listen for changes in data. The system utilizes a publish-subscribe (PubSub) mechanism to broadcast events whenever specific mutations occur, such as creating, updating, or deleting entities.
This feature integrates with the core GraphQL Resolvers to automatically provide subscription endpoints for DTOs. It supports various transport layers through a flexible interface, ensuring that real-time notifications reach connected clients efficiently.
Sources: packages/query-graphql/README.md, documentation/sidebars.js:81
Architecture and Components
The subscription system relies on two primary pillars: a standardized PubSub interface and specialized decorators that link resolvers to event streams.
PubSub Interface
The PubSub interface defines the contract for any event broadcasting engine used within the framework. It abstracts the underlying implementation, allowing developers to switch between local EventEmitter based systems or distributed systems like Redis.
The diagram shows the required methods for a PubSub implementation: publish for sending events and asyncIterator for receiving them.
Sources: packages/query-graphql/src/subscription/pub-sub.interface.ts:1-7
Resolver Subscription Decorator
The ResolverSubscription decorator configures how a resolver method handles incoming subscription requests. It allows developers to specify the return type and options for the subscription field in the GraphQL schema.
| Property | Description |
|---|---|
typeFunc | A function returning the GraphQL type for the subscription payload. |
options | Configuration for the subscription field (e.g., name, description). |
Sources: packages/query-graphql/src/decorators/resolver-subscription.decorator.ts:6-12
Data Flow
When a subscription is established, the system creates an AsyncIterator that waits for events published to specific triggers.
This flow illustrates the cycle from client connection to data delivery via the PubSub mechanism.
Sources: packages/query-graphql/src/subscription/pub-sub.interface.ts:6, packages/query-graphql/src/decorators/resolver-subscription.decorator.ts:12
Implementation Example
In a NestJS application, subscriptions are enabled by configuring the GraphQLModule with a subscription-capable server (like Apollo) and providing a PubSub implementation.
// examples/subscriptions/src/app.module.ts
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
installSubscriptionHandlers: true,
autoSchemaFile: 'schema.gql',
}),
],
})
export class AppModule {}
Setting installSubscriptionHandlers: true enables the necessary WebSocket infrastructure for GraphQL subscriptions.
Sources: examples/subscriptions/src/app.module.ts:1-15
Event Handling Logic
The logic for subscriptions in nestjs-query is typically automated for CRUD operations.
- Trigger Identification: Every DTO/Entity pair has unique trigger names for
created,updated, anddeletedevents. - Event Publishing: Services call
PubSub.publishafter successful database operations. - Payload Filtering: Subscriptions can include filters to ensure clients only receive updates for entities they are interested in.
Sources: packages/query-graphql/src/subscription/pub-sub.interface.ts:5, packages/query-graphql/README.md
Summary
GraphQL Subscriptions provide a robust way to implement real-time features in nestjs-query applications. By adhering to the PubSub interface and using provided decorators, the system ensures that data consistency is maintained across all connected clients whenever mutations occur. This module is a critical part of the query-graphql package, serving to complete the CRUD lifecycle with real-time feedback.