GraphQL has become a staple technology in the JavaScript community, but it is still relatively new and unknown in the C# world.
Join me as we explore the GraphQL tools available for .NET developers, like HotChocolate + Strawberry Shake, and how these tools make it easy to consume GraphQL APIs in C#!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using GraphQL.Client;
using GraphQL.Client.Http;
using GraphQL.Common.Request;
using GraphQL.Common.Response;
using Newtonsoft.Json;
using Xamarin.Forms;
namespace XamarinGraphQL
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
base.OnAppearing();
ResponseLabel.Text = "Retrieving Data...";
var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", nameof(XamarinGraphQL));
client.DefaultRequestHeaders.Add("Authorization", "bearer ceeddf326115d5f094526b7f29fe980e565e497d");
var stringContent = new StringContent("{ \"query\":\"query{ user(login: brminnick) { bio, company, createdAt }}\"}");
var response = await client.PostAsync("https://api.github.com/graphql", stringContent);
var json = await response.Content.ReadAsStringAsync();
var userResponse = JsonConvert.DeserializeObject<UserResponse>(json);
ResponseLabel.Text = userResponse.Data.User.Bio;
}
}
}
--------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using GraphQL.Client;
using GraphQL.Client.Http;
using GraphQL.Common.Request;
using GraphQL.Common.Response;
using ModernHttpClient;
using Newtonsoft.Json;
using Xamarin.Forms;
namespace XamarinGraphQL
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
base.OnAppearing();
ResponseLabel.Text = "Retrieving Data...";
var options = new GraphQLHttpClientOptions
{
EndPoint = new Uri("https://api.github.com/graphql"),
HttpMessageHandler = new NativeMessageHandler()
};
var client = new GraphQLHttpClient(options);
client.DefaultRequestHeaders.Add("User-Agent", nameof(XamarinGraphQL));
client.DefaultRequestHeaders.Add("Authorization", "bearer ceeddf326115d5f094526b7f29fe980e565e497d");
GraphQLResponse response = await client.SendQueryAsync(new GraphQLRequest
{
Query = "query{ user(login: brminnick) { bio, company, createdAt }}"
});
var bio = response.Data.user.bio;
var company = response.GetDataFieldAs<User>("user").Company;
var createdAt = response.GetDataFieldAs<User>(nameof(User).ToLower()).CreatedAt;
ResponseLabel.Text = $"createdAt: {createdAt}\n\nbio: {bio}\n\ncompany: {company}";
}
}
}