Skip to content

Commit

Permalink
Merge pull request #83 from caffeinetv/validate-operations
Browse files Browse the repository at this point in the history
Validate graphql operations
  • Loading branch information
trilorez authored Jun 27, 2019
2 parents ff2df85 + 362b744 commit 1afd56b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/CaffQL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace caffql {
using absl::monostate;
using absl::visit;

enum class Operation { Query, Mutation, Subscription };

struct GraphqlError {
std::string message;
};
Expand Down Expand Up @@ -685,6 +687,8 @@ namespace caffql {
*/
struct StageField {

static Operation constexpr operation = Operation::Subscription;

static Json request(Id const & clientId, ClientType clientType, std::string const & username, optional<bool> constrainedBaseline, optional<std::vector<StageSubscriptionViewerStreamInput>> const & viewerStreams, optional<bool> skipStreamAllocation) {
Json query = R"(
subscription Stage(
Expand Down Expand Up @@ -867,6 +871,8 @@ namespace caffql {
*/
struct StageField {

static Operation constexpr operation = Operation::Query;

static Json request(std::string const & username) {
Json query = R"(
query Stage(
Expand Down Expand Up @@ -1091,9 +1097,20 @@ namespace caffql {
Adding a feed may mutate other feeds, since Reyes always maintains a legal
assignment of roles to the various feeds.
Roles are assigned according to the following rules:
1. Each client may have at most 2 feeds on the stage.
2. Each client may have at most 1 live hosting feed on the stage.
3. If the client did not specify the roles on the feeds, then the last feed
that was added will be PRIMARY.
4. However, if one of the feeds is live hosting, then that feed will be made
PRIMARY.
*/
struct AddFeedField {

static Operation constexpr operation = Operation::Mutation;

static Json request(Id const & clientId, ClientType clientType, FeedInput const & input) {
Json query = R"(
mutation AddFeed(
Expand Down Expand Up @@ -1246,6 +1263,8 @@ namespace caffql {
*/
struct SetLiveHostingFeedField {

static Operation constexpr operation = Operation::Mutation;

static Json request(Id const & clientId, ClientType clientType, FeedInput const & input) {
Json query = R"(
mutation SetLiveHostingFeed(
Expand Down Expand Up @@ -1390,6 +1409,8 @@ namespace caffql {
*/
struct UpdateFeedField {

static Operation constexpr operation = Operation::Mutation;

static Json request(Id const & clientId, ClientType clientType, FeedInput const & input) {
Json query = R"(
mutation UpdateFeed(
Expand Down Expand Up @@ -1535,6 +1556,8 @@ namespace caffql {
*/
struct RemoveFeedField {

static Operation constexpr operation = Operation::Mutation;

static Json request(Id const & clientId, ClientType clientType, Id const & feedId) {
Json query = R"(
mutation RemoveFeed(
Expand Down Expand Up @@ -1640,6 +1663,8 @@ namespace caffql {
// If a client is controlling the stage, then only that client may change the title.
struct ChangeStageTitleField {

static Operation constexpr operation = Operation::Mutation;

static Json request(Id const & clientId, ClientType clientType, std::string const & title) {
Json query = R"(
mutation ChangeStageTitle(
Expand Down Expand Up @@ -1748,6 +1773,8 @@ namespace caffql {
*/
struct StartBroadcastField {

static Operation constexpr operation = Operation::Mutation;

static Json request(Id const & clientId, ClientType clientType, optional<std::string> const & title) {
Json query = R"(
mutation StartBroadcast(
Expand Down Expand Up @@ -1856,6 +1883,8 @@ namespace caffql {
*/
struct StopBroadcastField {

static Operation constexpr operation = Operation::Mutation;

static Json request(Id const & clientId, optional<bool> keepLiveHostingFeeds) {
Json query = R"(
mutation StopBroadcast(
Expand Down Expand Up @@ -1966,6 +1995,8 @@ namespace caffql {
*/
struct RequestViewerStreamField {

static Operation constexpr operation = Operation::Mutation;

static Json request(Id const & clientId, Id const & feedId) {
Json query = R"(
mutation RequestViewerStream(
Expand Down Expand Up @@ -2004,6 +2035,8 @@ namespace caffql {
// This is an admin-only mutation.
struct TakedownStageField {

static Operation constexpr operation = Operation::Mutation;

static Json request(std::string const & username) {
Json query = R"(
mutation TakedownStage(
Expand Down
3 changes: 3 additions & 0 deletions src/RestApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ namespace caff {

template <typename OperationField, typename... Args>
optional<typename OperationField::ResponseData> graphqlRequest(SharedCredentials & creds, Args const &... args) {
static_assert(
OperationField::operation != caffql::Operation::Subscription,
"graphqlRequest only supports query and mutation operations");

auto requestJson = OperationField::request(args...);
auto rawResponse = graphqlRawRequest(creds, requestJson);
Expand Down
4 changes: 4 additions & 0 deletions src/WebsocketApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ namespace caff {
template <typename OperationField>
class GraphqlSubscription : public std::enable_shared_from_this<GraphqlSubscription<OperationField>> {
public:
static_assert(
OperationField::operation == caffql::Operation::Subscription,
"GraphqlSubscription only supports subscription operations");

template <typename... Args>
GraphqlSubscription(
WebsocketClient & client,
Expand Down

0 comments on commit 1afd56b

Please sign in to comment.