cppgraphqlgen icon indicating copy to clipboard operation
cppgraphqlgen copied to clipboard

Question: How to use cppgraphqlgen as Client

Open sebu06 opened this issue 8 months ago • 4 comments
trafficstars

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

sebu06 avatar Mar 19 '25 09:03 sebu06