Skip to content

Commit a74155a

Browse files
authored
Merge pull request #240 from wravery/optional-subscriptions
Make the default subscription object optional and allow an override for subscribe/unsubscribe
2 parents c47cd51 + c78c31b commit a74155a

File tree

15 files changed

+246
-122
lines changed

15 files changed

+246
-122
lines changed

include/graphqlservice/GraphQLService.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,9 @@ struct [[nodiscard]] RequestSubscribeParams
12361236

12371237
// Optional sub-class of RequestState which will be passed to each resolver and field accessor.
12381238
std::shared_ptr<RequestState> state {};
1239+
1240+
// Optional override for the default Subscription operation object.
1241+
std::shared_ptr<const Object> subscriptionObject {};
12391242
};
12401243

12411244
struct [[nodiscard]] RequestUnsubscribeParams
@@ -1245,6 +1248,9 @@ struct [[nodiscard]] RequestUnsubscribeParams
12451248

12461249
// Optional async execution awaitable.
12471250
await_async launch {};
1251+
1252+
// Optional override for the default Subscription operation object.
1253+
std::shared_ptr<const Object> subscriptionObject {};
12481254
};
12491255

12501256
using SubscriptionArguments = std::map<std::string_view, response::Value>;
@@ -1321,6 +1327,13 @@ struct [[nodiscard]] SubscriptionData : std::enable_shared_from_this<Subscriptio
13211327
const peg::ast_node& selection;
13221328
};
13231329

1330+
// Placeholder for an empty subscription object.
1331+
class SubscriptionPlaceholder
1332+
{
1333+
// Private constructor, since it should never actually be instantiated.
1334+
SubscriptionPlaceholder() noexcept = default;
1335+
};
1336+
13241337
// Forward declare just the class type so we can reference it in the Request::_validation member.
13251338
class ValidateExecutableVisitor;
13261339

samples/learn/schema/StarWarsSchema.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ namespace learn {
9898

9999
Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation)
100100
: service::Request({
101-
{ "query", query },
102-
{ "mutation", mutation }
101+
{ service::strQuery, query },
102+
{ service::strMutation, mutation }
103103
}, GetSchema())
104104
, _query(std::move(query))
105105
, _mutation(std::move(mutation))

samples/learn/schema/StarWarsSchema.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ class [[nodiscard]] Operations final
7777

7878
template <class TQuery, class TMutation>
7979
explicit Operations(std::shared_ptr<TQuery> query, std::shared_ptr<TMutation> mutation)
80-
: Operations { std::make_shared<object::Query>(std::move(query)), std::make_shared<object::Mutation>(std::move(mutation)) }
80+
: Operations {
81+
std::make_shared<object::Query>(std::move(query)),
82+
std::make_shared<object::Mutation>(std::move(mutation))
83+
}
8184
{
8285
}
8386

samples/today/TodayMock.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ class NextAppointmentChange
547547

548548
std::shared_ptr<object::Node> getNodeChange(const response::IdType&) const
549549
{
550-
return {};
550+
throw std::runtime_error("Unexpected call to getNodeChange");
551551
}
552552

553553
private:
@@ -561,8 +561,9 @@ class NextAppointmentChange
561561
class NodeChange
562562
{
563563
public:
564-
using nodeChange = std::function<std::shared_ptr<object::Node>(
565-
const std::shared_ptr<service::RequestState>&, response::IdType&&)>;
564+
using nodeChange =
565+
std::function<std::shared_ptr<object::Node>(service::ResolverContext resolverContext,
566+
const std::shared_ptr<service::RequestState>&, response::IdType&&)>;
566567

567568
explicit NodeChange(nodeChange&& changeNode)
568569
: _changeNode(std::move(changeNode))
@@ -577,7 +578,7 @@ class NodeChange
577578
std::shared_ptr<object::Node> getNodeChange(
578579
const service::FieldParams& params, response::IdType&& idArg) const
579580
{
580-
return _changeNode(params.state, std::move(idArg));
581+
return _changeNode(params.resolverContext, params.state, std::move(idArg));
581582
}
582583

583584
private:

samples/today/nointrospection/TodaySchema.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ namespace today {
227227

228228
Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation, std::shared_ptr<object::Subscription> subscription)
229229
: service::Request({
230-
{ "query", query },
231-
{ "mutation", mutation },
232-
{ "subscription", subscription }
230+
{ service::strQuery, query },
231+
{ service::strMutation, mutation },
232+
{ service::strSubscription, subscription }
233233
}, GetSchema())
234234
, _query(std::move(query))
235235
, _mutation(std::move(mutation))

samples/today/nointrospection/TodaySchema.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,13 @@ class [[nodiscard]] Operations final
148148
public:
149149
explicit Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation, std::shared_ptr<object::Subscription> subscription);
150150

151-
template <class TQuery, class TMutation, class TSubscription>
152-
explicit Operations(std::shared_ptr<TQuery> query, std::shared_ptr<TMutation> mutation, std::shared_ptr<TSubscription> subscription)
153-
: Operations { std::make_shared<object::Query>(std::move(query)), std::make_shared<object::Mutation>(std::move(mutation)), std::make_shared<object::Subscription>(std::move(subscription)) }
151+
template <class TQuery, class TMutation, class TSubscription = service::SubscriptionPlaceholder>
152+
explicit Operations(std::shared_ptr<TQuery> query, std::shared_ptr<TMutation> mutation, std::shared_ptr<TSubscription> subscription = {})
153+
: Operations {
154+
std::make_shared<object::Query>(std::move(query)),
155+
std::make_shared<object::Mutation>(std::move(mutation)),
156+
subscription ? std::make_shared<object::Subscription>(std::move(subscription)) : std::shared_ptr<object::Subscription> {}
157+
}
154158
{
155159
}
156160

samples/today/schema/TodaySchema.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ namespace today {
227227

228228
Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation, std::shared_ptr<object::Subscription> subscription)
229229
: service::Request({
230-
{ "query", query },
231-
{ "mutation", mutation },
232-
{ "subscription", subscription }
230+
{ service::strQuery, query },
231+
{ service::strMutation, mutation },
232+
{ service::strSubscription, subscription }
233233
}, GetSchema())
234234
, _query(std::move(query))
235235
, _mutation(std::move(mutation))

samples/today/schema/TodaySchema.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,13 @@ class [[nodiscard]] Operations final
148148
public:
149149
explicit Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation, std::shared_ptr<object::Subscription> subscription);
150150

151-
template <class TQuery, class TMutation, class TSubscription>
152-
explicit Operations(std::shared_ptr<TQuery> query, std::shared_ptr<TMutation> mutation, std::shared_ptr<TSubscription> subscription)
153-
: Operations { std::make_shared<object::Query>(std::move(query)), std::make_shared<object::Mutation>(std::move(mutation)), std::make_shared<object::Subscription>(std::move(subscription)) }
151+
template <class TQuery, class TMutation, class TSubscription = service::SubscriptionPlaceholder>
152+
explicit Operations(std::shared_ptr<TQuery> query, std::shared_ptr<TMutation> mutation, std::shared_ptr<TSubscription> subscription = {})
153+
: Operations {
154+
std::make_shared<object::Query>(std::move(query)),
155+
std::make_shared<object::Mutation>(std::move(mutation)),
156+
subscription ? std::make_shared<object::Subscription>(std::move(subscription)) : std::shared_ptr<object::Subscription> {}
157+
}
154158
{
155159
}
156160

samples/validation/ValidationMock.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ class Mutation
2626
explicit Mutation() = default;
2727
};
2828

29-
class Subscription
30-
{
31-
public:
32-
explicit Subscription() = default;
33-
};
34-
3529
} // namespace graphql::validation
3630

3731
#endif // VALIDATIONMOCK_H

samples/validation/schema/ValidationSchema.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ namespace validation {
155155

156156
Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation, std::shared_ptr<object::Subscription> subscription)
157157
: service::Request({
158-
{ "query", query },
159-
{ "mutation", mutation },
160-
{ "subscription", subscription }
158+
{ service::strQuery, query },
159+
{ service::strMutation, mutation },
160+
{ service::strSubscription, subscription }
161161
}, GetSchema())
162162
, _query(std::move(query))
163163
, _mutation(std::move(mutation))

0 commit comments

Comments
 (0)