C# 对于Hasura上的GraphQL,C中的Fetch()等价物
我试图学习应用于.NETC的GraphQL 我的目标是转换此javascript代码:C# 对于Hasura上的GraphQL,C中的Fetch()等价物,c#,graphql,hasura,C#,Graphql,Hasura,我试图学习应用于.NETC的GraphQL 我的目标是转换此javascript代码: fetch('https://____.herokuapp.com/v1/graphql', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', }, body: JSON.stringify({query: " {test {qqch da
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));
,改为C。这就是我现在所处的位置,但不能再进一步了:
//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();
}
谁能告诉我我做错了什么
当我运行javascript代码时,我没有问题。当我运行C代码时,我从Hasura那里得到以下消息:
{错误:[{扩展名:{path:$,代码:解析失败},消息:解析类型为Hasura.GraphQL.Transport.HTTP.Protocol.GQLReq的构造函数GQLReq时,需要对象,但得到字符串。}]}
body: JSON.stringify({query: " {test {qqch date}}"})
所以body={\query\:{test{qqch date}}}
var json=JsonConvert.SerializeObject{query:{test{qqch date}}};
但是在您的c代码中,json=\{query:{test{qqch date}}}\
您可以硬编码json字符串。或者研究使用JsonConvert.SerializeObject将c类型转换为json字符串的其他方法。您的JS使用查询。你的C没有。我已经用:var json=JsonConvert.SerializeObject{query:{test{qqch date}}};但没用,干得好。。管理油井是解决方案。