最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - GraphQLHttpClient is returning 'null' data - C# - Stack Overflow

programmeradmin2浏览0评论

I'm trying to run a GraphQL query against Rubrik (our backup environment).

I'm using the 'GraphQL' nuggets including the GraphQL.Client.Abstractions, Client.Serializer.SystemTextJson,...

I have a feeling I'm close but the last part is eluding me.

I set the options for 'GraphQLHttpClientOptions' to using 'application/json' and I set the Uri to the graphql api.

GraphQLHttpClientOptions options = new GraphQLHttpClientOptions();
options.MediaType = "application/json";
options.EndPoint = new Uri(";);

Next the client is created with the options and the bearer token

GraphQLHttpClient graphClient = new GraphQLHttpClient(options, new GraphQL.Client.Serializer.SystemTextJson.SystemTextJsonSerializer());
graphClient.HttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
graphClient.HttpClient.DefaultRequestHeaders.Add("Accept", "*/*");
graphClient.HttpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");

I have the query:

string query = @"query HypervServersQuery($input: QueryHypervHostInput!) {
            hypervServers(input: $input) {
                data {
                    hostname
                    serverName
                    id
                    __typename
                }
                __typename
            }
        }";

and the variables

var variables_ = new
{
    name = "xxx",
    primaryClusterId = "xxx",
    clusterUuid = "xxx"
};

var variables = new
{
    input = variables_
};

And as last part I'am sending the query

GraphQLRequest query = new GraphQLRequest { Query = query , Variables = variables };
var result = graphClient.SendQueryAsync<DataResponse>(query).GetAwaiter().GetResult(); 

The status code is 'OK'. It was 'Bad Request' for a lot of tries until it finally was in the correct format.

The only remaining issue is that the returned data is 'NULL'. I'am using the same query and variables as I'm using when I do this via Powershell. So it should not be 'NULL'. Data should be returned.

Maybe I'm missing a final JSON convertion somewere but my expertise in GraphQL is not that big.

I tried multiple converts like via 'JsonSerializer.Serialize' but alas nothing returns. I have no idea of what I'm doing wrong.

Solved

I just had to change the last line to:

GraphQLResponse<dynamic> graphQLResponse = graphQLClient.SendQueryAsync<dynamic>(query).GetAwaiter().GetResult();
发布评论

评论列表(0)

  1. 暂无评论