Skip to content

Commit 3e97396

Browse files
committed
Move RequestDeliverParams field name out of SubscriptionFilter
1 parent e46b572 commit 3e97396

File tree

4 files changed

+63
-62
lines changed

4 files changed

+63
-62
lines changed

include/graphqlservice/GraphQLService.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,9 +1233,6 @@ using SubscriptionDirectiveFilterCallback = std::function<bool(Directives::const
12331233

12341234
struct SubscriptionFilter
12351235
{
1236-
// Deliver to subscriptions on this field.
1237-
std::string_view field;
1238-
12391236
// Optional field argument filter, which can either be a set of required arguments, or a
12401237
// callback which returns true if the arguments match custom criteria.
12411238
std::optional<std::variant<SubscriptionArguments, SubscriptionArgumentFilterCallback>>
@@ -1252,8 +1249,11 @@ using RequestDeliverFilter = std::optional<std::variant<SubscriptionKey, Subscri
12521249

12531250
struct RequestDeliverParams
12541251
{
1252+
// Deliver to subscriptions on this field.
1253+
std::string_view field;
1254+
12551255
// Optional filter to control which subscriptions will receive the event. If not specified,
1256-
// every subscription will receive the event and evaluate their queries against it.
1256+
// every subscription on this field will receive the event and evaluate their queries.
12571257
RequestDeliverFilter filter;
12581258

12591259
// Optional async execution awaitable.
@@ -1329,7 +1329,7 @@ class Request : public std::enable_shared_from_this<Request>
13291329
SubscriptionKey addSubscription(RequestSubscribeParams&& params);
13301330
void removeSubscription(SubscriptionKey key);
13311331
std::vector<std::shared_ptr<SubscriptionData>> collectRegistrations(
1332-
RequestDeliverFilter&& filter) const noexcept;
1332+
std::string_view field, RequestDeliverFilter&& filter) const noexcept;
13331333

13341334
const TypeMap _operations;
13351335
std::unique_ptr<ValidateExecutableVisitor> _validation;

src/GraphQLService.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,7 @@ AwaitableDeliver Request::deliver(RequestDeliverParams params) const
18371837
throw std::invalid_argument("Missing subscriptionObject");
18381838
}
18391839

1840-
const auto registrations = collectRegistrations(std::move(params.filter));
1840+
const auto registrations = collectRegistrations(params.field, std::move(params.filter));
18411841

18421842
if (registrations.empty())
18431843
{
@@ -1987,38 +1987,41 @@ void Request::removeSubscription(SubscriptionKey key)
19871987
}
19881988

19891989
std::vector<std::shared_ptr<SubscriptionData>> Request::collectRegistrations(
1990-
RequestDeliverFilter&& filter) const noexcept
1990+
std::string_view field, RequestDeliverFilter&& filter) const noexcept
19911991
{
19921992
std::vector<std::shared_ptr<SubscriptionData>> registrations;
1993+
const auto itrListeners = _listeners.find(field);
19931994

1994-
if (!filter)
1995+
if (itrListeners != _listeners.end())
19951996
{
1996-
// Return all of the registered subscriptions.
1997-
registrations.reserve(_subscriptions.size());
1998-
std::transform(_subscriptions.begin(),
1999-
_subscriptions.end(),
2000-
std::back_inserter(registrations),
2001-
[](const auto& entry) noexcept {
2002-
return entry.second;
2003-
});
2004-
}
2005-
else if (std::holds_alternative<SubscriptionKey>(*filter))
2006-
{
2007-
// Return the specific subscription for this key.
2008-
const auto itr = _subscriptions.find(std::get<SubscriptionKey>(*filter));
2009-
2010-
if (itr != _subscriptions.end())
1997+
if (!filter)
20111998
{
2012-
registrations.push_back(itr->second);
1999+
// Return all of the registered subscriptions for this field.
2000+
registrations.reserve(itrListeners->second.size());
2001+
std::transform(itrListeners->second.begin(),
2002+
itrListeners->second.end(),
2003+
std::back_inserter(registrations),
2004+
[this](const auto& key) noexcept {
2005+
const auto itr = _subscriptions.find(key);
2006+
2007+
return itr == _subscriptions.end() ? std::shared_ptr<SubscriptionData> {}
2008+
: itr->second;
2009+
});
20132010
}
2014-
}
2015-
else if (std::holds_alternative<SubscriptionFilter>(*filter))
2016-
{
2017-
auto& subscriptionFilter = std::get<SubscriptionFilter>(*filter);
2018-
const auto itrListeners = _listeners.find(subscriptionFilter.field);
2011+
else if (std::holds_alternative<SubscriptionKey>(*filter))
2012+
{
2013+
// Return the specific subscription for this key.
2014+
const auto itr = _subscriptions.find(std::get<SubscriptionKey>(*filter));
20192015

2020-
if (itrListeners != _listeners.end())
2016+
if (itr != _subscriptions.end() && itr->second->field == field)
2017+
{
2018+
registrations.push_back(itr->second);
2019+
}
2020+
}
2021+
else if (std::holds_alternative<SubscriptionFilter>(*filter))
20212022
{
2023+
auto& subscriptionFilter = std::get<SubscriptionFilter>(*filter);
2024+
20222025
registrations.reserve(itrListeners->second.size());
20232026

20242027
std::optional<SubscriptionArgumentFilterCallback> argumentsMatch;

test/ClientTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ TEST_F(ClientCase, SubscribeNextAppointmentChangeDefault)
247247
{},
248248
state })
249249
.get();
250-
_service->deliver({ { service::SubscriptionFilter { "nextAppointmentChange"sv } } }).get();
250+
_service->deliver({ "nextAppointmentChange"sv }).get();
251251
_service->unsubscribe({ key }).get();
252252

253253
try

test/TodayTests.cpp

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ TEST_F(TodayServiceCase, SubscribeNextAppointmentChangeDefault)
618618
{},
619619
state })
620620
.get();
621-
_service->deliver({ { service::SubscriptionFilter { "nextAppointmentChange"sv } } }).get();
621+
_service->deliver({ "nextAppointmentChange"sv }).get();
622622
_service->unsubscribe({ key }).get();
623623

624624
try
@@ -682,8 +682,9 @@ TEST_F(TodayServiceCase, SubscribeNextAppointmentChangeOverride)
682682
state })
683683
.get();
684684
_service
685-
->deliver({ { service::SubscriptionFilter { "nextAppointmentChange"sv } },
686-
{},
685+
->deliver({ "nextAppointmentChange"sv,
686+
{}, // filter
687+
{}, // launch
687688
std::make_shared<today::object::Subscription>(std::move(subscriptionObject)) })
688689
.get();
689690
_service->unsubscribe({ key }).get();
@@ -722,7 +723,7 @@ TEST_F(TodayServiceCase, DeliverNextAppointmentChangeNoSubscriptionObject)
722723

723724
try
724725
{
725-
service->deliver({ { service::SubscriptionFilter { "nextAppointmentChange"sv } } }).get();
726+
service->deliver({ "nextAppointmentChange"sv }).get();
726727
}
727728
catch (const std::invalid_argument& ex)
728729
{
@@ -740,7 +741,7 @@ TEST_F(TodayServiceCase, DeliverNextAppointmentChangeNoSubscriptionSupport)
740741

741742
try
742743
{
743-
service->deliver({ { service::SubscriptionFilter { "nextAppointmentChange"sv } } }).get();
744+
service->deliver({ "nextAppointmentChange"sv }).get();
744745
}
745746
catch (const std::logic_error& ex)
746747
{
@@ -1269,10 +1270,10 @@ TEST_F(TodayServiceCase, SubscribeNodeChangeMatchingId)
12691270
state })
12701271
.get();
12711272
_service
1272-
->deliver({ { service::SubscriptionFilter { "nodeChange"sv,
1273-
{ service::SubscriptionArguments {
1274-
{ "id", response::Value("ZmFrZVRhc2tJZA=="s) } } } } },
1275-
{},
1273+
->deliver({ "nodeChange"sv,
1274+
{ service::SubscriptionFilter { { service::SubscriptionArguments {
1275+
{ "id", response::Value("ZmFrZVRhc2tJZA=="s) } } } } },
1276+
{}, // launch
12761277
std::make_shared<today::object::Subscription>(std::move(subscriptionObject)) })
12771278
.get();
12781279
_service->unsubscribe({ key }).get();
@@ -1330,10 +1331,10 @@ TEST_F(TodayServiceCase, SubscribeNodeChangeMismatchedId)
13301331
std::move(variables) })
13311332
.get();
13321333
_service
1333-
->deliver({ { service::SubscriptionFilter { "nodeChange"sv,
1334-
{ service::SubscriptionArguments {
1335-
{ "id", response::Value("ZmFrZUFwcG9pbnRtZW50SWQ="s) } } } } },
1336-
{},
1334+
->deliver({ "nodeChange"sv,
1335+
{ service::SubscriptionFilter { { service::SubscriptionArguments {
1336+
{ "id", response::Value("ZmFrZUFwcG9pbnRtZW50SWQ="s) } } } } },
1337+
{}, // launch
13371338
std::make_shared<today::object::Subscription>(std::move(subscriptionObject)) })
13381339
.get();
13391340
_service->unsubscribe({ key }).get();
@@ -1396,11 +1397,11 @@ TEST_F(TodayServiceCase, SubscribeNodeChangeFuzzyComparator)
13961397
state })
13971398
.get();
13981399
_service
1399-
->deliver(
1400-
{ { service::SubscriptionFilter { "nodeChange"sv,
1401-
{ service::SubscriptionArgumentFilterCallback { std::move(filterCallback) } } } },
1402-
{},
1403-
std::make_shared<today::object::Subscription>(std::move(subscriptionObject)) })
1400+
->deliver({ "nodeChange"sv,
1401+
{ service::SubscriptionFilter {
1402+
{ service::SubscriptionArgumentFilterCallback { std::move(filterCallback) } } } },
1403+
{}, // launch
1404+
std::make_shared<today::object::Subscription>(std::move(subscriptionObject)) })
14041405
.get();
14051406
_service->unsubscribe({ key }).get();
14061407

@@ -1467,11 +1468,11 @@ TEST_F(TodayServiceCase, SubscribeNodeChangeFuzzyMismatch)
14671468
std::move(variables) })
14681469
.get();
14691470
_service
1470-
->deliver(
1471-
{ { service::SubscriptionFilter { "nodeChange"sv,
1472-
{ service::SubscriptionArgumentFilterCallback { std::move(filterCallback) } } } },
1473-
{},
1474-
std::make_shared<today::object::Subscription>(std::move(subscriptionObject)) })
1471+
->deliver({ "nodeChange"sv,
1472+
{ service::SubscriptionFilter {
1473+
{ service::SubscriptionArgumentFilterCallback { std::move(filterCallback) } } } },
1474+
{}, // launch
1475+
std::make_shared<today::object::Subscription>(std::move(subscriptionObject)) })
14751476
.get();
14761477
_service->unsubscribe({ key }).get();
14771478

@@ -1524,10 +1525,10 @@ TEST_F(TodayServiceCase, SubscribeNodeChangeMatchingVariable)
15241525
state })
15251526
.get();
15261527
_service
1527-
->deliver({ { service::SubscriptionFilter { "nodeChange"sv,
1528-
{ service::SubscriptionArguments {
1529-
{ "id", response::Value("ZmFrZVRhc2tJZA=="s) } } } } },
1530-
{},
1528+
->deliver({ "nodeChange"sv,
1529+
{ service::SubscriptionFilter { { service::SubscriptionArguments {
1530+
{ "id", response::Value("ZmFrZVRhc2tJZA=="s) } } } } },
1531+
{}, // launch
15311532
std::make_shared<today::object::Subscription>(std::move(subscriptionObject)) })
15321533
.get();
15331534
_service->unsubscribe({ key }).get();
@@ -1722,10 +1723,7 @@ TEST_F(TodayServiceCase, SubscribeNextAppointmentChangeAsync)
17221723
{},
17231724
state })
17241725
.get();
1725-
_service
1726-
->deliver(
1727-
{ { service::SubscriptionFilter { "nextAppointmentChange"sv } }, std::launch::async })
1728-
.get();
1726+
_service->deliver({ "nextAppointmentChange"sv, {}, std::launch::async }).get();
17291727
_service->unsubscribe({ key }).get();
17301728

17311729
try

0 commit comments

Comments
 (0)