How do I add a Variable to a GraphQLRequest
gregorvilkner opened this issue · 2 comments
gregorvilkner commented
I would like to inject a Variable into a GraphQLRequest. I am only able to accomplish this by casting the request through a JObject like so:
GraphQLRequest request = new GraphQLRequest { Query = fancyQuery };
JObject requestJObject = JObject.FromObject(request);
JObject variablesProperty = requestJObject["Variables"] as JObject != null ? requestJObject["Variables"] as JObject : new JObject();
variablesProperty["my variable"] = "variable value";
requestJObject["Variables"] = variablesProperty;
request = requestJObject.ToObject<GraphQLRequest>();
I feel like there's got to be a better way to do this, but can't figure out how to deal with the GraphQL.Input class.
Shane32 commented
Can you use new Inputs(new Dictionary<string, object?> { { “test”, “value” } })
? (Going from memory here…)
gregorvilkner commented
dang. i think i got close to that before. missed the constructor on the inputs that takes a dictionary. going with this:
Dictionary<string, object?> variables = new Dictionary<string, object?> { { "my variable", "variable value" } };
if (request.Variables != null)
{
foreach (var aVariable in request.Variables)
{
variables.Add(aVariable.Key, aVariable.Value);
}
}
request.Variables = new Inputs(variables);
thanks a lot for the quick response.
cheers.