Skip to content

Commit 09a4e80

Browse files
committed
Convert validation schema to separate files
1 parent e6f61a1 commit 09a4e80

36 files changed

+3917
-3198
lines changed

samples/validation/ValidationMock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#include "ValidationSchema.h"
1010

11+
#include "QueryObject.h"
12+
#include "MutationObject.h"
13+
#include "SubscriptionObject.h"
14+
1115
namespace graphql::validation {
1216

1317
class Query
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
// WARNING! Do not edit this file manually, your changes will be overwritten.
5+
6+
#include "AlienObject.h"
7+
8+
#include "graphqlservice/introspection/Introspection.h"
9+
10+
#include <algorithm>
11+
#include <functional>
12+
#include <sstream>
13+
#include <stdexcept>
14+
#include <unordered_map>
15+
16+
using namespace std::literals;
17+
18+
namespace graphql::validation {
19+
namespace object {
20+
21+
Alien::Alien(std::unique_ptr<Concept>&& pimpl) noexcept
22+
: service::Object{ getTypeNames(), getResolvers() }
23+
, _pimpl { std::move(pimpl) }
24+
{
25+
}
26+
27+
service::TypeNames Alien::getTypeNames() const noexcept
28+
{
29+
return {
30+
R"gql(Sentient)gql"sv,
31+
R"gql(HumanOrAlien)gql"sv,
32+
R"gql(Alien)gql"sv
33+
};
34+
}
35+
36+
service::ResolverMap Alien::getResolvers() const noexcept
37+
{
38+
return {
39+
{ R"gql(name)gql"sv, [this](service::ResolverParams&& params) { return resolveName(std::move(params)); } },
40+
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } },
41+
{ R"gql(homePlanet)gql"sv, [this](service::ResolverParams&& params) { return resolveHomePlanet(std::move(params)); } }
42+
};
43+
}
44+
45+
void Alien::beginSelectionSet(const service::SelectionSetParams& params) const
46+
{
47+
_pimpl->beginSelectionSet(params);
48+
}
49+
50+
void Alien::endSelectionSet(const service::SelectionSetParams& params) const
51+
{
52+
_pimpl->endSelectionSet(params);
53+
}
54+
55+
service::AwaitableResolver Alien::resolveName(service::ResolverParams&& params) const
56+
{
57+
std::unique_lock resolverLock(_resolverMutex);
58+
auto directives = std::move(params.fieldDirectives);
59+
auto result = _pimpl->getName(service::FieldParams(service::SelectionSetParams{ params }, std::move(directives)));
60+
resolverLock.unlock();
61+
62+
return service::ModifiedResult<std::string>::convert(std::move(result), std::move(params));
63+
}
64+
65+
service::AwaitableResolver Alien::resolveHomePlanet(service::ResolverParams&& params) const
66+
{
67+
std::unique_lock resolverLock(_resolverMutex);
68+
auto directives = std::move(params.fieldDirectives);
69+
auto result = _pimpl->getHomePlanet(service::FieldParams(service::SelectionSetParams{ params }, std::move(directives)));
70+
resolverLock.unlock();
71+
72+
return service::ModifiedResult<std::string>::convert<service::TypeModifier::Nullable>(std::move(result), std::move(params));
73+
}
74+
75+
service::AwaitableResolver Alien::resolve_typename(service::ResolverParams&& params) const
76+
{
77+
return service::ModifiedResult<std::string>::convert(std::string{ R"gql(Alien)gql" }, std::move(params));
78+
}
79+
80+
} // namespace object
81+
82+
void AddAlienDetails(const std::shared_ptr<schema::ObjectType>& typeAlien, const std::shared_ptr<schema::Schema>& schema)
83+
{
84+
typeAlien->AddInterfaces({
85+
std::static_pointer_cast<const schema::InterfaceType>(schema->LookupType(R"gql(Sentient)gql"sv))
86+
});
87+
typeAlien->AddFields({
88+
schema::Field::Make(R"gql(name)gql"sv, R"md()md"sv, std::nullopt, schema->WrapType(introspection::TypeKind::NON_NULL, schema->LookupType(R"gql(String)gql"sv))),
89+
schema::Field::Make(R"gql(homePlanet)gql"sv, R"md()md"sv, std::nullopt, schema->LookupType(R"gql(String)gql"sv))
90+
});
91+
}
92+
93+
} // namespace graphql::validation
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
// WARNING! Do not edit this file manually, your changes will be overwritten.
5+
6+
#pragma once
7+
8+
#ifndef ALIENOBJECT_H
9+
#define ALIENOBJECT_H
10+
11+
#include "ValidationSchema.h"
12+
13+
namespace graphql::validation::object {
14+
namespace implements {
15+
16+
template <class I>
17+
concept AlienIs = std::is_same_v<I, Sentient> || std::is_same_v<I, HumanOrAlien>;
18+
19+
} // namespace implements
20+
21+
namespace methods::AlienHas {
22+
23+
template <class TImpl>
24+
concept getNameWithParams = requires (TImpl impl, service::FieldParams params)
25+
{
26+
{ service::FieldResult<std::string> { impl.getName(std::move(params)) } };
27+
};
28+
29+
template <class TImpl>
30+
concept getName = requires (TImpl impl)
31+
{
32+
{ service::FieldResult<std::string> { impl.getName() } };
33+
};
34+
35+
template <class TImpl>
36+
concept getHomePlanetWithParams = requires (TImpl impl, service::FieldParams params)
37+
{
38+
{ service::FieldResult<std::optional<std::string>> { impl.getHomePlanet(std::move(params)) } };
39+
};
40+
41+
template <class TImpl>
42+
concept getHomePlanet = requires (TImpl impl)
43+
{
44+
{ service::FieldResult<std::optional<std::string>> { impl.getHomePlanet() } };
45+
};
46+
47+
template <class TImpl>
48+
concept beginSelectionSet = requires (TImpl impl, const service::SelectionSetParams params)
49+
{
50+
{ impl.beginSelectionSet(params) };
51+
};
52+
53+
template <class TImpl>
54+
concept endSelectionSet = requires (TImpl impl, const service::SelectionSetParams params)
55+
{
56+
{ impl.endSelectionSet(params) };
57+
};
58+
59+
} // namespace methods::AlienHas
60+
61+
class Alien
62+
: public service::Object
63+
{
64+
private:
65+
service::AwaitableResolver resolveName(service::ResolverParams&& params) const;
66+
service::AwaitableResolver resolveHomePlanet(service::ResolverParams&& params) const;
67+
68+
service::AwaitableResolver resolve_typename(service::ResolverParams&& params) const;
69+
70+
struct Concept
71+
{
72+
virtual ~Concept() = default;
73+
74+
virtual void beginSelectionSet(const service::SelectionSetParams& params) const = 0;
75+
virtual void endSelectionSet(const service::SelectionSetParams& params) const = 0;
76+
77+
virtual service::FieldResult<std::string> getName(service::FieldParams&& params) const = 0;
78+
virtual service::FieldResult<std::optional<std::string>> getHomePlanet(service::FieldParams&& params) const = 0;
79+
};
80+
81+
template <class T>
82+
struct Model
83+
: Concept
84+
{
85+
Model(std::shared_ptr<T>&& pimpl) noexcept
86+
: _pimpl { std::move(pimpl) }
87+
{
88+
}
89+
90+
service::FieldResult<std::string> getName(service::FieldParams&& params) const final
91+
{
92+
if constexpr (methods::AlienHas::getNameWithParams<T>)
93+
{
94+
return { _pimpl->getName(std::move(params)) };
95+
}
96+
else if constexpr (methods::AlienHas::getName<T>)
97+
{
98+
return { _pimpl->getName() };
99+
}
100+
else
101+
{
102+
throw std::runtime_error(R"ex(Alien::getName is not implemented)ex");
103+
}
104+
}
105+
106+
service::FieldResult<std::optional<std::string>> getHomePlanet(service::FieldParams&& params) const final
107+
{
108+
if constexpr (methods::AlienHas::getHomePlanetWithParams<T>)
109+
{
110+
return { _pimpl->getHomePlanet(std::move(params)) };
111+
}
112+
else if constexpr (methods::AlienHas::getHomePlanet<T>)
113+
{
114+
return { _pimpl->getHomePlanet() };
115+
}
116+
else
117+
{
118+
throw std::runtime_error(R"ex(Alien::getHomePlanet is not implemented)ex");
119+
}
120+
}
121+
122+
void beginSelectionSet(const service::SelectionSetParams& params) const final
123+
{
124+
if constexpr (methods::AlienHas::beginSelectionSet<T>)
125+
{
126+
_pimpl->beginSelectionSet(params);
127+
}
128+
}
129+
130+
void endSelectionSet(const service::SelectionSetParams& params) const final
131+
{
132+
if constexpr (methods::AlienHas::endSelectionSet<T>)
133+
{
134+
_pimpl->endSelectionSet(params);
135+
}
136+
}
137+
138+
private:
139+
const std::shared_ptr<T> _pimpl;
140+
};
141+
142+
Alien(std::unique_ptr<Concept>&& pimpl) noexcept;
143+
144+
// Interfaces which this type implements
145+
friend Sentient;
146+
147+
// Unions which include this type
148+
friend HumanOrAlien;
149+
150+
template <class I>
151+
static constexpr bool implements() noexcept
152+
{
153+
return implements::AlienIs<I>;
154+
}
155+
156+
service::TypeNames getTypeNames() const noexcept;
157+
service::ResolverMap getResolvers() const noexcept;
158+
159+
void beginSelectionSet(const service::SelectionSetParams& params) const final;
160+
void endSelectionSet(const service::SelectionSetParams& params) const final;
161+
162+
const std::unique_ptr<Concept> _pimpl;
163+
164+
public:
165+
template <class T>
166+
Alien(std::shared_ptr<T> pimpl) noexcept
167+
: Alien { std::unique_ptr<Concept> { std::make_unique<Model<T>>(std::move(pimpl)) } }
168+
{
169+
}
170+
};
171+
172+
} // namespace graphql::validation::object
173+
174+
#endif // ALIENOBJECT_H

0 commit comments

Comments
 (0)