https://github.com/anttikajanus/tanka-graphql-net-client
Cross platform .Net client library for working with Tanka GraphQL execution library.
https://github.com/anttikajanus/tanka-graphql-net-client
client-side cross-platform graphql native-apps netstandard20 signalr signalr-core signalr-hub tanka uwp wpf xamarin
Last synced: 5 months ago
JSON representation
Cross platform .Net client library for working with Tanka GraphQL execution library.
- Host: GitHub
- URL: https://github.com/anttikajanus/tanka-graphql-net-client
- Owner: anttikajanus
- License: apache-2.0
- Created: 2019-01-03T15:35:26.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T09:52:46.000Z (over 3 years ago)
- Last Synced: 2025-11-15T23:13:13.464Z (7 months ago)
- Topics: client-side, cross-platform, graphql, native-apps, netstandard20, signalr, signalr-core, signalr-hub, tanka, uwp, wpf, xamarin
- Language: C#
- Size: 162 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Tanka GraphQL .NET Client library
=====================================
This library provides easy to use cross-platform client API for [Tanka GraphQL execution library](https://github.com/pekkah/tanka-graphql) based server using [ASP.NET Core SignalR .NET Client](https://docs.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-2.2)s `HubConnection`.
[](https://dev.azure.com/anttikajanus/tanka-graphql-net-client/_build/latest?definitionId=1?branchName=master)
## 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](https://go.microsoft.com/fwlink/?linkid=698608)
### Short API summary
#### Add usings
```csharp
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.
```csharp
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](https://docs.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-2.2#connect-to-a-hub)
Read more about the server implementation from [Tanka GraphQL documentation](https://github.com/pekkah/tanka-graphql/tree/aef8fc4a8f9ae4da08812293ad0e7e51cf0312eb#server)
```csharp
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
```csharp
var channelId = 1;
var channelMessageGQL = @"query Messages($channelId: Int!) {
messages(channelId: $channelId) {
id
content
}
}";
var queryRequest = new QueryRequest()
{
Query = channelMessageGQL,
Variables = new Dictionary()
{
{ "channelId", channelId }
}
};
var result = await connection.QueryAsync(queryRequest);
var data = result.GetDataFieldAs>();
````
##### Mutation
```csharp
var postMessageMutationGQL = @"mutation PostMessage($channelId: Int!, $message: InputMessage) {
postMessage(channelId: $channelId, message: $message) {
id
content
}
}";
var queryRequest = new QueryRequest()
{
Query = postMessageMutationGQL,
Variables = new Dictionary()
{
{ "channelId", channelId },
{ "message", new InputMessage() { Content = message } }
}
};
var result = await connection.QueryAsync(queryRequest);
var data = result.GetDataFieldAs();
```
#### Subscription
API provides support for subscriptions as streams using `IObservable`. You can subscribe to the stream using `Subscribe` method.
```csharp
var channelSubsribtionGQL = @"subscription MessageAdded($channelId: Int!) {
messageAdded(channelId: $channelId) {
id
content
}
}";
var queryRequest = new QueryRequest()
{
Query = channelSubsribtionGQL,
Variables = new Dictionary()
{
{ "channelId", channelId}
}
};
var subscriptionSource = new CancellationTokenSource();
var serverSubscription = await connection.SubscribeAsync(request, subscriptionSource);
serverSubscription.Subscribe(
// On new message added
result =>
{
var message = result.GetDataFieldAs();
// Handle new message added
},
// On error corrured
error =>
{
//Handle error
},
// On completed
() =>
{
//No more messages coming
});
// Cancelling the subscription
subscriptionSource.Cancel();
```