Skip to content

Commit 37c7712

Browse files
committed
Add overrides for subscribe/unsubscribe
1 parent 26f8b2f commit 37c7712

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

include/graphqlservice/GraphQLService.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,10 @@ class Request : public std::enable_shared_from_this<Request>
879879
GRAPHQLSERVICE_EXPORT std::future<response::Value> resolve(std::launch launch, const std::shared_ptr<RequestState>& state, peg::ast& query, const std::string& operationName, response::Value&& variables) const;
880880

881881
GRAPHQLSERVICE_EXPORT SubscriptionKey subscribe(SubscriptionParams&& params, SubscriptionCallback&& callback);
882+
GRAPHQLSERVICE_EXPORT SubscriptionKey subscribe(std::launch launch, SubscriptionParams&& params, SubscriptionCallback&& callback);
883+
882884
GRAPHQLSERVICE_EXPORT void unsubscribe(SubscriptionKey key);
885+
GRAPHQLSERVICE_EXPORT void unsubscribe(std::launch launch, SubscriptionKey key);
883886

884887
GRAPHQLSERVICE_EXPORT void deliver(const SubscriptionName& name, const std::shared_ptr<Object>& subscriptionObject) const;
885888
GRAPHQLSERVICE_EXPORT void deliver(const SubscriptionName& name, const SubscriptionArguments& arguments, const std::shared_ptr<Object>& subscriptionObject) const;

src/GraphQLService.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,39 @@ SubscriptionKey Request::subscribe(SubscriptionParams&& params, SubscriptionCall
21042104
return key;
21052105
}
21062106

2107+
SubscriptionKey Request::subscribe(std::launch launch, SubscriptionParams&& params, SubscriptionCallback&& callback)
2108+
{
2109+
const auto key = subscribe(std::move(params), std::move(callback));
2110+
const auto itrOperation = _operations.find(std::string { strSubscription });
2111+
const auto itrSubscription = _subscriptions.find(key);
2112+
2113+
if (itrOperation != _operations.cend()
2114+
&& itrSubscription != _subscriptions.cend())
2115+
{
2116+
const auto& operation = itrOperation->second;
2117+
const auto& registration = itrSubscription->second;
2118+
response::Value emptyFragmentDirectives(response::Type::Map);
2119+
const SelectionSetParams selectionSetParams {
2120+
ResolverContext::NotifySubscribe,
2121+
registration->data->state,
2122+
registration->data->directives,
2123+
emptyFragmentDirectives,
2124+
emptyFragmentDirectives,
2125+
emptyFragmentDirectives,
2126+
{},
2127+
launch,
2128+
};
2129+
2130+
std::async(launch,
2131+
[](std::future<response::Value> document)
2132+
{
2133+
return document.get();
2134+
}, operation->resolve(selectionSetParams, registration->selection, registration->data->fragments, registration->data->variables)).get();
2135+
}
2136+
2137+
return key;
2138+
}
2139+
21072140
void Request::unsubscribe(SubscriptionKey key)
21082141
{
21092142
auto itrSubscription = _subscriptions.find(key);
@@ -2133,6 +2166,38 @@ void Request::unsubscribe(SubscriptionKey key)
21332166
}
21342167
}
21352168

2169+
void Request::unsubscribe(std::launch launch, SubscriptionKey key)
2170+
{
2171+
const auto itrOperation = _operations.find(std::string { strSubscription });
2172+
const auto itrSubscription = _subscriptions.find(key);
2173+
2174+
if (itrOperation != _operations.cend()
2175+
&& itrSubscription != _subscriptions.cend())
2176+
{
2177+
const auto& operation = itrOperation->second;
2178+
const auto& registration = itrSubscription->second;
2179+
response::Value emptyFragmentDirectives(response::Type::Map);
2180+
const SelectionSetParams selectionSetParams {
2181+
ResolverContext::NotifyUnsubscribe,
2182+
registration->data->state,
2183+
registration->data->directives,
2184+
emptyFragmentDirectives,
2185+
emptyFragmentDirectives,
2186+
emptyFragmentDirectives,
2187+
{},
2188+
launch,
2189+
};
2190+
2191+
std::async(launch,
2192+
[](std::future<response::Value> document)
2193+
{
2194+
return document.get();
2195+
}, operation->resolve(selectionSetParams, registration->selection, registration->data->fragments, registration->data->variables)).get();
2196+
}
2197+
2198+
unsubscribe(key);
2199+
}
2200+
21362201
void Request::deliver(const SubscriptionName& name, const std::shared_ptr<Object>& subscriptionObject) const
21372202
{
21382203
deliver(std::launch::deferred, name, subscriptionObject);

0 commit comments

Comments
 (0)