-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraphql.h
54 lines (48 loc) · 1.69 KB
/
graphql.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
class GraphQL {
public:
// Register a query
template <class T>
void Query(std::string type, std::string member,
std::function<T(std::string)> query) {
queries[type][member] = [query](std::string parameter) {
return static_cast<std::string>(query(parameter));
};
};
// Digest a GraphQL request
// Returns a JSON response
std::string Digest(std::string request) {
size_t start = 0;
size_t end = 0;
auto GetToken = [&]() {
const char* letters_and_numbers =
"abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
start = request.find_first_of(letters_and_numbers, end);
end = request.find_first_not_of(letters_and_numbers, start);
return request.substr(start, end - start);
};
std::string type = GetToken();
std::string member = GetToken();
std::string value = GetToken();
std::stringstream response;
response << "{ \"data\": { \"" << type
<< "\" : "
// TODO(DavidUser): Instead get the string output, get a json
// document and remove the uneeded members based on request
<< queries[type][member](value) << " } }";
return response.str();
};
private:
std::map<std::string,
std::map<std::string, std::function<std::string(std::string)>>>
queries;
};
#define QL_MEMBER(CLASS_NAME, MEMBER_NAME, ACTION) \
<CLASS_NAME>(#CLASS_NAME, #MEMBER_NAME, \
[](decltype(CLASS_NAME::MEMBER_NAME) MEMBER_NAME) { ACTION })
// TODO extract this member signature to assembly the graph QL from-end digest