This library provides easy to use cross-platform client API for Tanka GraphQL execution library based server using ASP.NET Core SignalR .NET Clients HubConnection
.
- Execute queries, mutations and subscriptions using SignalR HubConnection
- Supports GraphQL validation and tracing
- Leverage power of SignalR communication techniques (WebSockets, Server-Sent Events, Long Polling)
Current release is available on Nuget gallery.
Install-Package Tanka.GraphQL.Net.Client -Version 0.3.2
To access latest (pre-release) builds, you can connect to a following feed:
https://pkgs.dev.azure.com/anttikajanus/_packaging/tanka-graphql-net-client-packages/nuget/v3/index.json
See how to add custom nuget feeds in Visual Studio from here
using Tanka.GraphQL;
You can define your DTOs as POCOs. In these examples, I'm using separate models for input and output messages.
public class Message
{
public int Id { get; set; }
public string Content { get; set; }
}
public class InputMessage
{
public string Content { get; set; }
}
Create SignalR HubConnection
normally. Read more how to connect to a hub
Read more about the server implementation from Tanka GraphQL documentation
var connection = new HubConnectionBuilder()
.WithUrl("http://localhost:5000/hubs/graphql")
.Build();
await connection.StartAsync();
In GraphQL both queries and mutations are defined as queries so they are handled the same way in the API.
var channelId = 1;
var channelMessageGQL = @"query Messages($channelId: Int!) {
messages(channelId: $channelId) {
id
content
}
}";
var queryRequest = new QueryRequest()
{
Query = channelMessageGQL,
Variables = new Dictionary<string, object>()
{
{ "channelId", channelId }
}
};
var result = await connection.QueryAsync(queryRequest);
var data = result.GetDataFieldAs<List<Message>>();
var postMessageMutationGQL = @"mutation PostMessage($channelId: Int!, $message: InputMessage) {
postMessage(channelId: $channelId, message: $message) {
id
content
}
}";
var queryRequest = new QueryRequest()
{
Query = postMessageMutationGQL,
Variables = new Dictionary<string, object>()
{
{ "channelId", channelId },
{ "message", new InputMessage() { Content = message } }
}
};
var result = await connection.QueryAsync(queryRequest);
var data = result.GetDataFieldAs<Messages>();
API provides support for subscriptions as streams using IObservable<ExecutionResult>
. You can subscribe to the stream using Subscribe
method.
var channelSubsribtionGQL = @"subscription MessageAdded($channelId: Int!) {
messageAdded(channelId: $channelId) {
id
content
}
}";
var queryRequest = new QueryRequest()
{
Query = channelSubsribtionGQL,
Variables = new Dictionary<string, object>()
{
{ "channelId", channelId}
}
};
var subscriptionSource = new CancellationTokenSource();
var serverSubscription = await connection.SubscribeAsync(request, subscriptionSource);
serverSubscription.Subscribe(
// On new message added
result =>
{
var message = result.GetDataFieldAs<Message>();
// Handle new message added
},
// On error corrured
error =>
{
//Handle error
},
// On completed
() =>
{
//No more messages coming
});
// Cancelling the subscription
subscriptionSource.Cancel();