c# - expected name not found

Viewed 41

I have the following GraphQl query, which works fine in PostMan.

query getFirstName($id: String!) {
  getFirstName(id: $id) {
    myId
  }
}

However, when I am executing using the traditional HttpClient module I get the error

code":400,"stacktrace":"Syntax Error: Expected Name, found \"$

My Code:

var queryObject = new
{
    query = @"query {
     getFirstName($id: String!) {
      getFirstName(id: $id) {
       myId
      }
    }
  }",
    variables = new { id = "132" }
};

httpClient.DefaultRequestHeaders.Accept.Clear();
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
httpClient.DefaultRequestHeaders.Accept.Add(contentType);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);


var myContent = JsonConvert.SerializeObject(queryObject);
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var responseTask = httpClient.PostAsync(myUri, byteContent);
responseTask.Wait();
var response = responseTask.Result;

I think my queryObject is not parsed correctly.

0 Answers
Related