forked from microsoft/cppgraphqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
382 lines (303 loc) · 10.2 KB
/
Copy pathclient.cpp
File metadata and controls
382 lines (303 loc) · 10.2 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// 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 "schema/QueryResultsObject.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 <algorithm>
#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
struct AsyncIoWorker : service::RequestState
{
AsyncIoWorker()
: worker { std::make_shared<service::await_worker_thread>() }
{
}
const service::await_async worker;
};
class Results
{
public:
explicit Results(response::Value&& data, std::vector<client::Error> errors) noexcept;
service::AwaitableScalar<std::optional<std::string>> getData(
service::FieldParams&& fieldParams) const;
service::AwaitableScalar<std::optional<std::vector<std::optional<std::string>>>> getErrors(
service::FieldParams&& fieldParams) const;
private:
mutable response::Value m_data;
mutable std::vector<client::Error> m_errors;
};
Results::Results(response::Value&& data, std::vector<client::Error> errors) noexcept
: m_data { std::move(data) }
, m_errors { std::move(errors) }
{
}
service::AwaitableScalar<std::optional<std::string>> Results::getData(
service::FieldParams&& fieldParams) const
{
auto asyncIoWorker = std::static_pointer_cast<AsyncIoWorker>(fieldParams.state);
auto data = std::move(m_data);
// Jump to a worker thread for the resolver where we can run a separate I/O context without
// blocking the I/O context in Query::getRelay. This simulates how you might fan out to
// additional async I/O tasks for sub-field resolvers.
co_await asyncIoWorker->worker;
net::io_context ioc;
auto future = net::co_spawn(
ioc,
[](response::Value&& data) -> net::awaitable<std::optional<std::string>> {
co_return (data.type() == response::Type::Null)
? std::nullopt
: std::make_optional(response::toJSON(std::move(data)));
}(std::move(data)),
net::use_future);
ioc.run();
co_return future.get();
}
service::AwaitableScalar<std::optional<std::vector<std::optional<std::string>>>> Results::getErrors(
service::FieldParams&& fieldParams) const
{
auto asyncIoWorker = std::static_pointer_cast<AsyncIoWorker>(fieldParams.state);
auto errors = std::move(m_errors);
// Jump to a worker thread for the resolver where we can run a separate I/O context without
// blocking the I/O context in Query::getRelay. This simulates how you might fan out to
// additional async I/O tasks for sub-field resolvers.
co_await asyncIoWorker->worker;
net::io_context ioc;
auto future = net::co_spawn(
ioc,
[](std::vector<client::Error> errors)
-> net::awaitable<std::optional<std::vector<std::optional<std::string>>>> {
if (errors.empty())
{
co_return std::nullopt;
}
std::vector<std::optional<std::string>> results { errors.size() };
std::transform(errors.begin(),
errors.end(),
results.begin(),
[](auto& error) noexcept -> std::optional<std::string> {
return error.message.empty()
? std::nullopt
: std::make_optional<std::string>(std::move(error.message));
});
co_return std::make_optional(results);
}(std::move(errors)),
net::use_future);
ioc.run();
co_return future.get();
}
class Query
{
public:
explicit Query(std::string_view host, std::string_view port, std::string_view target,
int version) noexcept;
std::future<std::shared_ptr<proxy::object::QueryResults>> getRelay(
proxy::QueryInput&& inputArg) 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::shared_ptr<proxy::object::QueryResults>> Query::getRelay(
proxy::QueryInput&& inputArg) const
{
response::Value payload { response::Type::Map };
payload.emplace_back("query"s, response::Value { std::move(inputArg.query) });
if (inputArg.operationName)
{
payload.emplace_back("operationName"s,
response::Value { std::move(*inputArg.operationName) });
}
if (inputArg.variables)
{
payload.emplace_back("variables"s, response::Value { std::move(*inputArg.variables) });
}
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::shared_ptr<proxy::object::QueryResults>> {
// 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");
}
auto [data, errors] = client::parseServiceResponse(response::parseJSON(res.body()));
co_return std::make_shared<proxy::object::QueryResults>(
std::make_shared<Results>(std::move(data), std::move(errors)));
}(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 proxy::client::query::relayQuery;
auto query = GetRequestObject();
auto variables = serializeVariables({ QueryInput { OperationType::QUERY,
input,
((argc > 1) ? std::make_optional(argv[1]) : std::nullopt),
std::nullopt } });
auto launch = service::await_async { std::make_shared<service::await_worker_queue>() };
auto state = std::make_shared<AsyncIoWorker>();
auto serviceResponse = client::parseServiceResponse(
service->resolve({ query, GetOperationName(), std::move(variables), launch, state })
.get());
auto result = parseResponse(std::move(serviceResponse.data));
auto errors = std::move(serviceResponse.errors);
if (result.relay.data)
{
std::cout << "Data: " << *result.relay.data << std::endl;
}
if (result.relay.errors)
{
for (const auto& message : *result.relay.errors)
{
std::cerr << "Remote Error: "
<< (message ? std::string_view { *message } : "<empty>"sv) << 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;
}