diff --git a/server/servers/api/src/main/scala/com/prisma/subscriptions/SubscriptionExecutor.scala b/server/servers/api/src/main/scala/com/prisma/subscriptions/SubscriptionExecutor.scala index 9546e09d6b..11ef5289b9 100644 --- a/server/servers/api/src/main/scala/com/prisma/subscriptions/SubscriptionExecutor.scala +++ b/server/servers/api/src/main/scala/com/prisma/subscriptions/SubscriptionExecutor.scala @@ -11,8 +11,9 @@ import com.prisma.shared.models.ModelMutationType.ModelMutationType import com.prisma.shared.models._ import com.prisma.subscriptions.schema.{QueryTransformer, SubscriptionSchema, VariablesTransformer} import play.api.libs.json._ -import sangria.ast.Document +import sangria.ast.{Document, Selection} import sangria.execution.Executor +import sangria.marshalling.queryAst import sangria.parser.QueryParser import sangria.renderer.QueryRenderer @@ -112,8 +113,22 @@ object SubscriptionExecutor { deferredResolver = new DeferredResolverImpl(dataResolver) ) .map { result => - val lookup = result.as[JsObject] \ "data" \ camelCase(model.name) - if (lookup.validate[JsValue].get != JsNull) Some(result) else None + val lookup = for { + subscriptionOperation <- transformedQuery.operations.find(x => x._2.operationType == sangria.ast.OperationType.Subscription) + fields = subscriptionOperation._2.selections.collect { case x: sangria.ast.Field => x } + field <- fields.find(p => p.name == camelCase(model.name)) + } yield { + field.alias match { + case Some(alias) => result.as[JsObject] \ "data" \ alias + case None => result.as[JsObject] \ "data" \ camelCase(model.name) + } + } + + lookup match { + case None => None + case Some(look) => if (look.validate[JsValue].get != JsNull) Some(result) else None + } + } } else { Future.successful(None) diff --git a/server/servers/subscriptions/src/test/scala/com/prisma/subscriptions/specs/SubscriptionFilterSpec.scala b/server/servers/subscriptions/src/test/scala/com/prisma/subscriptions/specs/SubscriptionFilterSpec.scala index 8703e09e0b..1d43f49d03 100644 --- a/server/servers/subscriptions/src/test/scala/com/prisma/subscriptions/specs/SubscriptionFilterSpec.scala +++ b/server/servers/subscriptions/src/test/scala/com/prisma/subscriptions/specs/SubscriptionFilterSpec.scala @@ -104,6 +104,48 @@ class SubscriptionFilterSpec extends FlatSpec with Matchers with SubscriptionSpe } } + "this" should "work when using aliases" in { + testInitializedWebsocket(project) { wsClient => + wsClient.sendMessage( + startMessage( + id = "3", + query = """subscription { + | alias: todo{ + | mutation + | previousValues { + | id + | text + | status + | } + | } + |}""".stripMargin + ) + ) + + sleep(8000) + + val event = nodeEvent( + modelId = model.name, + changedFields = Seq("text"), + previousValues = s"""{"id":"$testNodeId", "text":"event1", "status": "Active", "tags":[]}""" + ) + + sssEventsTestKit.publish(Only(s"subscription:event:${project.id}:updateTodo"), event) + + wsClient.expectMessage( + dataMessage( + id = "3", + payload = s"""{ + | "alias":{ + | "mutation":"UPDATED", + | "previousValues":{"id":"$testNodeId", "text":"event1", "status":"Active"} + | } + |}""".stripMargin + ) + ) + } + } + "this" should "support scalar lists in previous values" ignore { testInitializedWebsocket(project) { wsClient => wsClient.sendMessage(