Skip to content

Conversation

@rose-a
Copy link
Collaborator

@rose-a rose-a commented Nov 7, 2018

Relating to #25

@deinok Here is my first stab at creating a subscription "API" based on System.Reactive.

I've left the existing API untouched and implemented my idea alongside it. I've also added a simple integration test based on the chat sample to prove it working.

Currently the new variant could be used as follows:

var graphQLRequest = new GraphQLRequest
{
    Query = @"
    subscription {
        messageAdded{
        content
        }
    }"
};

// create the desired subscription stream. 
// No connection is established yet.
IObservable<GraphQLResponse> subscriptionStream = client.CreateSubscriptionStream(graphQLRequest);

// subscribe to the stream. When the first subscription is made, 
// the web socket connection for the stream is initiated
IDisposable subscription = subscriptionStream.Subscribe(response => processUpdate(response));

// when you are done with the subscription, dispose of it. 
// Disposing the last subscription will dispose the web socket and all related resources.
subscription.Dispose();

Disposing the parent GraphQlHttpClient will cancel and dispose all web sockets made on it, which will result in the subscribers receiving an OnError() call with a ThreadCancelledException.

This should satisfy the requirements you mentioned in #25.

Please let me know what you think.

@deinok
Copy link
Member

deinok commented Nov 7, 2018

Looks pretty good. I will take a closer look in the following days.

@deinok
Copy link
Member

deinok commented Nov 7, 2018

And about the integration tests, probably we are going to move to use our own integrated server and not something exposed to internet

@rose-a
Copy link
Collaborator Author

rose-a commented Nov 7, 2018

The integration tests are already using the "IntegrationTestServer" in the tests subdirectory (since I did not find any public demo APIs which support subscriptions).

So basically this server project can also be reused for all other tests which currently run against a public API.

For everything NOT using websockets we can do this the way pointed out in this Microsoft tutorial

With ClientWebSocket the WebApplicationFactory doesn't seem to work, so we have to create the test server the "manual" way.

@deinok deinok self-requested a review November 15, 2018 19:40
@deinok deinok changed the base branch from master to subscriptions-api November 20, 2018 21:12
@deinok deinok merged commit 041f327 into graphql-dotnet:subscriptions-api Nov 20, 2018
@deinok
Copy link
Member

deinok commented Nov 20, 2018

@rose-a Very well done.
It need a little bit more reviewing but Reactive Extensions seems the way to go.
I temporaly move it to subscriptions-api while we ensure that API is "perfect".

Many thanks. ( Paypal and I will give you the beer I promised ;) )

@rose-a rose-a deleted the observable-subscriptions branch November 21, 2018 06:42
@curbol
Copy link

curbol commented May 7, 2019

Can you create a new alpha with this included? I would like to use it.

@akoliya003
Copy link

Relating to #25

@deinok Here is my first stab at creating a subscription "API" based on System.Reactive.

I've left the existing API untouched and implemented my idea alongside it. I've also added a simple integration test based on the chat sample to prove it working.

Currently the new variant could be used as follows:

var graphQLRequest = new GraphQLRequest
{
    Query = @"
    subscription {
        messageAdded{
        content
        }
    }"
};

// create the desired subscription stream. 
// No connection is established yet.
IObservable<GraphQLResponse> subscriptionStream = client.CreateSubscriptionStream(graphQLRequest);

// subscribe to the stream. When the first subscription is made, 
// the web socket connection for the stream is initiated
IDisposable subscription = subscriptionStream.Subscribe(response => processUpdate(response));

// when you are done with the subscription, dispose of it. 
// Disposing the last subscription will dispose the web socket and all related resources.
subscription.Dispose();

Disposing the parent GraphQlHttpClient will cancel and dispose all web sockets made on it, which will result in the subscribers receiving an OnError() call with a ThreadCancelledException.

This should satisfy the requirements you mentioned in #25.

Please let me know what you think.

Hi, it seems like CreateSubscriptionStream is not a member function of GraphQL.Client.Http.GraphQLHttpClient

I am using GraphQL (3.0.0-preview-1271) , GraphQL.Client(2.0.0-alpha.3) , GraphQL.Common (2.0.0-alpha.3).

Could you please send me sample link.

Appreciate your help.

Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants