27 return [executor](
http::HttpRequest& req, std::shared_ptr<http::ResponseWriter> res) ->
bool {
29 std::string operation_name;
33 if (req.
headers.count(
"Content-Type") && req.
headers.at(
"Content-Type").find(
"application/json") != std::string::npos) {
34 auto body = req.
json();
35 if (body.contains(
"query") && body[
"query"].is_string()) {
36 query = body[
"query"].
get<std::string>();
38 if (body.contains(
"operationName") && body[
"operationName"].is_string()) {
39 operation_name = body[
"operationName"].get<std::string>();
41 if (body.contains(
"variables") && body[
"variables"].is_object()) {
42 variables = body[
"variables"];
44 }
else if (req.
headers.count(
"Content-Type") && req.
headers.at(
"Content-Type").find(
"application/graphql") != std::string::npos) {
49 auto it_query = req.
query.find(
"query");
50 if (it_query != req.
query.end()) query = it_query->second;
52 auto it_op = req.
query.find(
"operationName");
53 if (it_op != req.
query.end()) operation_name = it_op->second;
55 auto it_vars = req.
query.find(
"variables");
56 if (it_vars != req.
query.end()) {
69 res->
send(std::move(response));
74 nlohmann::json result = executor(query, operation_name, variables);
77 response.
json(result);
78 res->
send(std::move(response));
79 }
catch (
const std::exception& e) {
85 res->
send(std::move(response));
a class to store JSON values
Definition json.hpp:19401
static basic_json parse(InputType &&i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true, const bool ignore_comments=false)
deserialize from a compatible input
Definition json.hpp:23317
auto get() const noexcept(noexcept(std::declval< const basic_json_t & >().template get_impl< ValueType >(detail::priority_tag< 4 > {}))) -> decltype(std::declval< const basic_json_t & >().template get_impl< ValueType >(detail::priority_tag< 4 > {}))
get a (pointer) value (explicit)
Definition json.hpp:21046
static basic_json array(initializer_list_t init={})
explicitly create an array from an initializer list
Definition json.hpp:20307
static basic_json object(initializer_list_t init={})
explicitly create an object from an initializer list
Definition json.hpp:20315
Represents an HTTP response to be sent to a client.
Definition HttpResponse.hpp:107
HttpResponse & json(const std::string &j) &
Sets the response body as JSON from a string.
Definition HttpResponse.hpp:181
HttpStatus status_code
Definition HttpResponse.hpp:110
HttpResponse & send(const std::string &b) &
Sets the response body as plain text.
Definition HttpResponse.hpp:174
basic_json<> json
default specialization
Definition json.hpp:3422
Definition HttpRequest.hpp:19
std::function< bool(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> graphql(GraphQLExecutor executor)
Creates a middleware handler for GraphQL endpoints. It parses the incoming GET or POST request accord...
Definition GraphQL.hpp:26
std::function< nlohmann::json(const std::string &query, const std::string &operation_name, const nlohmann::json &variables)> GraphQLExecutor
Signature for a GraphQL executor function. Takes the query string, operation name (optional),...
Definition GraphQL.hpp:17
Represents an incoming HTTP request.
Definition HttpRequest.hpp:54
HttpMethod method
Definition HttpRequest.hpp:55
std::unordered_map< std::string, std::string > query
Definition HttpRequest.hpp:58
nlohmann::json json() const
Parses and returns the request body as a JSON object.
Definition HttpRequest.hpp:150
std::string_view body
Definition HttpRequest.hpp:61
std::unordered_map< std::string_view, std::string_view, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual > headers
Definition HttpRequest.hpp:60