Hasura上GraphQL的C#中的Fetch()等效项 [英] Fetch() equivalent in C# for GraphQL on Hasura
问题描述
我正在尝试了解应用于.net(C#)的GraphQL.
I'm trying to learn about GraphQL applied to .net (C#).
我的目标是转换此javascript代码:
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));
,转换为C#.这是我的位置,但无法继续操作:
,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?
当我运行javascript代码时,当我运行C#代码时没有问题,我从Hasura收到以下消息:{"errors":[{"extensions":{"path":"$","code":"parse-failed"}},"message":在解析类型为Hasura.GraphQL.Transport.HTTP的构造函数GQLReq时.Protocol.GQLReq应该是对象,但得到了字符串.}]}
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."}]}
推荐答案
body: JSON.stringify({query: " {test {qqch date}}"})
所以正文= "{\" query \:\" {测试{qqch日期}} \}"
?
var json = JsonConvert.SerializeObject("{query: {test {qqch date}}}");
但是在您的C#代码中,json = "\" {query:{test {qqch date}}} \"
?
But in your c# code, json = "\"{query: {test {qqch date}}}\""
?
您可以对您的json字符串进行硬编码.或研究其他使用JsonConvert.SerializeObject将c#类型转换为json字符串的方法.
You could just hard code your json string. Or research other methods to use JsonConvert.SerializeObject to convert c# types to json strings.
这篇关于Hasura上GraphQL的C#中的Fetch()等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!