Skip to content

Instantly share code, notes, and snippets.

@ZaninAndrea
Created April 4, 2022 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZaninAndrea/5ff7153b736d8fda176621ffc62cd375 to your computer and use it in GitHub Desktop.
Save ZaninAndrea/5ff7153b736d8fda176621ffc62cd375 to your computer and use it in GitHub Desktop.
Connecting to Igloo using c++
#include <iostream>
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
nlohmann::json sendRequest(std::string query, std::string token){
// Send the HTTP request
cpr::Response r = cpr::Post(cpr::Url{"https://v1.igloo.ooo/graphql"},
cpr::Body{"{\"query\": \"" + query + "\"}"},
cpr::Header{
{"Content-Type", "application/json"},
{"Authorization", "Bearer "+ token}
});
// Parse the response body as JSON
nlohmann::json parsedResponse = nlohmann::json::parse(r.text);
// Check if the server returned any errors
if (parsedResponse["errors"] != nullptr){
std::cerr << parsedResponse["errors"][0]["message"].get<std::string>() << std::endl;
throw "GraphQL Error";
}
return parsedResponse["data"];
}
int main(int argc, char** argv) {
std::string query = "{ user { email } }";
std::string token = "replace-your-token-here";
nlohmann::json parsedResponse = sendRequest(query, token);
std::string email = parsedResponse["user"]["email"].get<std::string>();
std::cout << email << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment