-
I hope I can use graphql query to export some data to Excel files, but this requires me to initiate a graphql query request in the background and then obtain the data. I found such a class in the source code public class OrchardGraphQLClient
{
public OrchardGraphQLClient(HttpClient client)
{
Client = client;
}
public ContentResource Content => new ContentResource(Client);
public HttpClient Client { get; }
} As shown above, I need to build an httpclient before using it At the same time, we need to fill all the information requested by the clientRequest into the new httpclient |
Beta Was this translation helpful? Give feedback.
Answered by
hyzx86
Jul 26, 2022
Replies: 1 comment 4 replies
-
http: private readonly HttpClient _httpClient;
public ExcelController(IHttpClientFactory httpClientFactory)
{
_httpClient = httpClientFactory.CreateClient(Consts.GraphqlClient);
}
public async Task<ActionResult> ExportByGraphqlQuery(ExportByGraphqlQueryOptions options)
{
#if DEBUG
options.GraphQLQuery = @" customer { createdUtc
custNum dateeeee datetimesssssss displayText guimo modifiedUtc name publishedUtc timeeeeeeeeeee }";
#endif
_httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}");
_httpClient.DefaultRequestHeaders.Clear();
if (!string.IsNullOrEmpty(Request.Headers["Cookie"]))
{
_httpClient.DefaultRequestHeaders.Add("Cookie", Request.Headers["Cookie"].ToString());
}
if (!string.IsNullOrEmpty(Request.Headers["Authorization"]))
{
_httpClient.DefaultRequestHeaders.Add("Authorization", Request.Headers["Authorization"].ToString());
}
var requestJson = new JObject(
new JProperty("query", @"query { " + options.GraphQLQuery + " }")
);
if (options.QueryParams is not null)
{
requestJson["variables"] = JObject.FromObject(options.QueryParams);
}
var request = new HttpRequestMessage(HttpMethod.Post, "api/graphql");
request.Content = new StringContent(requestJson.ToString(), Encoding.UTF8, "application/json");
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
throw new Exception(response.StatusCode + " " + await response.Content.ReadAsStringAsync());
}
var result = JObject.Parse(await response.Content.ReadAsStringAsync());
return Json(result);//scuccess
} direct to excute graphql Query: |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
hyzx86
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http: