Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SubscriptionValue public #869

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions modules/core/src/main/scala/sangria/schema/Context.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import sangria.marshalling._
import sangria.ast.SourceMapper
import sangria.{ast, introspection}
import sangria.execution.deferred.Deferred
import sangria.streaming.SubscriptionStream
import sangria.streaming.{SubscriptionStream, SubscriptionStreamLike}
import sangria.util.Cache

import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -163,7 +163,7 @@ object UpdateCtx {
new UpdateCtx(action, newCtx)
}

private[sangria] case class SubscriptionValue[Ctx, Val, S[_]](
case class SubscriptionValue[Ctx, Val, S[_]](
source: Val,
stream: SubscriptionStream[S])
extends LeafAction[Ctx, Val] {
Expand All @@ -172,6 +172,13 @@ private[sangria] case class SubscriptionValue[Ctx, Val, S[_]](
throw new IllegalStateException(
"`map` is not supported subscription actions. Action is only intended for internal use.")
}
object SubscriptionValue {
def apply[Ctx, Val, StreamSource, Res, Out](source: StreamSource)(
implicit stream: SubscriptionStreamLike[StreamSource, Action, Ctx, Res, Out]): SubscriptionValue[Ctx, StreamSource, stream.StreamSource] = {
val s = stream.subscriptionStream
SubscriptionValue(source, s)
}
}

case class ProjectionName(name: String) extends FieldTag
case object ProjectionExclude extends FieldTag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,50 @@ class StreamSpec extends AnyWordSpec with Matchers with FutureResultSupport {
result should (have(size(1)).and(
contain("""{"data": {"letters": "a", "numbers": 10}}""".parseJson)))
}

"work with materialized schemas" in {
import _root_.monix.reactive.Observable
import sangria.streaming.monix._

val schemaDefinition = gql"""
type Subscription {
letters: String!
numbers: Int!
}

type Query {
hello: String
}
"""

val builder = ResolverBasedAstSchemaBuilder[Unit](
FieldResolver.map(
"Query" -> Map("hello" -> (_ => "world")),
"Subscription" -> Map(
"letters" -> (_ => SubscriptionValue(Observable("a", "b").map(Action(_)))),
"numbers" -> (_ => SubscriptionValue(Observable(1, 2).map(Action(_)))),
)
)
)

val schema = Schema.buildFromAst(schemaDefinition, builder)

import sangria.execution.ExecutionScheme.Stream

val stream: Observable[JsValue] =
Executor.execute(
schema,
graphql"subscription { letters numbers }",
queryValidator = QueryValidator.default.withoutValidation[SingleFieldSubscriptions])

val result = stream.toListL.runToFuture.await(timeout)

result should (have(size(4))
.and(contain("""{"data": {"letters": "a"}}""".parseJson))
.and(contain("""{"data": {"letters": "b"}}""".parseJson))
.and(contain("""{"data": {"numbers": 1}}""".parseJson))
.and(contain("""{"data": {"numbers": 2}}""".parseJson)))
}
}
}
}