-
Notifications
You must be signed in to change notification settings - Fork 130
/
Program.cs
135 lines (121 loc) · 4.05 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Net.WebSockets;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GraphQL.Client.Http;
using GraphQL.Common.Request;
namespace SubsccriptionIntegrationTest.ConsoleClient
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("configuring client ...");
using (var client = new GraphQLHttpClient("http://localhost:5000/graphql/", new GraphQLHttpClientOptions{ UseWebSocketForQueriesAndMutations = true }))
{
Console.WriteLine("subscribing to message stream ...");
var subscriptions = new CompositeDisposable();
subscriptions.Add(client.WebSocketReceiveErrors.Subscribe(e => {
if(e is WebSocketException we)
Console.WriteLine($"WebSocketException: {we.Message} (WebSocketError {we.WebSocketErrorCode}, ErrorCode {we.ErrorCode}, NativeErrorCode {we.NativeErrorCode}");
else
Console.WriteLine($"Exception in websocket receive stream: {e.ToString()}");
}));
subscriptions.Add(CreateSubscription("1", client));
await Task.Delay(200);
subscriptions.Add(CreateSubscription2("2", client));
await Task.Delay(200);
subscriptions.Add(CreateSubscription("3", client));
await Task.Delay(200);
subscriptions.Add(CreateSubscription("4", client));
await Task.Delay(200);
subscriptions.Add(CreateSubscription("5", client));
await Task.Delay(200);
subscriptions.Add(CreateSubscription("6", client));
await Task.Delay(200);
subscriptions.Add(CreateSubscription("7", client));
using (subscriptions)
{
Console.WriteLine("client setup complete");
var quit = false;
do
{
Console.WriteLine("write message and press enter...");
var message = Console.ReadLine();
var graphQLRequest = new GraphQLRequest(@"
mutation($input: MessageInputType){
addMessage(message: $input){
content
}
}")
{
Variables = new
{
input = new
{
fromId = "2",
content = message,
sentAt = DateTime.Now
}
}
};
var result = await client.SendMutationAsync(graphQLRequest).ConfigureAwait(false);
if(result.Errors != null && result.Errors.Length > 0)
{
Console.WriteLine($"request returned {result.Errors.Length} errors:");
foreach (var item in result.Errors)
{
Console.WriteLine($"{item.Message}");
}
}
}
while(!quit);
Console.WriteLine("shutting down ...");
}
Console.WriteLine("subscriptions disposed ...");
}
Console.WriteLine("client disposed ...");
}
private static IDisposable CreateSubscription(string id, GraphQLHttpClient client)
{
#pragma warning disable 618
var stream = client.CreateSubscriptionStream(new GraphQLRequest(@"
subscription {
messageAdded{
content
from {
displayName
}
}
}"
)
{ Variables = new { id } });
#pragma warning restore 618
return stream.Subscribe(
response => Console.WriteLine($"{id}: new message from \"{response.Data.messageAdded.from.displayName.Value}\": {response.Data.messageAdded.content.Value}"),
exception => Console.WriteLine($"{id}: message subscription stream failed: {exception}"),
() => Console.WriteLine($"{id}: message subscription stream completed"));
}
private static IDisposable CreateSubscription2(string id, GraphQLHttpClient client)
{
#pragma warning disable 618
var stream = client.CreateSubscriptionStream(new GraphQLRequest(@"
subscription {
contentAdded{
content
from {
displayName
}
}
}"
)
{ Variables = new { id } });
#pragma warning restore 618
return stream.Subscribe(
response => Console.WriteLine($"{id}: new content from \"{response.Data.contentAdded.from.displayName.Value}\": {response.Data.contentAdded.content.Value}"),
exception => Console.WriteLine($"{id}: content subscription stream failed: {exception}"),
() => Console.WriteLine($"{id}: content subscription stream completed"));
}
}
}