Skip to content

Commit 8861d50

Browse files
committed
Add a filter callback version of deliver for #26
1 parent d9b8f5f commit 8861d50

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

GraphQLService.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,10 +1707,23 @@ void Request::unsubscribe(SubscriptionKey key)
17071707

17081708
void Request::deliver(const SubscriptionName& name, const std::shared_ptr<Object>& subscriptionObject) const
17091709
{
1710-
deliver(name, {}, subscriptionObject);
1710+
deliver(name, SubscriptionArguments {}, subscriptionObject);
17111711
}
17121712

1713-
void Request::deliver(const SubscriptionName& name, const std::unordered_map<std::string, response::Value>& arguments, const std::shared_ptr<Object>& subscriptionObject) const
1713+
void Request::deliver(const SubscriptionName& name, const SubscriptionArguments& arguments, const std::shared_ptr<Object>& subscriptionObject) const
1714+
{
1715+
SubscriptionFilterCallback exactMatch = [&arguments](response::MapType::const_reference required) noexcept -> bool
1716+
{
1717+
auto itrArgument = arguments.find(required.first);
1718+
1719+
return (itrArgument != arguments.cend()
1720+
&& itrArgument->second == required.second);
1721+
};
1722+
1723+
deliver(name, exactMatch, subscriptionObject);
1724+
}
1725+
1726+
void Request::deliver(const SubscriptionName& name, const SubscriptionFilterCallback& apply, const std::shared_ptr<Object>& subscriptionObject) const
17141727
{
17151728
const auto& optionalOrDefaultSubscription = subscriptionObject
17161729
? subscriptionObject
@@ -1738,10 +1751,7 @@ void Request::deliver(const SubscriptionName& name, const std::unordered_map<std
17381751

17391752
for (auto itrRequired = required.begin(); itrRequired != required.end(); ++itrRequired)
17401753
{
1741-
auto itrArgument = arguments.find(itrRequired->first);
1742-
1743-
if (itrArgument == arguments.cend()
1744-
|| itrArgument->second != itrRequired->second)
1754+
if (!apply(*itrRequired))
17451755
{
17461756
matchedArguments = false;
17471757
break;
@@ -1773,13 +1783,13 @@ void Request::deliver(const SubscriptionName& name, const std::unordered_map<std
17731783
{
17741784
result = std::async(std::launch::deferred,
17751785
[registration](std::future<response::Value> data)
1776-
{
1777-
response::Value document(response::Type::Map);
1786+
{
1787+
response::Value document(response::Type::Map);
17781788

1779-
document.emplace_back("data", data.get());
1789+
document.emplace_back("data", data.get());
17801790

1781-
return document;
1782-
}, optionalOrDefaultSubscription->resolve(selectionSetParams, registration->selection, registration->data->fragments, registration->data->variables));
1791+
return document;
1792+
}, optionalOrDefaultSubscription->resolve(selectionSetParams, registration->selection, registration->data->fragments, registration->data->variables));
17831793
}
17841794
catch (const schema_exception& ex)
17851795
{

include/graphqlservice/GraphQLService.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ struct OperationData : std::enable_shared_from_this<OperationData>
523523
// Subscription callbacks receive the response::Value representing the result of evaluating the
524524
// SelectionSet against the payload.
525525
using SubscriptionCallback = std::function<void(std::future<response::Value>)>;
526+
using SubscriptionArguments = std::unordered_map<std::string, response::Value>;
527+
using SubscriptionFilterCallback = std::function<bool(response::MapType::const_reference) noexcept>;
526528

527529
// Subscriptions are stored in maps using these keys.
528530
using SubscriptionKey = size_t;
@@ -558,7 +560,8 @@ class Request : public std::enable_shared_from_this<Request>
558560
void unsubscribe(SubscriptionKey key);
559561

560562
void deliver(const SubscriptionName& name, const std::shared_ptr<Object>& subscriptionObject) const;
561-
void deliver(const SubscriptionName& name, const std::unordered_map<std::string, response::Value>& arguments, const std::shared_ptr<Object>& subscriptionObject) const;
563+
void deliver(const SubscriptionName& name, const SubscriptionArguments& arguments, const std::shared_ptr<Object>& subscriptionObject) const;
564+
void deliver(const SubscriptionName& name, const SubscriptionFilterCallback& apply, const std::shared_ptr<Object>& subscriptionObject) const;
562565

563566
private:
564567
TypeMap _operations;

0 commit comments

Comments
 (0)