Question: How to use cppgraphqlgen as Client
Hi, I'm missing a sample to use cppgraphqlgen only as a client for an existing graphql server (Graphene/Django). After a lot of reading, I came up with this, but I'm a little bit stuck on how to set Variables to my query.
auto svar = graphql::client::query::getObjectclassById::serializeVariables({1});
// auto query = graphql::client::query::getObjectclassById::GetRequestObject();
// const auto& name = graphql::client::query::getObjectclassById::GetOperationName();
auto res = cpr::Post(
cpr::Url({Config::gqlServer() + "/graphql/"}),
cpr::Header({{"Content-Type", "application/graphql"}}),
cpr::Body(graphql::client::query::getObjectclassById::GetRequestText()));
auto r = graphql::response::parseJSON(res.text);
auto serviceResponse = graphql::client::parseServiceResponse(std::move(r));
const auto parsed = graphql::client::query::getObjectclassById::parseResponse(std::move(serviceResponse.data));
if (parsed.objectclassById) {
std::cout << parsed.objectclassById->title.value_or("title not set") << std::endl;
}
Is my use of the library correct so far? How do I set the variables in the query to the server?
Thanks, Sebastian
What I had to do was use a separate json library, create a json object that I set the query (GetRequestText), operation name (GetOperationName), and the variables. then send that as the payload. as far as I could tell cppgraphqlgen client does not have a way to set the variables and then serialize it to a json string.
I came up with the following:
nlohmann::json req;
req["variables"] = nlohmann::json::parse(graphql::response::toJSON(
graphql::client::query::getActivityById::serializeVariables({mDBid})));
req["operationName"] =
graphql::client::query::getActivityById::GetOperationName();
req["query"] = graphql::client::query::getActivityById::GetRequestText();
auto res = cpr::Post(
cpr::Url({"https://some.example/graphql/"}),
cpr::Header({{"Content-Type", "application/json"}}),
cpr::Body(req.dump()));
if (res.status_code != 200) {
return;
}
auto r = graphql::response::parseJSON(res.text);
auto serviceResponse = graphql::client::parseServiceResponse(std::move(r));
const auto parsed = graphql::client::query::getActivityById::parseResponse(
std::move(serviceResponse.data));
What I had to do was use a separate json library, create a json object that I set the query (GetRequestText), operation name (GetOperationName), and the variables. then send that as the payload. as far as I could tell cppgraphqlgen client does not have a way to set the variables and then serialize it to a json string.
Sorry this took so long to respond. The idea is that you should not need to use another JSON library, you can use the graphqljson library directly (see JSONResponse.h). Once you have a graphql::response::Value for the variables (which has slightly different semantics from JSON, but is generally compatible with JSON), you could even build a container graphql::response::Value with your full request payload and serialize that directly to JSON. But if you are already using another JSON library or you don't want to use this type for it, you can certainly replace that with your own JSON library and just reparse the JSON output from cppgraphqlgen or treat it as an opaque string, depending on your transport.
There's an example of this in the proxy sample, where it builds a complete request payload using graphql::response::Value: https://github.com/microsoft/cppgraphqlgen/blob/0df2b32d94402ecc090bf55b9d7cf7ab2cf93245/samples/proxy/client.cpp#L79
thanks for the reply! I thought it was weird that it was possible to fill out a payload but no way to serialize it (turns out that was wrong)