Implementing GraphQL subscriptions
GraphQL subscriptions, by default, work over WebSockets but can also work over Server-Sent Events (SSE), SignalR, or even gRPC.
Imagine that a client app wants to be notified when a product has its unit price reduced. It would be great if the client could subscribe to an event that gets triggered whenever a unit price is reduced, instead of having to query for changes to the unit prices.
Adding a subscription and topic to the GraphQL service
Let’s add this feature to our GraphQL service using subscriptions:
- Add a new class file named
ProductDiscount.cs
. - Modify the contents to define a model to notify a client about a product’s unit price reduction, as shown in the following code:
namespace Northwind.GraphQL.Service; public class ProductDiscount { public int? ProductId { get; set; } public decimal? OriginalUnitPrice { get; set; } public decimal? NewUnitPrice { get; set; } }
- Add...