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 #4791 from prisma/AliasesInSubscriptions
Browse files Browse the repository at this point in the history
Subscription Lookup Should Use Alias if Specified
  • Loading branch information
do4gr authored Aug 8, 2019
2 parents 9adefc9 + f60ac5a commit b77460f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
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

0 comments on commit b77460f

Please sign in to comment.