I want to send chat messages using AppSync from a .net Core backend, using the nuget package AWSSDK.AppSync. But I Can't find any guide or sample code to do so.
I'm trying to code something similar to the AWS mobile sample application AppSync Chat Starter Angular. The goal is to have a conversation between the user (on a browser) and my backend. There are many samples for iOS, Android, web and react native, where I could find some clues on how to do it, but couldn't manage to get it working in .net core. The best documentation I've found so far is the official Amazon.AppSync API but there you'll find only classes and fields descriptions, not sample code.
I have that Angular AppSync Chat sample app running fine. I did manage to run the React sample app too. Everything goes smoothly with those. However I'm unable to fully understand what's going on only by reading the angular and react sample code, especially because I'm not proficient on those.
Would be great to have a sample code on how to initialise the AmazonAppSyncClient
, run some queries, perform some mutations, subscribe/unsubscribe and set background tasks.
Ok, so here is a little snippet for how to setup client and do a simple query.
Install GraphQL client library, for example from NuGet (works also in Unity), you need these things:
using GraphQL;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.Newtonsoft;
using GraphQL.Client.Abstractions;
Then initialize your client like this:
var client = new GraphQLHttpClient("your_graphql_endpoint_url", new NewtonsoftJsonSerializer());
Optionally, you may want to add authentication or api key headers, here is example of how to add authorization token (notice, there is no "Bearer " word in it, just the token)
client.HttpClient.DefaultRequestHeaders.Add("Authorization", "your_long_token_string");
Now about how to do a simple query. Here is an example for listing items query, create request:
var request = new GraphQLRequest
{
Query = @"
query MyQuery {
listItems {
items {
id
ItemName
}
}
}"
};
Send request:
var request = await _client.SendQueryAsync(request, () => new
{
listItems = new
{
items = new List<Your_Item_Type>()
}
});
Your_Item_type[] items = request.Data.listItems.items.ToArray();
Check the official repository for the client here: https://github.com/graphql-dotnet/graphql-client