Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2171 from graphcool/ServerSideSubscriptionMutatio…
Browse files Browse the repository at this point in the history
…nTypeBug

Fix bug where only delete ServerSideSubscriptions were being run
  • Loading branch information
do4gr authored Mar 30, 2018
2 parents 37e0d54 + c32d9c4 commit 543dd3b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object ServerSideSubscriptions {
)(implicit apiDependencies: ApiDependencies): Vector[ServerSideSubscription] = {
for {
mutaction <- mutactions
sssFn <- serverSideSubscriptionFunctionsFor(project, mutaction.model, ModelMutationType.Deleted)
sssFn <- serverSideSubscriptionFunctionsFor(project, mutaction.model, ModelMutationType.Created)
} yield {
ServerSideSubscription(
project,
Expand All @@ -52,7 +52,7 @@ object ServerSideSubscriptions {
)(implicit apiDependencies: ApiDependencies): Vector[ServerSideSubscription] = {
for {
mutaction <- mutactions
sssFn <- serverSideSubscriptionFunctionsFor(project, mutaction.model, ModelMutationType.Deleted)
sssFn <- serverSideSubscriptionFunctionsFor(project, mutaction.model, ModelMutationType.Updated)
} yield {
ServerSideSubscription(
project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ trait SideEffectMutactionExecutor {

case class SideEffectMutactionExecutorImpl()(implicit apiDependencies: ApiDependencies, ec: ExecutionContext) extends SideEffectMutactionExecutor {

override def execute(mutactions: Vector[SideEffectMutaction]): Future[Unit] = {
Future.sequence(mutactions.map(execute)).map(_ => ())
}
override def execute(mutactions: Vector[SideEffectMutaction]): Future[Unit] = Future.sequence(mutactions.map(execute)).map(_ => ())

def execute(mutaction: SideEffectMutaction): Future[Unit] = mutaction match {
case mutaction: PublishSubscriptionEvent =>
PublishSubscriptionEventExecutor.execute(mutaction, apiDependencies.sssEventsPubSub)
case mutaction: ServerSideSubscription =>
ServerSideSubscriptionExecutor.execute(mutaction)
case mutaction: PublishSubscriptionEvent => PublishSubscriptionEventExecutor.execute(mutaction, apiDependencies.sssEventsPubSub)
case mutaction: ServerSideSubscription => ServerSideSubscriptionExecutor.execute(mutaction)
}

}
Expand All @@ -44,10 +40,8 @@ object PublishSubscriptionEventExecutor {
object ServerSideSubscriptionExecutor {
def execute(mutaction: ServerSideSubscription)(implicit apiDependencies: ApiDependencies): Future[Unit] = {
mutaction.function.delivery match {
case webhookDelivery: WebhookDelivery =>
deliverWebhook(mutaction, webhookDelivery)
case _ =>
Future.unit
case webhookDelivery: WebhookDelivery => deliverWebhook(mutaction, webhookDelivery)
case _ => Future.unit
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ServerSideSubscriptionSpec extends FlatSpec with Matchers with ApiBaseSpec

override def beforeEach = {
super.beforeEach()

database.truncate(project)
webhookTestKit.reset
}
Expand All @@ -33,14 +32,14 @@ class ServerSideSubscriptionSpec extends FlatSpec with Matchers with ApiBaseSpec
.model("Todo")
.field("title", _.String)
.field("status", _.Enum, enum = Some(status))
.oneToManyRelation("comments", "todo", comment)
todo.oneToManyRelation("comments", "todo", comment)
}

val subscriptionQueryForCreates: String =
"""
def subscriptionQueryFor(mutation: String): String =
s"""
|subscription {
| todo(where: {
| mutation_in : [CREATED, UPDATED, DELETED]
| mutation_in : [$mutation]
| node: {
| status: Active
| }
Expand All @@ -61,17 +60,37 @@ class ServerSideSubscriptionSpec extends FlatSpec with Matchers with ApiBaseSpec

val webhookUrl = "http://www.mywebhooks.com"
val webhookHeaders = Vector("header" -> "value")
val sssFunction = ServerSideSubscriptionFunction(
name = "Test Function",
val sssFunctionForCreate = ServerSideSubscriptionFunction(
name = "Test Function CREATED",
isActive = true,
query = subscriptionQueryFor("CREATED"),
delivery = WebhookDelivery(
url = webhookUrl,
headers = webhookHeaders
)
)

val sssFunctionForUpdate = ServerSideSubscriptionFunction(
name = "Test Function UPDATED",
isActive = true,
query = subscriptionQueryForCreates,
query = subscriptionQueryFor("UPDATED"),
delivery = WebhookDelivery(
url = webhookUrl,
headers = webhookHeaders
)
)

val actualProject: Project = project.copy(functions = List(sssFunction))
val sssFunctionForDeleted = ServerSideSubscriptionFunction(
name = "Test Function DELETED",
isActive = true,
query = subscriptionQueryFor("DELETED"),
delivery = WebhookDelivery(
url = webhookUrl,
headers = webhookHeaders
)
)

val actualProject: Project = project.copy(functions = List(sssFunctionForCreate, sssFunctionForUpdate, sssFunctionForDeleted))

val newTodoTitle = "The title of the new todo"
val newTodoStatus = "Active"
Expand All @@ -89,13 +108,14 @@ class ServerSideSubscriptionSpec extends FlatSpec with Matchers with ApiBaseSpec
| }
|}
""".stripMargin

val id = server.query(createTodo, actualProject).pathAsString("data.createTodo.id")

webhookTestKit.expectPublishCount(1)

val webhook = webhookTestKit.messagesPublished.head

webhook.functionName shouldEqual sssFunction.name
webhook.functionName shouldEqual sssFunctionForCreate.name
webhook.projectId shouldEqual project.id
// webhook.requestId shouldNot be(empty)
// webhook.id shouldNot be(empty)
Expand Down Expand Up @@ -149,7 +169,7 @@ class ServerSideSubscriptionSpec extends FlatSpec with Matchers with ApiBaseSpec

val webhook = webhookTestKit.messagesPublished.head

webhook.functionName shouldEqual sssFunction.name
webhook.functionName shouldEqual sssFunctionForUpdate.name
webhook.projectId shouldEqual project.id
// webhook.requestId shouldNot be(empty)
// webhook.id shouldNot be(empty)
Expand All @@ -172,7 +192,7 @@ class ServerSideSubscriptionSpec extends FlatSpec with Matchers with ApiBaseSpec
webhook.headers shouldEqual Map("header" -> "value")
}

"ServerSideSubscription" should "send a message to our Webhook Queue if the SSS Query matches on an Delete" in {
"ServerSideSubscription" should "send a message to our Webhook Queue if the SSS Query matches on a Delete" in {
val createTodo =
s"""
|mutation {
Expand Down Expand Up @@ -204,7 +224,7 @@ class ServerSideSubscriptionSpec extends FlatSpec with Matchers with ApiBaseSpec

val webhook = webhookTestKit.messagesPublished.head

webhook.functionName shouldEqual sssFunction.name
webhook.functionName shouldEqual sssFunctionForDeleted.name
webhook.projectId shouldEqual project.id
// webhook.requestId shouldNot be(empty)
// webhook.id shouldNot be(empty)
Expand Down Expand Up @@ -249,7 +269,7 @@ class ServerSideSubscriptionSpec extends FlatSpec with Matchers with ApiBaseSpec

val webhook = webhookTestKit.messagesPublished.head

webhook.functionName shouldEqual sssFunction.name
webhook.functionName shouldEqual sssFunctionForCreate.name
webhook.projectId shouldEqual project.id
// webhook.requestId shouldNot be(empty)
// webhook.id shouldNot be(empty)
Expand Down Expand Up @@ -314,7 +334,7 @@ class ServerSideSubscriptionSpec extends FlatSpec with Matchers with ApiBaseSpec

val webhook = webhookTestKit.messagesPublished.head

webhook.functionName shouldEqual sssFunction.name
webhook.functionName shouldEqual sssFunctionForCreate.name
webhook.projectId shouldEqual project.id
// webhook.requestId shouldNot be(empty)
// webhook.id shouldNot be(empty)
Expand Down

0 comments on commit 543dd3b

Please sign in to comment.