GraphQL in .NET Windows Forms Application

Solved

GraphQL in .NET Windows Forms Application

mpex
Tourist
10 1 3

I'm trying to get product data using HttpClient and GraphQL in a .NET Windows Forms Application, but it's not working properly.
After some research, I created the following code, but the PostAsync response is 400 (Bad Request).
Do you know what's wrong?

 

async void Example()
{
    try
    {
        var accessToken = "***";
        var uri = new Uri("https://***.myshopify.com/admin/api/2024-07/graphql.json");

        using var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("X-Shopify-Access-Token", $"{accessToken}");

        using var jsonContent = JsonContent.Create("{ products(first: 3) { nodes { title id variants(first: 3) { nodes { title barcode inventoryQuantity } } } } }");
        //using var jsonContent = JsonContent.Create("{\"query\": \"{ products(first: 3) { nodes { title id variants(first: 3) { nodes { title barcode inventoryQuantity } } } } }\" }");

        var response = await httpClient.PostAsync(uri, jsonContent);
        if (response.IsSuccessStatusCode)
        {
            var result = await response.Content.ReadAsStringAsync();
            Debug.WriteLine($"{result}");
        }
    }
    catch (Exception exception)
    {
        Debug.WriteLine($"{exception.Message}");
    }
}
Accepted Solution (1)

mpex
Tourist
10 1 3

This is an accepted solution.

I got it to work by doing the following.
I don't know why you need to write the query like this though.

 

async void Example()
{
    try
    {
        // GraphiQL for the Admin API
        // https://shopify.dev/docs/api/usage/api-exploration/admin-graphiql-explorer

        #if true
            var accessToken = "***";
            var uri = new Uri("https://***.myshopify.com/admin/api/2024-07/graphql.json");
        #else
            var accessToken = "123";
            var uri = new Uri("http://httpbin.org/post");
        #endif

        using var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("X-Shopify-Access-Token", $"{accessToken}");

        var stringContent = new StringContent("{\"query\":\"query Products { products(first: 3) { nodes { title id variants(first: 3) { nodes { title barcode inventoryQuantity } } } } }\"}", Encoding.UTF8, "application/json");

        var response = await httpClient.PostAsync(uri, stringContent);
        if (response.IsSuccessStatusCode)
        {
            var result = await response.Content.ReadAsStringAsync();
            Debug.WriteLine($"{result}");
        }
    }
    catch (Exception exception)
    {
        Debug.WriteLine($"{exception.Message}");
    }
}

View solution in original post

Reply 1 (1)

mpex
Tourist
10 1 3

This is an accepted solution.

I got it to work by doing the following.
I don't know why you need to write the query like this though.

 

async void Example()
{
    try
    {
        // GraphiQL for the Admin API
        // https://shopify.dev/docs/api/usage/api-exploration/admin-graphiql-explorer

        #if true
            var accessToken = "***";
            var uri = new Uri("https://***.myshopify.com/admin/api/2024-07/graphql.json");
        #else
            var accessToken = "123";
            var uri = new Uri("http://httpbin.org/post");
        #endif

        using var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("X-Shopify-Access-Token", $"{accessToken}");

        var stringContent = new StringContent("{\"query\":\"query Products { products(first: 3) { nodes { title id variants(first: 3) { nodes { title barcode inventoryQuantity } } } } }\"}", Encoding.UTF8, "application/json");

        var response = await httpClient.PostAsync(uri, stringContent);
        if (response.IsSuccessStatusCode)
        {
            var result = await response.Content.ReadAsStringAsync();
            Debug.WriteLine($"{result}");
        }
    }
    catch (Exception exception)
    {
        Debug.WriteLine($"{exception.Message}");
    }
}