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

Commit b77460f

Browse files
authored
Merge pull request #4791 from prisma/AliasesInSubscriptions
Subscription Lookup Should Use Alias if Specified
2 parents 9adefc9 + f60ac5a commit b77460f

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

server/servers/api/src/main/scala/com/prisma/subscriptions/SubscriptionExecutor.scala

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import com.prisma.shared.models.ModelMutationType.ModelMutationType
1111
import com.prisma.shared.models._
1212
import com.prisma.subscriptions.schema.{QueryTransformer, SubscriptionSchema, VariablesTransformer}
1313
import play.api.libs.json._
14-
import sangria.ast.Document
14+
import sangria.ast.{Document, Selection}
1515
import sangria.execution.Executor
16+
import sangria.marshalling.queryAst
1617
import sangria.parser.QueryParser
1718
import sangria.renderer.QueryRenderer
1819

@@ -112,8 +113,22 @@ object SubscriptionExecutor {
112113
deferredResolver = new DeferredResolverImpl(dataResolver)
113114
)
114115
.map { result =>
115-
val lookup = result.as[JsObject] \ "data" \ camelCase(model.name)
116-
if (lookup.validate[JsValue].get != JsNull) Some(result) else None
116+
val lookup = for {
117+
subscriptionOperation <- transformedQuery.operations.find(x => x._2.operationType == sangria.ast.OperationType.Subscription)
118+
fields = subscriptionOperation._2.selections.collect { case x: sangria.ast.Field => x }
119+
field <- fields.find(p => p.name == camelCase(model.name))
120+
} yield {
121+
field.alias match {
122+
case Some(alias) => result.as[JsObject] \ "data" \ alias
123+
case None => result.as[JsObject] \ "data" \ camelCase(model.name)
124+
}
125+
}
126+
127+
lookup match {
128+
case None => None
129+
case Some(look) => if (look.validate[JsValue].get != JsNull) Some(result) else None
130+
}
131+
117132
}
118133
} else {
119134
Future.successful(None)

server/servers/subscriptions/src/test/scala/com/prisma/subscriptions/specs/SubscriptionFilterSpec.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,48 @@ class SubscriptionFilterSpec extends FlatSpec with Matchers with SubscriptionSpe
104104
}
105105
}
106106

107+
"this" should "work when using aliases" in {
108+
testInitializedWebsocket(project) { wsClient =>
109+
wsClient.sendMessage(
110+
startMessage(
111+
id = "3",
112+
query = """subscription {
113+
| alias: todo{
114+
| mutation
115+
| previousValues {
116+
| id
117+
| text
118+
| status
119+
| }
120+
| }
121+
|}""".stripMargin
122+
)
123+
)
124+
125+
sleep(8000)
126+
127+
val event = nodeEvent(
128+
modelId = model.name,
129+
changedFields = Seq("text"),
130+
previousValues = s"""{"id":"$testNodeId", "text":"event1", "status": "Active", "tags":[]}"""
131+
)
132+
133+
sssEventsTestKit.publish(Only(s"subscription:event:${project.id}:updateTodo"), event)
134+
135+
wsClient.expectMessage(
136+
dataMessage(
137+
id = "3",
138+
payload = s"""{
139+
| "alias":{
140+
| "mutation":"UPDATED",
141+
| "previousValues":{"id":"$testNodeId", "text":"event1", "status":"Active"}
142+
| }
143+
|}""".stripMargin
144+
)
145+
)
146+
}
147+
}
148+
107149
"this" should "support scalar lists in previous values" ignore {
108150
testInitializedWebsocket(project) { wsClient =>
109151
wsClient.sendMessage(

0 commit comments

Comments
 (0)