forked from microsoft/cppgraphqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStarWarsSharedTypes.cpp
More file actions
149 lines (117 loc) · 3.66 KB
/
StarWarsSharedTypes.cpp
File metadata and controls
149 lines (117 loc) · 3.66 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// WARNING! Do not edit this file manually, your changes will be overwritten.
#include "graphqlservice/GraphQLService.h"
#include "StarWarsSharedTypes.h"
#include <algorithm>
#include <array>
#include <cstddef>
#include <functional>
#include <stdexcept>
#include <string_view>
#include <utility>
#include <vector>
using namespace std::literals;
namespace graphql {
namespace service {
static const auto s_namesEpisode = learn::getEpisodeNames();
static const auto s_valuesEpisode = learn::getEpisodeValues();
template <>
learn::Episode Argument<learn::Episode>::convert(const response::Value& value)
{
if (!value.maybe_enum())
{
throw service::schema_exception { { R"ex(not a valid Episode value)ex" } };
}
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
s_valuesEpisode,
std::string_view { value.get<std::string>() });
if (!result)
{
throw service::schema_exception { { R"ex(not a valid Episode value)ex" } };
}
return *result;
}
template <>
service::AwaitableResolver Result<learn::Episode>::convert(service::AwaitableScalar<learn::Episode> result, ResolverParams&& params)
{
return ModifiedResult<learn::Episode>::resolve(std::move(result), std::move(params),
[](learn::Episode value, const ResolverParams&)
{
const auto idx = static_cast<size_t>(value);
if (idx >= s_namesEpisode.size())
{
throw service::schema_exception { { R"ex(Enum value out of range for Episode)ex" } };
}
return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesEpisode[idx] } } } };
});
}
template <>
void Result<learn::Episode>::validateScalar(const response::Value& value)
{
if (!value.maybe_enum())
{
throw service::schema_exception { { R"ex(not a valid Episode value)ex" } };
}
const auto [itr, itrEnd] = internal::sorted_map_equal_range<internal::shorter_or_less>(
s_valuesEpisode.begin(),
s_valuesEpisode.end(),
std::string_view { value.get<std::string>() });
if (itr == itrEnd)
{
throw service::schema_exception { { R"ex(not a valid Episode value)ex" } };
}
}
template <>
learn::ReviewInput Argument<learn::ReviewInput>::convert(const response::Value& value)
{
auto valueStars = service::ModifiedArgument<int>::require("stars", value);
auto valueCommentary = service::ModifiedArgument<std::string>::require<service::TypeModifier::Nullable>("commentary", value);
return learn::ReviewInput {
valueStars,
std::move(valueCommentary)
};
}
} // namespace service
namespace learn {
ReviewInput::ReviewInput() noexcept
: stars {}
, commentary {}
{
// Explicit definition to prevent ODR violations when LTO is enabled.
}
ReviewInput::ReviewInput(
int starsArg,
std::optional<std::string> commentaryArg) noexcept
: stars { std::move(starsArg) }
, commentary { std::move(commentaryArg) }
{
}
ReviewInput::ReviewInput(const ReviewInput& other)
: stars { service::ModifiedArgument<int>::duplicate(other.stars) }
, commentary { service::ModifiedArgument<std::string>::duplicate<service::TypeModifier::Nullable>(other.commentary) }
{
}
ReviewInput::ReviewInput(ReviewInput&& other) noexcept
: stars { std::move(other.stars) }
, commentary { std::move(other.commentary) }
{
}
ReviewInput::~ReviewInput()
{
// Explicit definition to prevent ODR violations when LTO is enabled.
}
ReviewInput& ReviewInput::operator=(const ReviewInput& other)
{
ReviewInput value { other };
std::swap(*this, value);
return *this;
}
ReviewInput& ReviewInput::operator=(ReviewInput&& other) noexcept
{
stars = std::move(other.stars);
commentary = std::move(other.commentary);
return *this;
}
} // namespace learn
} // namespace graphql