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

Subscription Lookup Should Use Alias if Specified #4791

Merged
merged 2 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down