0

I'm trying to learn about GraphQL applied to .net (C#).

My goal is to do transform this javascript code :

fetch('https://____.herokuapp.com/v1/graphql', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  },
  body: JSON.stringify({query: " {test {qqch date}}"})
})
  .then(r => r.json())
  .then(data => console.log('data returned:', data)); 

,into C# . Here is where I'm at but can't get any further :

    //Define your baseUrl
    string baseUrl = "https://_____.herokuapp.com/v1/graphql";
    var json = JsonConvert.SerializeObject("{test {qqch date}}"); 

    StringContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");


    //Have your using statements within a try/catch block
    try
    {
        //We will now define your HttpClient with your first using statement which will use a IDisposable.
        using (HttpClient client = new HttpClient())
        {
            //In the next using statement you will initiate the Get Request, use the await keyword so it will execute the using statement in order.
            using (HttpResponseMessage res = await client.PostAsync(baseUrl, httpContent))
            {
                //Then get the content from the response in the next using statement, then within it you will get the data, and convert it to a c# object.
                using (HttpContent content = res.Content)
                {
                    //Now assign your content to your data variable, by converting into a string using the await keyword.
                    var data = await content.ReadAsStringAsync();

                    //If the data isn't null return log convert the data using newtonsoft JObject Parse class method on the data.
                    if (data != null)
                    {
                        //Now log your data in the console
                        Console.WriteLine("data------------{0}", data);
                        Console.ReadKey();

                    }
                    else
                    {
                        Console.WriteLine("NO Data----------");
                        Console.ReadKey();

                    }
                }
            }
        }
    }
    catch (Exception exception)
    {
        Console.WriteLine("Exception Hit------------");
        Console.WriteLine(exception);
        Console.ReadKey();
    }

Can somebody tell me what I'm doing wrong?

When I run javascript code I have no problem when I run the C# code I'm getting the following message from Hasura : {"errors":[{"extensions":{"path":"$","code":"parse-failed"},"message":"When parsing the constructor GQLReq of type Hasura.GraphQL.Transport.HTTP.Protocol.GQLReq expected Object but got String."}]}

ooo
  • 3
  • 3

1 Answers1

1
body: JSON.stringify({query: " {test {qqch date}}"})

So body = "{\"query\": \" {test {qqch date}}\"}"?

var json = JsonConvert.SerializeObject("{query: {test {qqch date}}}");

But in your c# code, json = "\"{query: {test {qqch date}}}\""?

You could just hard code your json string. Or research other methods to use JsonConvert.SerializeObject to convert c# types to json strings.

Jeremy Lakeman
  • 9,515
  • 25
  • 29