I'm trying to set up subscriptions on my GraphQL server using GraphQL .Net. I have followed the documentation in the QraphQL.Net docs and the PluralSight course by Roland Guijt and looked online for more information.
What I have looks like it should work. I have my Subscribe method which returns the IObserverable to the event stream field. I have also tried using a function to pass the IObserverable and it still doesn't work.
This is my subscription
public class MyObjectSupscription : ObjectGraphType
{
private readonly MyObjectObservable _myObjectObservable;
private readonly MyObjectSubjectHelper _myObjectSubjectHelper;
public MyObjectSupscription(SportsEventObservable myObjectObservable, SportsEventSubjectHelper myObjectSubjectHelper)
{
Name = "Subscription";
_sportsEventObservable = myObjectObservable;
_sportsEventSubjectHelper = myObjectSubjectHelper;
AddField(new EventStreamFieldType
{
Name = "MyObjectAdded",
Type = typeof(MyObjectType),
Resolver = new FuncFieldResolver<MyObject>(ResolveMessage),
Subscriber = new EventStreamResolver<MyObject>(Subscribe),
});
}
private IObservable<SportsEvent> Subscribe(IResolveEventStreamContext context)
{
return _myObjectObservable;
// return _myObjectSubjectHelper.MyObjects(); //This uses a replaySubject as an observerable
}
private MyObject ResolveMessage(IResolveFieldContext context)
{
return context.Source as MyObject;
}
When I trace the code it and put a breakpoint in the GraphQL .Net package it comes into the EventStream subscriber
public class EventStreamResolver<T> : IEventStreamResolver<T>
{
private readonly Func<IResolveEventStreamContext, IObservable<T>> _subscriber;
public EventStreamResolver(
Func<IResolveEventStreamContext, IObservable<T>> subscriber)
{
_subscriber = subscriber ?? throw new ArgumentNullException(nameof(subscriber));
}
public IObservable<T> Subscribe(IResolveEventStreamContext context) => _subscriber(context);
IObservable<object> IEventStreamResolver.Subscribe(IResolveEventStreamContext context) => (IObservable<object>)Subscribe(context);
}
Which looks like it should call the Subscribe method in my IObserverable, but the breakpoint I put in here isn't reached.
My Observerable is registered with AutoFac as a Singleton and my GraphQL Mutations and Querys work, so it looks like GraphQL is set up correctly and working. The notify method in my observable is called after a MyObject is added to the database.
I looked over my commits to get this working and I think there were a few changes that I had to make to get Subscriptions to work.
I have a sample project on GitHub https://github.com/graphql-dotnet/graphql-dotnet/issues/2494 that works