Fetch() 在 C# 中等效于 Hasura 上的 GraphQL
Fetch() equivalent in C# for GraphQL on Hasura
我正在尝试了解应用于 .net (C#) 的 GraphQL。
我的目标是转换此 javascript 代码:
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 收到以下消息:
{"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}}"})
所以 body = "{\"query\": \" {test {qqch date}}\"}"
?
var json = JsonConvert.SerializeObject("{query: {test {qqch date}}}");
但是在你的 C# 代码中,json = "\"{query: {test {qqch date}}}\""
?
您可以对 json 字符串进行硬编码。或者研究其他方法来使用 JsonConvert.SerializeObject 将 c# 类型转换为 json 字符串。
我正在尝试了解应用于 .net (C#) 的 GraphQL。
我的目标是转换此 javascript 代码:
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 收到以下消息: {"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}}"})
所以 body = "{\"query\": \" {test {qqch date}}\"}"
?
var json = JsonConvert.SerializeObject("{query: {test {qqch date}}}");
但是在你的 C# 代码中,json = "\"{query: {test {qqch date}}}\""
?
您可以对 json 字符串进行硬编码。或者研究其他方法来使用 JsonConvert.SerializeObject 将 c# 类型转换为 json 字符串。