Skip to content

Commit 8b0102c

Browse files
committed
feat: generalize stitched service in Request::stitch
1 parent 6d55067 commit 8b0102c

File tree

8 files changed

+83
-105
lines changed

8 files changed

+83
-105
lines changed

include/graphqlservice/GraphQLService.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,9 @@ class [[nodiscard("unnecessary construction")]] Request
14441444
GRAPHQLSERVICE_EXPORT virtual ~Request();
14451445

14461446
public:
1447+
[[nodiscard("unnecessary call")]] GRAPHQLSERVICE_EXPORT std::shared_ptr<const Request> stitch(
1448+
const std::shared_ptr<const Request>& added) const;
1449+
14471450
[[nodiscard("unnecessary call")]] GRAPHQLSERVICE_EXPORT std::list<schema_error> validate(
14481451
peg::ast& query) const;
14491452

samples/learn/StarWarsData.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,11 @@ std::shared_ptr<learn::object::Query> MakeQuery() noexcept
115115
std::make_shared<learn::Query>(std::move(heroes), std::move(humans), std::move(droids)));
116116
}
117117

118-
std::shared_ptr<service::Object> GetQueryObject() noexcept
119-
{
120-
return MakeQuery();
121-
}
122-
123118
std::shared_ptr<learn::object::Mutation> MakeMutation() noexcept
124119
{
125120
return std::make_shared<learn::object::Mutation>(std::make_shared<learn::Mutation>());
126121
}
127122

128-
std::shared_ptr<service::Object> GetMutationObject() noexcept
129-
{
130-
return MakeMutation();
131-
}
132-
133-
std::shared_ptr<service::Object> GetSubscriptionObject() noexcept
134-
{
135-
return {};
136-
}
137-
138123
std::shared_ptr<service::Request> GetService() noexcept
139124
{
140125
return std::make_shared<learn::Operations>(MakeQuery(),

samples/learn/StarWarsData.h

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

1111
namespace graphql::star_wars {
1212

13-
std::shared_ptr<service::Object> GetQueryObject() noexcept;
14-
std::shared_ptr<service::Object> GetMutationObject() noexcept;
15-
std::shared_ptr<service::Object> GetSubscriptionObject() noexcept;
16-
1713
std::shared_ptr<service::Request> GetService() noexcept;
1814

1915
} // namespace graphql::star_wars

samples/stitched/StitchedSchema.cpp

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -10,83 +10,9 @@ import GraphQL.Today.Mock;
1010

1111
namespace graphql::stitched {
1212

13-
std::shared_ptr<schema::Schema> GetSchema()
13+
std::shared_ptr<const service::Request> GetService()
1414
{
15-
static std::weak_ptr<schema::Schema> s_wpSchema;
16-
auto schema = s_wpSchema.lock();
17-
18-
if (!schema)
19-
{
20-
auto learnSchema = learn::GetSchema();
21-
auto todaySchema = today::GetSchema();
22-
schema = learnSchema->StitchSchema(todaySchema);
23-
s_wpSchema = schema;
24-
}
25-
26-
return schema;
27-
}
28-
29-
class Operations final : public service::Request
30-
{
31-
public:
32-
explicit Operations(std::shared_ptr<service::Object> query,
33-
std::shared_ptr<service::Object> mutation, std::shared_ptr<service::Object> subscription);
34-
35-
private:
36-
std::shared_ptr<service::Object> _query;
37-
std::shared_ptr<service::Object> _mutation;
38-
std::shared_ptr<service::Object> _subscription;
39-
};
40-
41-
Operations::Operations(std::shared_ptr<service::Object> query,
42-
std::shared_ptr<service::Object> mutation, std::shared_ptr<service::Object> subscription)
43-
: service::Request(
44-
{
45-
{ service::strQuery, query },
46-
{ service::strMutation, mutation },
47-
{ service::strSubscription, subscription },
48-
},
49-
GetSchema())
50-
, _query(std::move(query))
51-
, _mutation(std::move(mutation))
52-
, _subscription(std::move(subscription))
53-
{
54-
}
55-
56-
std::shared_ptr<service::Request> GetService()
57-
{
58-
auto learnQuery = star_wars::GetQueryObject();
59-
auto todayQuery = std::static_pointer_cast<service::Object>(
60-
std::make_shared<today::object::Query>(today::mock_query(today::mock_service())));
61-
auto stitchedQuery = learnQuery->StitchObject(todayQuery);
62-
63-
auto learnMutation = star_wars::GetMutationObject();
64-
auto todayMutation = std::static_pointer_cast<service::Object>(
65-
std::make_shared<today::object::Mutation>(today::mock_mutation()));
66-
auto stitchedMutation = learnMutation->StitchObject(todayMutation);
67-
68-
auto learnSubscription = star_wars::GetSubscriptionObject();
69-
auto todaySubscription = std::static_pointer_cast<service::Object>(
70-
std::make_shared<today::object::Subscription>(today::mock_subscription()));
71-
std::shared_ptr<service::Object> stitchedSubscription;
72-
73-
if (learnSubscription)
74-
{
75-
if (todaySubscription)
76-
{
77-
stitchedSubscription = learnSubscription->StitchObject(todaySubscription);
78-
}
79-
else
80-
{
81-
stitchedSubscription = learnSubscription;
82-
}
83-
}
84-
else
85-
{
86-
stitchedSubscription = todaySubscription;
87-
}
88-
89-
return std::make_shared<Operations>(stitchedQuery, stitchedMutation, stitchedSubscription);
15+
return star_wars::GetService()->stitch(today::mock_service()->service);
9016
}
9117

9218
} // namespace graphql::stitched

samples/stitched/StitchedSchema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace graphql::stitched {
1515

16-
std::shared_ptr<service::Request> GetService();
16+
std::shared_ptr<const service::Request> GetService();
1717

1818
} // namespace graphql::stitched
1919

samples/today/TodayMock.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ class Query : public std::enable_shared_from_this<Query>
119119
std::vector<std::shared_ptr<Folder>> _unreadCounts;
120120
};
121121

122-
std::shared_ptr<Query> mock_query(const std::shared_ptr<TodayMockService>& service) noexcept;
123-
124122
class PageInfo
125123
{
126124
public:
@@ -303,8 +301,6 @@ class Mutation
303301
static std::optional<double> _setFloat;
304302
};
305303

306-
std::shared_ptr<Mutation> mock_mutation() noexcept;
307-
308304
class Subscription
309305
{
310306
public:
@@ -336,8 +332,6 @@ class NextAppointmentChange
336332
static std::size_t _notifyUnsubscribeCount;
337333
};
338334

339-
std::shared_ptr<NextAppointmentChange> mock_subscription() noexcept;
340-
341335
class NodeChange
342336
{
343337
public:

samples/today/TodayMock.ixx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ using today::mock_service;
2222
using today::RequestState;
2323

2424
using today::Query;
25-
using today::mock_query;
2625

2726
using today::PageInfo;
2827
using today::Appointment;
@@ -35,10 +34,8 @@ using today::FolderEdge;
3534
using today::FolderConnection;
3635
using today::CompleteTaskPayload;
3736
using today::Mutation;
38-
using today::mock_mutation;
3937
using today::Subscription;
4038
using today::NextAppointmentChange;
41-
using today::mock_subscription;
4239
using today::NodeChange;
4340
using today::CapturedParams;
4441
using today::NestedType;

src/GraphQLService.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,7 @@ void SubscriptionDefinitionVisitor::visitInlineFragment(const peg::ast_node& inl
17351735

17361736
Request::Request(TypeMap operationTypes, std::shared_ptr<schema::Schema> schema)
17371737
: _operations(std::move(operationTypes))
1738+
, _schema(schema)
17381739
, _validation(std::make_unique<ValidateExecutableVisitor>(std::move(schema)))
17391740
{
17401741
}
@@ -1746,6 +1747,82 @@ Request::~Request()
17461747
// forward declaration of the class.
17471748
}
17481749

1750+
std::shared_ptr<const Request> Request::stitch(const std::shared_ptr<const Request>& added) const
1751+
{
1752+
TypeMap operations;
1753+
auto itrOriginalQuery = _operations.find(strQuery);
1754+
auto itrAddedQuery = added->_operations.find(strQuery);
1755+
1756+
if (itrOriginalQuery != _operations.end() && itrOriginalQuery->second)
1757+
{
1758+
if (itrAddedQuery != added->_operations.end() && itrAddedQuery->second)
1759+
{
1760+
operations.emplace(strQuery,
1761+
itrOriginalQuery->second->StitchObject(itrAddedQuery->second));
1762+
}
1763+
else
1764+
{
1765+
operations.emplace(strQuery, itrOriginalQuery->second);
1766+
}
1767+
}
1768+
else if (itrAddedQuery != added->_operations.end() && itrAddedQuery->second)
1769+
{
1770+
operations.emplace(strQuery, itrAddedQuery->second);
1771+
}
1772+
1773+
auto itrOriginalMutation = _operations.find(strMutation);
1774+
auto itrAddedMutation = added->_operations.find(strMutation);
1775+
1776+
if (itrOriginalMutation != _operations.end() && itrOriginalMutation->second)
1777+
{
1778+
if (itrAddedMutation != added->_operations.end() && itrAddedMutation->second)
1779+
{
1780+
operations.emplace(strMutation,
1781+
itrOriginalMutation->second->StitchObject(itrAddedMutation->second));
1782+
}
1783+
else
1784+
{
1785+
operations.emplace(strMutation, itrOriginalMutation->second);
1786+
}
1787+
}
1788+
else if (itrAddedMutation != added->_operations.end() && itrAddedMutation->second)
1789+
{
1790+
operations.emplace(strMutation, itrAddedMutation->second);
1791+
}
1792+
1793+
auto itrOriginalSubscription = _operations.find(strSubscription);
1794+
auto itrAddedSubscription = added->_operations.find(strSubscription);
1795+
1796+
if (itrOriginalSubscription != _operations.end() && itrOriginalSubscription->second)
1797+
{
1798+
if (itrAddedSubscription != added->_operations.end() && itrAddedSubscription->second)
1799+
{
1800+
operations.emplace(strSubscription,
1801+
itrOriginalSubscription->second->StitchObject(itrAddedSubscription->second));
1802+
}
1803+
else
1804+
{
1805+
operations.emplace(strSubscription, itrOriginalSubscription->second);
1806+
}
1807+
}
1808+
else if (itrAddedSubscription != added->_operations.end() && itrAddedSubscription->second)
1809+
{
1810+
operations.emplace(strSubscription, itrAddedSubscription->second);
1811+
}
1812+
1813+
class StitchedRequest : public Request
1814+
{
1815+
public:
1816+
StitchedRequest(TypeMap operations, std::shared_ptr<schema::Schema> schema)
1817+
: Request { std::move(operations), std::move(schema) }
1818+
{
1819+
}
1820+
};
1821+
1822+
return std::make_shared<StitchedRequest>(std::move(operations),
1823+
_schema->StitchSchema(added->_schema));
1824+
}
1825+
17491826
std::list<schema_error> Request::validate(peg::ast& query) const
17501827
{
17511828
std::list<schema_error> errors;

0 commit comments

Comments
 (0)