@@ -13,8 +13,13 @@ the subscriptions to those listeners.
1313Subscriptions are created or removed by calling the ` Request::subscribe `
1414and ` Request::unsubscribe ` methods in [ GraphQLService.h] ( ../include/graphqlservice/GraphQLService.h ) :
1515``` cpp
16- SubscriptionKey subscribe (SubscriptionParams&& params, SubscriptionCallback&& callback);
17- void unsubscribe(SubscriptionKey key);
16+ GRAPHQLSERVICE_EXPORT SubscriptionKey subscribe (
17+ SubscriptionParams&& params, SubscriptionCallback&& callback);
18+ GRAPHQLSERVICE_EXPORT std::future<SubscriptionKey > subscribe(
19+ std::launch launch, SubscriptionParams&& params, SubscriptionCallback&& callback);
20+
21+ GRAPHQLSERVICE_EXPORT void unsubscribe(SubscriptionKey key);
22+ GRAPHQLSERVICE_EXPORT std::future<void > unsubscribe(std::launch launch, SubscriptionKey key);
1823```
1924You need to fill in a `SubscriptionParams` struct with the [parsed](./parsing.md)
2025query and any other relevant operation parameters:
@@ -37,6 +42,23 @@ The `SubscriptionCallback` signature is:
3742using SubscriptionCallback = std::function<void (std::future<response::Value>)>;
3843```
3944
45+ ## ` ResolverContext::NotifySubscribe ` and ` ResolverContext::NotifyUnsubscribe `
46+
47+ If you use the async version of ` subscribe ` and ` unsubscribe ` which take a
48+ ` std::launch ` parameter, and you provide a default instance of the
49+ ` Subscription ` object to the ` Request ` /` Operations ` constructor, you will get
50+ additional callbacks with the ` ResolverContext::NotifySubscribe ` and
51+ ` ResolverContext::NotifyUnsubscribe ` values for the
52+ ` FieldParams::resolverContext ` member. These are passed as part of the
53+ ` subscribe ` and ` unsubscribe ` calls on the default subscription object, and
54+ they provide an opportunity to acquire or release resources that are required
55+ to implement the subscription. You can provide separate implementations of the
56+ ` Subscription ` object as the default in the ` Operations ` constructor and as the
57+ payload of a specific ` deliver ` call, which will be resolved with
58+ ` ResolverContext::Subscription ` . If you do not provide a separate
59+ ` Subscription ` object in the ` deliver ` call, it will fall back to delivering a
60+ new event resolved against the default
61+
4062## Delivering Subscription Updates
4163
4264There are currently three ` Request::deliver ` overrides you can choose from when
@@ -46,33 +68,67 @@ parameter (which should match the `Subscription` type in the `schema`). It will
4668unconditionally invoke every subscribed ` SubscriptionCallback ` callback with
4769the response to its query:
4870``` cpp
49- void deliver (const SubscriptionName& name, const std::shared_ptr<Object >& subscriptionObject) const;
71+ GRAPHQLSERVICE_EXPORT void deliver (
72+ const SubscriptionName& name, const std::shared_ptr<Object>& subscriptionObject) const;
5073```
5174
5275The second override adds argument filtering. It will look at the field
5376arguments in the subscription `query`, and if all of the required parameters
54- in the `arguments` parameter are present and are an exact match it will dispatch the callback to that subscription:
77+ in the `arguments` parameter are present and are an exact match it will
78+ dispatch the callback to that subscription:
79+ ```cpp
80+ GRAPHQLSERVICE_EXPORT void deliver(const SubscriptionName& name,
81+ const SubscriptionArguments& arguments,
82+ const std::shared_ptr<Object>& subscriptionObject) const;
83+ ```
84+
85+ The third override adds directive filtering. It will look at both the field
86+ arguments and the directives with their own arguments in the subscription
87+ ` query ` , and if all of them match it will dispatch the callback to that
88+ subscription:
5589``` cpp
56- void deliver(const SubscriptionName& name, const SubscriptionArguments& arguments, const std::shared_ptr<Object>& subscriptionObject) const;
90+ GRAPHQLSERVICE_EXPORT void deliver (const SubscriptionName& name,
91+ const SubscriptionArguments& arguments, const SubscriptionArguments& directives,
92+ const std::shared_ptr<Object >& subscriptionObject) const;
5793```
5894
59- The last override lets you customize the the way that the required arguments
60- are matched. Instead of an exact match or making all of the arguments required,
61- it will dispatch the callback if the ` apply ` function parameter returns true
62- for every required field in the subscription ` query ` .
95+ The last two overrides let you customize the the way that the required
96+ arguments and directives are matched. Instead of an exact match or making all
97+ of the arguments required, it will dispatch the callback if the `apply`
98+ function parameters return true for every required field and directive in the
99+ subscription `query`.
63100```cpp
64- void deliver (const SubscriptionName& name, const SubscriptionFilterCallback& apply, const std::shared_ptr<Object >& subscriptionObject) const;
101+ GRAPHQLSERVICE_EXPORT void deliver(const SubscriptionName& name,
102+ const SubscriptionFilterCallback& applyArguments,
103+ const std::shared_ptr<Object>& subscriptionObject) const;
104+ GRAPHQLSERVICE_EXPORT void deliver(const SubscriptionName& name,
105+ const SubscriptionFilterCallback& applyArguments,
106+ const SubscriptionFilterCallback& applyDirectives,
107+ const std::shared_ptr<Object>& subscriptionObject) const;
65108```
66109
67- By default, `deliver` invokes all of the `SubscriptionCallback` listeners with `std::future`
68- payloads which are resolved on-demand but synchronously, using `std::launch::deferred` with the
69- `std::async` function. There's also a version of each overload which lets you substitute the
70- `std::launch::async` option to begin executing the queries and invoke the callbacks on multiple
71- threads in parallel:
110+ By default, ` deliver ` invokes all of the ` SubscriptionCallback ` listeners with
111+ ` std::future ` payloads which are resolved on-demand but synchronously, using
112+ ` std::launch::deferred ` with the ` std::async ` function. There's also a version
113+ of each overload which lets you substitute the ` std::launch::async ` option to
114+ begin executing the queries and invoke the callbacks on multiple threads in
115+ parallel:
72116``` cpp
73- void deliver(std::launch launch, const SubscriptionName& name, const std::shared_ptr<Object>& subscriptionObject) const;
74- void deliver(std::launch launch, const SubscriptionName& name, const SubscriptionArguments& arguments, const std::shared_ptr<Object>& subscriptionObject) const;
75- void deliver(std::launch launch, const SubscriptionName& name, const SubscriptionFilterCallback& apply, const std::shared_ptr<Object>& subscriptionObject) const;
117+ GRAPHQLSERVICE_EXPORT void deliver (std::launch launch, const SubscriptionName& name,
118+ const std::shared_ptr<Object >& subscriptionObject) const;
119+ GRAPHQLSERVICE_EXPORT void deliver(std::launch launch, const SubscriptionName& name,
120+ const SubscriptionArguments& arguments,
121+ const std::shared_ptr<Object >& subscriptionObject) const;
122+ GRAPHQLSERVICE_EXPORT void deliver(std::launch launch, const SubscriptionName& name,
123+ const SubscriptionArguments& arguments, const SubscriptionArguments& directives,
124+ const std::shared_ptr<Object >& subscriptionObject) const;
125+ GRAPHQLSERVICE_EXPORT void deliver(std::launch launch, const SubscriptionName& name,
126+ const SubscriptionFilterCallback& applyArguments,
127+ const std::shared_ptr<Object >& subscriptionObject) const;
128+ GRAPHQLSERVICE_EXPORT void deliver(std::launch launch, const SubscriptionName& name,
129+ const SubscriptionFilterCallback& applyArguments,
130+ const SubscriptionFilterCallback& applyDirectives,
131+ const std::shared_ptr<Object >& subscriptionObject) const;
76132```
77133
78134## Handling Multiple Operation Types
@@ -83,8 +139,9 @@ tell which operation type it is without parsing the query and searching for
83139a specific operation name, so it's hard to tell whether you should call
84140`resolve` or `subscribe` when the request is received that way. To help with
85141that, there's a public `Request::findOperationDefinition` method which returns
86- the operation type as a ` std::string_view ` along with a pointer to the AST node for
87- the selected operation in the parsed query:
142+ the operation type as a `std::string_view` along with a pointer to the AST node
143+ for the selected operation in the parsed query:
88144```cpp
89- std::pair<std::string_view, const peg::ast_node*> findOperationDefinition (peg::ast& root, std::string_view operationName) const;
145+ GRAPHQLSERVICE_EXPORT std::pair<std::string_view, const peg::ast_node*> findOperationDefinition(
146+ peg::ast& query, std::string_view operationName) const;
90147```
0 commit comments