FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppgraphqlgen/src/GraphQLClient.cpp at next · wravery/cppgraphqlgen · GitHub
wravery
/
cppgraphqlgen
Public
forked from
microsoft/cppgraphqlgen
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppgraphqlgen
/
src
/
GraphQLClient.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
263 lines (211 loc) · 5.13 KB
Breadcrumbs
cppgraphqlgen
/
src
/
GraphQLClient.cpp
Copy path
File metadata and controls
263 lines (211 loc) · 5.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
Copyright (c) Microsoft Corporation. All rights reserved.
//
Licensed under the MIT License.
#
include
"
graphqlservice/GraphQLClient.h
"
#
include
<
cstddef
>
using
namespace
std
::literals
;
namespace
graphql
::client {
ErrorLocation
parseServiceErrorLocation
(response::Value&& location)
{
ErrorLocation result;
if
(location.
type
() == response::Type::Map)
{
auto
members = location.
release
<response::MapType>();
for
(
auto
& member : members)
{
if
(member.
first
==
"
line
"
sv)
{
if
(member.
second
.
type
() == response::Type::Int)
{
result.
line
=
static_cast
<std::
size_t
>(member.
second
.
get
<
int
>());
}
continue
;
}
if
(member.
first
==
"
column
"
sv)
{
if
(member.
second
.
type
() == response::Type::Int)
{
result.
column
=
static_cast
<std::
size_t
>(member.
second
.
get
<
int
>());
}
continue
;
}
}
}
return
result;
}
ErrorPathSegment
parseServiceErrorPathSegment
(response::Value&& segment)
{
ErrorPathSegment result {
int
{} };
switch
(segment.
type
())
{
case
response::Type::Int:
result = segment.
get
<
int
>();
break
;
case
response::Type::String:
result = segment.
release
<std::string>();
break
;
default
:
break
;
}
return
result;
}
Error
parseServiceError
(response::Value&& error)
{
Error result;
if
(error.
type
() == response::Type::Map)
{
auto
members = error.
release
<response::MapType>();
for
(
auto
& member : members)
{
if
(member.
first
==
"
message
"
sv)
{
if
(member.
second
.
type
() == response::Type::String)
{
result.
message
= member.
second
.
release
<std::string>();
}
continue
;
}
if
(member.
first
==
"
locations
"
sv)
{
if
(member.
second
.
type
() == response::Type::List)
{
auto
locations = member.
second
.
release
<response::ListType>();
result.
locations
.
reserve
(locations.
size
());
std::ranges::transform
(locations,
std::back_inserter
(result.
locations
),
[](response::Value& location) {
return
parseServiceErrorLocation
(
std::move
(location));
});
}
continue
;
}
if
(member.
first
==
"
path
"
sv)
{
if
(member.
second
.
type
() == response::Type::List)
{
auto
segments = member.
second
.
release
<response::ListType>();
result.
path
.
reserve
(segments.
size
());
std::ranges::transform
(segments,
std::back_inserter
(result.
path
),
[](response::Value& segment) {
return
parseServiceErrorPathSegment
(
std::move
(segment));
});
}
continue
;
}
}
}
return
result;
}
ServiceResponse
parseServiceResponse
(response::Value response)
{
ServiceResponse result;
if
(response.
type
() == response::Type::Map)
{
auto
members = response.
release
<response::MapType>();
for
(
auto
& member : members)
{
if
(member.
first
==
"
data
"
sv)
{
//
The generated client code can parse this.
result.
data
=
std::move
(member.
second
);
continue
;
}
if
(member.
first
==
"
errors
"
sv)
{
if
(member.
second
.
type
() == response::Type::List)
{
auto
errors = member.
second
.
release
<response::ListType>();
result.
errors
.
reserve
(errors.
size
());
std::ranges::transform
(errors,
std::back_inserter
(result.
errors
),
[](response::Value& error) {
return
parseServiceError
(
std::move
(error));
});
}
continue
;
}
}
}
return
result;
}
template
<>
response::Value Variable<
int
>::serialize(
int
&& value)
{
return
response::Value { value };
}
template
<>
response::Value Variable<
double
>::serialize(
double
&& value)
{
return
response::Value { value };
}
template
<>
response::Value Variable<std::string>::serialize(std::string&& value)
{
return
response::Value {
std::move
(value) };
}
template
<>
response::Value Variable<
bool
>::serialize(
bool
&& value)
{
return
response::Value { value };
}
template
<>
response::Value Variable<response::Value>::serialize(response::Value&& value)
{
return
response::Value {
std::move
(value) };
}
template
<>
response::Value Variable<response::IdType>::serialize(response::IdType&& value)
{
return
response::Value {
std::move
(value) };
}
template
<>
int
Response<
int
>::parse(response::Value&& value)
{
if
(value.
type
() != response::Type::Int)
{
throw
std::logic_error {
"
not an integer
"
};
}
return
value.
get
<
int
>();
}
template
<>
double
Response<
double
>::parse(response::Value&& value)
{
if
(value.
type
() != response::Type::Float && value.
type
() != response::Type::Int)
{
throw
std::logic_error {
"
not a float
"
};
}
return
value.
get
<
double
>();
}
template
<>
std::string Response<std::string>::parse(response::Value&& value)
{
if
(value.
type
() != response::Type::String)
{
throw
std::logic_error {
"
not a string
"
};
}
return
value.
release
<std::string>();
}
template
<>
bool
Response<
bool
>::parse(response::Value&& value)
{
if
(value.
type
() != response::Type::Boolean)
{
throw
std::logic_error {
"
not a boolean
"
};
}
return
value.
get
<
bool
>();
}
template
<>
response::Value Response<response::Value>::parse(response::Value&& value)
{
return
{
std::move
(value) };
}
template
<>
response::IdType Response<response::IdType>::parse(response::Value&& value)
{
if
(!value.
maybe_id
())
{
throw
std::logic_error {
"
not an ID
"
};
}
return
value.
release
<response::IdType>();
}
}
//
namespace graphql::client
Back
|
FazBrowse Home
|
New Git URL