Skip to content

Cross platform .Net client library for working with Tanka GraphQL execution library.

License

Notifications You must be signed in to change notification settings

anttikajanus/tanka-graphql-net-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

100 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tanka GraphQL .NET Client library

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.

Build Status

Features

  • 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)

Gettings started

Install

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

Short API summary

Add usings

using Tanka.GraphQL;

Defining models

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; }
}

Connect to the server endpoint

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();

Queries and mutations

In GraphQL both queries and mutations are defined as queries so they are handled the same way in the API.

Query
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>>();
Mutation
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>();

Subscription

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();

About

Cross platform .Net client library for working with Tanka GraphQL execution library.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages