Successfully reported this slideshow.

Xamarin + GraphQL

0

Share

Upcoming SlideShare
Introduction to GraphQL
Introduction to GraphQL
Loading in …3
×
1 of 16
1 of 16

More Related Content

Related Books

Free with a 30 day trial from Scribd

See all

Xamarin + GraphQL

  1. 1. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/ +
  2. 2. A query language for your API
  3. 3. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/
  4. 4. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/
  5. 5. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/
  6. 6. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/ Query Response
  7. 7. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/
  8. 8. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/ GraphQL APIs Are Self Documenting Users can explore GraphQL APIs using GraphiQL
  9. 9. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/
  10. 10. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/ { “query” : “[Your GraphQL Query]” } GraphQL Request Body is JSON JSON contains one field: query
  11. 11. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/
  12. 12. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/ https://codetraveler.io/Indy-Xamarin-GraphQL/
  13. 13. @TheCodeTraveler https://codetraveler.io/indy-xamarin-graphql/ https://codetraveler.io/Indy-Xamarin-GraphQL/

Editor's Notes

  • 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}";
    }
    }
    }
  • ×