GraphQL .Net Subscriptions EventStreamFieldType not Subscribing to IObserverable

489 views Asked by At

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.

1

There are 1 answers

0
Jeff Finn On BEST ANSWER

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.

  1. Use I Subject instead of creating observables. This was more convenient because it wraps up the required observable functionality.
  2. Set the buffer size to 1 in the ReplaySubject so messages were sent instantly.
  3. Upgraded to GraphQL.Net V5. This was related to this issue I created https://github.com/graphql-dotnet/graphql-dotnet/issues/2494
  4. Switch testing from GraphiQL to Altair. GraphiQL didn't seem to be able to receive subscriptions, I'm not sure why because it did trigger the subscription but messages were never received.

I have a sample project on GitHub https://github.com/graphql-dotnet/graphql-dotnet/issues/2494 that works