forked from microsoft/cppgraphqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
265 lines (207 loc) · 6.91 KB
/
client.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "query/ProxyClient.h"
#include "schema/ProxySchema.h"
#include "schema/QueryObject.h"
#include "graphqlservice/JSONResponse.h"
#ifdef _MSC_VER
#include <SDKDDKVer.h>
#endif // _MSC_VER
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/awaitable.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <boost/asio/use_future.hpp>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <iterator>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
using namespace std::literals;
using namespace graphql;
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;
constexpr auto c_host = "127.0.0.1"sv;
constexpr auto c_port = "8080"sv;
constexpr auto c_target = "/graphql"sv;
constexpr int c_version = 11; // HTTP 1.1
class Query
{
public:
explicit Query(std::string_view host, std::string_view port, std::string_view target,
int version) noexcept;
std::future<std::optional<std::string>> getRelay(std::string&& queryArg,
std::optional<std::string>&& operationNameArg,
std::optional<std::string>&& variablesArg) const;
private:
const std::string m_host;
const std::string m_port;
const std::string m_target;
const int m_version;
};
Query::Query(
std::string_view host, std::string_view port, std::string_view target, int version) noexcept
: m_host { host }
, m_port { port }
, m_target { target }
, m_version { version }
{
}
// Based on:
// https://www.boost.org/doc/libs/1_82_0/libs/beast/example/http/client/awaitable/http_client_awaitable.cpp
std::future<std::optional<std::string>> Query::getRelay(std::string&& queryArg,
std::optional<std::string>&& operationNameArg, std::optional<std::string>&& variablesArg) const
{
response::Value payload { response::Type::Map };
payload.emplace_back("query"s, response::Value { std::move(queryArg) });
if (operationNameArg)
{
payload.emplace_back("operationName"s, response::Value { std::move(*operationNameArg) });
}
if (variablesArg)
{
payload.emplace_back("variables"s, response::Value { std::move(*variablesArg) });
}
std::string requestBody = response::toJSON(std::move(payload));
net::io_context ioc;
auto future = net::co_spawn(
ioc,
[](const char* host,
const char* port,
const char* target,
int version,
std::string requestBody) -> net::awaitable<std::optional<std::string>> {
// These objects perform our I/O. They use an executor with a default completion token
// of use_awaitable. This makes our code easy, but will use exceptions as the default
// error handling, i.e. if the connection drops, we might see an exception.
auto resolver =
net::use_awaitable.as_default_on(tcp::resolver(co_await net::this_coro::executor));
auto stream = net::use_awaitable.as_default_on(
beast::tcp_stream(co_await net::this_coro::executor));
// Look up the domain name.
const auto resolved = co_await resolver.async_resolve(host, port);
// Set the timeout.
stream.expires_after(std::chrono::seconds(30));
// Make the connection on the IP address we get from a lookup
stream.connect(resolved);
// Set up an HTTP POST request message
http::request<http::string_body> req { http::verb::post, target, version };
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
req.body() = std::move(requestBody);
req.prepare_payload();
// Set the timeout.
stream.expires_after(std::chrono::seconds(30));
// Send the HTTP request to the remote host
co_await http::async_write(stream, req);
// This buffer is used for reading and must be persisted
beast::flat_buffer b;
// Declare a container to hold the response
http::response<http::string_body> res;
// Receive the HTTP response
co_await http::async_read(stream, b, res);
// Gracefully close the socket
beast::error_code ec;
stream.socket().shutdown(tcp::socket::shutdown_both, ec);
// not_connected happens sometimes
// so don't bother reporting it.
if (ec && ec != beast::errc::not_connected)
{
throw boost::system::system_error(ec, "shutdown");
}
co_return std::make_optional<std::string>(std::move(res.body()));
}(m_host.c_str(), m_port.c_str(), m_target.c_str(), m_version, std::move(requestBody)),
net::use_future);
ioc.run();
return future;
}
int main(int argc, char** argv)
{
auto service = std::make_shared<proxy::Operations>(std::make_shared<proxy::object::Query>(
std::make_shared<Query>(c_host, c_port, c_target, c_version)));
std::cout << "Created the service..." << std::endl;
try
{
std::istream_iterator<char> start { std::cin >> std::noskipws }, end {};
std::string input { start, end };
std::cout << "Executing query..." << std::endl;
using namespace client::query::relayQuery;
auto query = GetRequestObject();
auto variables = serializeVariables(
{ input, ((argc > 1) ? std::make_optional(argv[1]) : std::nullopt) });
auto launch = service::await_async { std::make_shared<service::await_worker_queue>() };
auto serviceResponse = client::parseServiceResponse(
service->resolve({ query, GetOperationName(), std::move(variables), launch }).get());
auto result = client::query::relayQuery::parseResponse(std::move(serviceResponse.data));
auto errors = std::move(serviceResponse.errors);
if (result.relay)
{
std::cout << *result.relay << std::endl;
}
if (!errors.empty())
{
std::cerr << "Errors executing query:" << std::endl << GetRequestText() << std::endl;
for (const auto& error : errors)
{
std::cerr << "Error: " << error.message;
if (!error.locations.empty())
{
bool firstLocation = true;
std::cerr << ", Locations: [";
for (const auto& location : error.locations)
{
if (!firstLocation)
{
std::cerr << ", ";
}
firstLocation = false;
std::cerr << "(line: " << location.line << ", column: " << location.column
<< ")";
}
std::cerr << "]";
}
if (!error.path.empty())
{
bool firstSegment = true;
std::cerr << ", Path: ";
for (const auto& segment : error.path)
{
std::visit(
[firstSegment](const auto& value) noexcept {
using _Type = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<std::string, _Type>)
{
if (!firstSegment)
{
std::cerr << "/";
}
std::cerr << value;
}
else if constexpr (std::is_same_v<int, _Type>)
{
std::cerr << "[" << value << "]";
}
},
segment);
firstSegment = false;
}
}
std::cerr << std::endl;
}
}
}
catch (const std::runtime_error& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}