Skip to content

Commit 8d1a60f

Browse files
committed
Implement type-erased union object types
1 parent 74e4068 commit 8d1a60f

35 files changed

+1048
-182
lines changed

include/SchemaGenerator.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ class Generator
4242
std::string getSourcePath() const noexcept;
4343

4444
bool outputHeader() const noexcept;
45-
void outputInterfaceDeclaration(
46-
std::ostream& headerFile, const InterfaceType& interfaceType) const;
45+
void outputInterfaceDeclaration(std::ostream& headerFile, std::string_view cppType) const;
4746
void outputObjectImplements(std::ostream& headerFile, const ObjectType& objectType) const;
4847
void outputObjectStubs(std::ostream& headerFile, const ObjectType& objectType) const;
4948
void outputObjectDeclaration(
@@ -53,10 +52,10 @@ class Generator
5352
std::string getResolverDeclaration(const OutputField& outputField) const noexcept;
5453

5554
bool outputSource() const noexcept;
56-
void outputInterfaceImplementation(
57-
std::ostream& sourceFile, const InterfaceType& interfaceType) const;
55+
void outputInterfaceImplementation(std::ostream& sourceFile, std::string_view cppType) const;
5856
void outputInterfaceIntrospection(
5957
std::ostream& sourceFile, const InterfaceType& interfaceType) const;
58+
void outputUnionIntrospection(std::ostream& sourceFile, const UnionType& unionType) const;
6059
void outputObjectImplementation(
6160
std::ostream& sourceFile, const ObjectType& objectType, bool isQueryType) const;
6261
void outputObjectIntrospection(std::ostream& sourceFile, const ObjectType& objectType) const;

samples/learn/schema/DroidObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class Droid
218218

219219
Droid(std::unique_ptr<Concept>&& pimpl) noexcept;
220220

221-
// Interface objects need access to these methods
221+
// Interfaces which this type implements
222222
friend Character;
223223

224224
template <class I>

samples/learn/schema/HumanObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class Human
218218

219219
Human(std::unique_ptr<Concept>&& pimpl) noexcept;
220220

221-
// Interface objects need access to these methods
221+
// Interfaces which this type implements
222222
friend Character;
223223

224224
template <class I>

samples/today/TodayMock.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,18 +446,18 @@ TaskState Query::getTestTaskState()
446446
return TaskState::Unassigned;
447447
}
448448

449-
std::vector<std::shared_ptr<service::Object>> Query::getAnyType(
449+
std::vector<std::shared_ptr<object::UnionType>> Query::getAnyType(
450450
const service::FieldParams& params, const std::vector<response::IdType>&)
451451
{
452452
loadAppointments(params.state);
453453

454-
std::vector<std::shared_ptr<service::Object>> result(_appointments.size());
454+
std::vector<std::shared_ptr<object::UnionType>> result(_appointments.size());
455455

456456
std::transform(_appointments.cbegin(),
457457
_appointments.cend(),
458458
result.begin(),
459459
[](const auto& appointment) noexcept {
460-
return std::static_pointer_cast<service::Object>(
460+
return std::make_shared<object::UnionType>(
461461
std::make_shared<object::Appointment>(appointment));
462462
});
463463

samples/today/TodayMock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Query : public std::enable_shared_from_this<Query>
7373
std::shared_ptr<object::NestedType> getNested(service::FieldParams&& params);
7474
std::vector<std::shared_ptr<object::Expensive>> getExpensive();
7575
TaskState getTestTaskState();
76-
std::vector<std::shared_ptr<service::Object>> getAnyType(
76+
std::vector<std::shared_ptr<object::UnionType>> getAnyType(
7777
const service::FieldParams& params, const std::vector<response::IdType>& ids);
7878

7979
private:

samples/today/separate/AppointmentObject.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
#include "TodaySchema.h"
1212

1313
#include "NodeObject.h"
14+
#include "UnionTypeObject.h"
1415

1516
namespace graphql::today::object {
1617
namespace implements {
1718

1819
template <class I>
19-
concept AppointmentIs = std::is_same_v<I, Node>;
20+
concept AppointmentIs = std::is_same_v<I, Node> || std::is_same_v<I, UnionType>;
2021

2122
} // namespace implements
2223

@@ -233,9 +234,12 @@ class Appointment
233234

234235
Appointment(std::unique_ptr<Concept>&& pimpl) noexcept;
235236

236-
// Interface objects need access to these methods
237+
// Interfaces which this type implements
237238
friend Node;
238239

240+
// Unions which include this type
241+
friend UnionType;
242+
239243
template <class I>
240244
static constexpr bool implements() noexcept
241245
{

samples/today/separate/FolderObject.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
#include "TodaySchema.h"
1212

1313
#include "NodeObject.h"
14+
#include "UnionTypeObject.h"
1415

1516
namespace graphql::today::object {
1617
namespace implements {
1718

1819
template <class I>
19-
concept FolderIs = std::is_same_v<I, Node>;
20+
concept FolderIs = std::is_same_v<I, Node> || std::is_same_v<I, UnionType>;
2021

2122
} // namespace implements
2223

@@ -173,9 +174,12 @@ class Folder
173174

174175
Folder(std::unique_ptr<Concept>&& pimpl) noexcept;
175176

176-
// Interface objects need access to these methods
177+
// Interfaces which this type implements
177178
friend Node;
178179

180+
// Unions which include this type
181+
friend UnionType;
182+
179183
template <class I>
180184
static constexpr bool implements() noexcept
181185
{

samples/today/separate/QueryObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ service::AwaitableResolver Query::resolveAnyType(service::ResolverParams&& param
219219
auto result = _pimpl->getAnyType(service::FieldParams(service::SelectionSetParams{ params }, std::move(directives)), std::move(argIds));
220220
resolverLock.unlock();
221221

222-
return service::ModifiedResult<service::Object>::convert<service::TypeModifier::List, service::TypeModifier::Nullable>(std::move(result), std::move(params));
222+
return service::ModifiedResult<UnionType>::convert<service::TypeModifier::List, service::TypeModifier::Nullable>(std::move(result), std::move(params));
223223
}
224224

225225
service::AwaitableResolver Query::resolve_typename(service::ResolverParams&& params) const

samples/today/separate/QueryObject.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ concept getTestTaskState = requires (TImpl impl)
148148
template <class TImpl>
149149
concept getAnyTypeWithParams = requires (TImpl impl, service::FieldParams params, std::vector<response::IdType> idsArg)
150150
{
151-
{ service::FieldResult<std::vector<std::shared_ptr<service::Object>>> { impl.getAnyType(std::move(params), std::move(idsArg)) } };
151+
{ service::FieldResult<std::vector<std::shared_ptr<UnionType>>> { impl.getAnyType(std::move(params), std::move(idsArg)) } };
152152
};
153153

154154
template <class TImpl>
155155
concept getAnyType = requires (TImpl impl, std::vector<response::IdType> idsArg)
156156
{
157-
{ service::FieldResult<std::vector<std::shared_ptr<service::Object>>> { impl.getAnyType(std::move(idsArg)) } };
157+
{ service::FieldResult<std::vector<std::shared_ptr<UnionType>>> { impl.getAnyType(std::move(idsArg)) } };
158158
};
159159

160160
template <class TImpl>
@@ -212,7 +212,7 @@ class Query
212212
virtual service::FieldResult<std::string> getUnimplemented(service::FieldParams&& params) const = 0;
213213
virtual service::FieldResult<std::vector<std::shared_ptr<Expensive>>> getExpensive(service::FieldParams&& params) const = 0;
214214
virtual service::FieldResult<TaskState> getTestTaskState(service::FieldParams&& params) const = 0;
215-
virtual service::FieldResult<std::vector<std::shared_ptr<service::Object>>> getAnyType(service::FieldParams&& params, std::vector<response::IdType>&& idsArg) const = 0;
215+
virtual service::FieldResult<std::vector<std::shared_ptr<UnionType>>> getAnyType(service::FieldParams&& params, std::vector<response::IdType>&& idsArg) const = 0;
216216
};
217217

218218
template <class T>
@@ -400,7 +400,7 @@ class Query
400400
}
401401
}
402402

403-
service::FieldResult<std::vector<std::shared_ptr<service::Object>>> getAnyType(service::FieldParams&& params, std::vector<response::IdType>&& idsArg) const final
403+
service::FieldResult<std::vector<std::shared_ptr<UnionType>>> getAnyType(service::FieldParams&& params, std::vector<response::IdType>&& idsArg) const final
404404
{
405405
if constexpr (methods::QueryHas::getAnyTypeWithParams<T>)
406406
{

samples/today/separate/TaskObject.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
#include "TodaySchema.h"
1212

1313
#include "NodeObject.h"
14+
#include "UnionTypeObject.h"
1415

1516
namespace graphql::today::object {
1617
namespace implements {
1718

1819
template <class I>
19-
concept TaskIs = std::is_same_v<I, Node>;
20+
concept TaskIs = std::is_same_v<I, Node> || std::is_same_v<I, UnionType>;
2021

2122
} // namespace implements
2223

@@ -173,9 +174,12 @@ class Task
173174

174175
Task(std::unique_ptr<Concept>&& pimpl) noexcept;
175176

176-
// Interface objects need access to these methods
177+
// Interfaces which this type implements
177178
friend Node;
178179

180+
// Unions which include this type
181+
friend UnionType;
182+
179183
template <class I>
180184
static constexpr bool implements() noexcept
181185
{

0 commit comments

Comments
 (0)