-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStarWarsSchema.cpp
224 lines (179 loc) · 6.49 KB
/
StarWarsSchema.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
// 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 "QueryObject.h"
#include "MutationObject.h"
#include "graphqlservice/internal/Schema.h"
#include "graphqlservice/introspection/IntrospectionSchema.h"
#include <algorithm>
#include <array>
#include <functional>
#include <sstream>
#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" } };
}
response::Value resolvedResult(response::Type::EnumValue);
resolvedResult.set<std::string>(std::string { s_namesEpisode[idx] });
return resolvedResult;
});
}
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;
}
Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation)
: service::Request({
{ service::strQuery, query },
{ service::strMutation, mutation }
}, GetSchema())
, _query(std::move(query))
, _mutation(std::move(mutation))
{
}
void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema)
{
auto typeEpisode = schema::EnumType::Make(R"gql(Episode)gql"sv, R"md()md"sv);
schema->AddType(R"gql(Episode)gql"sv, typeEpisode);
auto typeReviewInput = schema::InputObjectType::Make(R"gql(ReviewInput)gql"sv, R"md()md"sv);
schema->AddType(R"gql(ReviewInput)gql"sv, typeReviewInput);
auto typeCharacter = schema::InterfaceType::Make(R"gql(Character)gql"sv, R"md()md"sv);
schema->AddType(R"gql(Character)gql"sv, typeCharacter);
auto typeHuman = schema::ObjectType::Make(R"gql(Human)gql"sv, R"md()md"sv);
schema->AddType(R"gql(Human)gql"sv, typeHuman);
auto typeDroid = schema::ObjectType::Make(R"gql(Droid)gql"sv, R"md()md"sv);
schema->AddType(R"gql(Droid)gql"sv, typeDroid);
auto typeQuery = schema::ObjectType::Make(R"gql(Query)gql"sv, R"md()md"sv);
schema->AddType(R"gql(Query)gql"sv, typeQuery);
auto typeReview = schema::ObjectType::Make(R"gql(Review)gql"sv, R"md()md"sv);
schema->AddType(R"gql(Review)gql"sv, typeReview);
auto typeMutation = schema::ObjectType::Make(R"gql(Mutation)gql"sv, R"md()md"sv);
schema->AddType(R"gql(Mutation)gql"sv, typeMutation);
typeEpisode->AddEnumValues({
{ service::s_namesEpisode[static_cast<size_t>(learn::Episode::NEW_HOPE)], R"md()md"sv, std::nullopt },
{ service::s_namesEpisode[static_cast<size_t>(learn::Episode::EMPIRE)], R"md()md"sv, std::nullopt },
{ service::s_namesEpisode[static_cast<size_t>(learn::Episode::JEDI)], R"md()md"sv, std::nullopt }
});
typeReviewInput->AddInputValues({
schema::InputValue::Make(R"gql(stars)gql"sv, R"md()md"sv, schema->WrapType(introspection::TypeKind::NON_NULL, schema->LookupType(R"gql(Int)gql"sv)), R"gql()gql"sv),
schema::InputValue::Make(R"gql(commentary)gql"sv, R"md()md"sv, schema->LookupType(R"gql(String)gql"sv), R"gql()gql"sv)
});
AddCharacterDetails(typeCharacter, schema);
AddHumanDetails(typeHuman, schema);
AddDroidDetails(typeDroid, schema);
AddQueryDetails(typeQuery, schema);
AddReviewDetails(typeReview, schema);
AddMutationDetails(typeMutation, schema);
schema->AddQueryType(typeQuery);
schema->AddMutationType(typeMutation);
}
std::shared_ptr<schema::Schema> GetSchema()
{
static std::weak_ptr<schema::Schema> s_wpSchema;
auto schema = s_wpSchema.lock();
if (!schema)
{
schema = std::make_shared<schema::Schema>(false, R"md()md"sv);
introspection::AddTypesToSchema(schema);
AddTypesToSchema(schema);
s_wpSchema = schema;
}
return schema;
}
} // namespace learn
} // namespace graphql