graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Trouble getting CreateSubscriptionStream to work

Open eMarkM opened this issue 3 years ago • 4 comments
trafficstars

Trying to follow the documentation to get subscriptions to work. The documentation on the Readme doesn't mention InitializeWebsocketConnection, but I saw that in some of the other code linked in similar issues here. But when I run that, I get WebSocketException : The server returned status code '400' when status code '101' was expected. Not sure how to configure this correctly. Something on the server side I'd need to do? I know the subscription code works ok.


[Fact]
        public async Task TestIt()
        {
            var client = new GraphQLHttpClient("http://localhost:4000/graphql/", new NewtonsoftJsonSerializer());
            await client.InitializeWebsocketConnection();

            IObservable<GraphQLResponse<Types.Subscription>> observableStream = client.CreateSubscriptionStream<Types.Subscription>(SubscriptionGQL.Request());
            
            var observer = new GraphQlObserver();

            using var subscription = observableStream.Subscribe(observer);
            
            subscription.Dispose();
        }

Observer def:

public class GraphQlObserver : IObserver<GraphQLResponse<Types.Subscription>>
    {
        private readonly List<int?> _ids;

        public bool IsComplete { get; private set; }

        public GraphQlObserver()
        {
            _ids = new List<int?>();
        }
        public void OnCompleted()
        {
            IsComplete = true;
        }

        public void OnError(Exception error)
        {
            throw new NotImplementedException();
        }

        public void OnNext(GraphQLResponse<Types.Subscription> value)
        {
            _ids.AddRange(value.Data.streamDocumentIds);
        }
    }

GraphQLRequest

public class SubscriptionGQL
    {
        public static GraphQLRequest Request()
        {
            return new GraphQLRequest
            {
                Query = SubscriptionDocument,
                OperationName = "Subscription"
            };
        }

        public static string SubscriptionDocument = @"
        subscription Subscription {
          streamDocumentIds
        }
        ";

    }

Subscription class

public class Subscription
{         
       [JsonProperty("streamDocumentIds")]
       public List<int?> streamDocumentIds { get; set; }
}

eMarkM avatar Apr 28 '22 21:04 eMarkM

Something on the server side I'd need to do?

Maybe the server tells you why it doesn't like your request (status code 400).

Which websocket subprotocol does your server use? This client currently only speaks graphql-ws.

InitializeWebsocketConnection() is just a way to force opening the websocket connection independently from an request. It is invoked internally on the first request that uses the websocket.

rose-a avatar Apr 28 '22 22:04 rose-a

Thanks for the quick response! So am I otherwise using it correctly in this code? If we solve the websocket subprotocol issue then when .Subscribe is called I should start seeing calls to OnNext in the Observer?

And yes, now I can step through the source code and see the same error being thrown in GraphQLHttpWebSocket.ConnectAsync when calling Subscribe

eMarkM avatar Apr 29 '22 15:04 eMarkM

Ok, coming back to this. Still have some issues. We had the vendor that was supplying the GraphQl server switch to graphql-ws. I no longer get the 400 error I was getting. However, still having issues. Same code as above. When it calls observableStream.Subscribe(observer) it eventually calls GraphQLHttpWebSocket.ConnectAsync which calls _clientWebSocket.ConnectAsync. And it just...dies and kills the whole process. No exception, just kills it. When this same code is called by directly calling InitializeWebsocketConnection(), it gets past this ok.

eMarkM avatar Jun 02 '22 21:06 eMarkM

Have you tried to subscribe to GraphQLHttpClient.WebSocketReceiveErrors? Maybe the problem surfaces there...

rose-a avatar Jun 05 '22 19:06 rose-a