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();