Skip to content

Commit

Permalink
Allow to compile and pass sangria core tests in Scala 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jchyb committed Jul 19, 2022
1 parent 783f888 commit a72721c
Show file tree
Hide file tree
Showing 39 changed files with 382 additions and 214 deletions.
6 changes: 6 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ version = 3.5.2

runner.dialect = scala213

fileOverride {
"glob:**/src/main/scala-3/**" {
runner.dialect = scala3
}
}

maxColumn = 100

// Vertical alignment is pretty, but leads to bigger diffs
Expand Down
15 changes: 5 additions & 10 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,16 @@ lazy val core = project
Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-oF"),
libraryDependencies ++= Seq(
// AST Visitor
if (isScala3.value) "org.sangria-graphql" %% "macro-visit" % "0.1.0-SNAPSHOT"
else "org.sangria-graphql" %% "macro-visit" % "0.1.3", // TODO needs release
"org.sangria-graphql" %% "macro-visit" % "0.2.0-RC1",
// Marshalling
if (isScala3.value)
"org.sangria-graphql" %% "sangria-marshalling-api" % "1.0.7+58-480b4c06-SNAPSHOT"
else "org.sangria-graphql" %% "sangria-marshalling-api" % "1.0.7", // TODO needs release
"org.sangria-graphql" %% "sangria-marshalling-api" % "1.0.8",
// Streaming
"org.sangria-graphql" %% "sangria-streaming-api" % "1.0.3",
// Testing
"co.fs2" %% "fs2-core" % "2.5.11" % Test,
"org.scalatest" %% "scalatest" % "3.2.12" % Test,
"org.sangria-graphql" %% "sangria-marshalling-testkit" % "1.0.4" % Test,
if (isScala3.value)
"org.sangria-graphql" %% "sangria-spray-json" % "0.1.0-SNAPSHOT" % Test // TODO needs release
else "org.sangria-graphql" %% "sangria-spray-json" % "1.0.2" % Test,
"org.sangria-graphql" %% "sangria-spray-json" % "1.0.3" % Test,
"org.sangria-graphql" %% "sangria-argonaut" % "1.0.2" % Test,
"org.sangria-graphql" %% "sangria-ion" % "2.0.1" % Test,
"org.sangria-graphql" %% "sangria-monix" % "2.0.1" % Test,
Expand All @@ -126,9 +121,9 @@ lazy val derivation = project
name := "sangria-derivation",
Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-oF"),
mimaPreviousArtifacts := Set("org.sangria-graphql" %% "sangria-derivation" % "3.0.0"),
// Macros
// Macros
libraryDependencies ++= (if (isScala3.value) Seq.empty
else Seq( "org.scala-lang" % "scala-reflect" % scalaVersion.value)),
else Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value)),
apiURL := {
val ver = CrossVersion.binaryScalaVersion(scalaVersion.value)
Some(url(s"https://www.javadoc.io/doc/org.sangria-graphql/sangria-derivation_$ver/latest/"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ParseMacro(context: blackbox.Context)
val c = context
}
with MacroAstLiftable {

import c.universe._

def impl(args: Expr[Any]*) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ trait ToExprGivens {
${ Expr(p) },
_root_.scala.Some(
new _root_.sangria.ast.DefaultSourceMapper(
${ Expr { doc.sourceMapper.get.id } },
${ Expr(doc.sourceMapper.get.id) },
_root_.sangria.parser.ParserConfig
.parboiledToSourceMapper(_root_.org.parboiled2.ParserInput(${
Expr(doc.source.get)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ class Resolver[Ctx](
case None =>
field.resolve match {
case pfn: Projector[Ctx, Any, _] =>
pfn(updatedCtx.asInstanceOf[Context[Ctx, Any]], collectProjections(path, field, astFields, pfn.maxLevel))
pfn(updatedCtx, collectProjections(path, field, astFields, pfn.maxLevel))
case fn =>
fn(updatedCtx)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ValueCoercionHelper[Ctx](
acc: marshaller.MapBuilder,
value: Option[Either[Vector[Violation], Trinary[marshaller.Node]]]
): marshaller.MapBuilder = {
val valueMapTyped: Any => marshaller.Node = valueMap.asInstanceOf[Any => marshaller.Node]
val valueMapTyped = valueMap.asInstanceOf[Any => marshaller.Node]

def locations =
inputFor match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ object ValueCollector {
marshaller,
fromInput.marshaller,
errors = errors,
valueMap =
((a: Any) => fromInput.fromResult(a.asInstanceOf[fromInput.marshaller.Node]))
.asInstanceOf[Any => Any],
valueMap = (v: Any) => fromInput.fromResult.asInstanceOf[Any => Any](v),
defaultValueInfo = defaultInfo,
undefinedValues = undefinedArgs,
isArgument = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sangria
import sangria.parser.QueryParser
import sangria.schema._

import sangria.util.tag.@@ // Scala 3 workaround
import sangria.util.tag.@@ // Scala 3 issue workaround
import sangria.marshalling.FromInput.CoercedScalaResult

package object introspection {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,11 @@ object ResolverBasedAstSchemaBuilder {
}

def defaultInputResolver[Ctx, In: InputUnmarshaller] =
FieldResolver[Ctx]({ case (_, _) =>
extractFieldValue[Ctx, In]
}, PartialFunction.empty)
FieldResolver[Ctx](
{ case (_, _) =>
extractFieldValue[Ctx, In]
},
PartialFunction.empty)

def defaultExistingInputResolver[Ctx, In: InputUnmarshaller] =
ExistingFieldResolver[Ctx] { case (_, _, _) =>
Expand Down Expand Up @@ -649,7 +651,10 @@ object ResolverBasedAstSchemaBuilder {
implicit val marshaller = gd.marshaller

resolve(
GenericDynamicDirectiveContext(astDir, node, createDynamicArgs(astDir)(marshaller)))
GenericDynamicDirectiveContext(
astDir,
node,
createDynamicArgs(astDir)(marshaller)))
}
}

Expand Down
7 changes: 5 additions & 2 deletions modules/core/src/main/scala/sangria/schema/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ sealed trait AbstractType extends Type with Named {
.flatMap(_.find(_.isInstanceOf(value)).asInstanceOf[Option[ObjectType[Ctx, _]]])
}

sealed trait MappedAbstractType[T] extends Type with AbstractType{
sealed trait MappedAbstractType[T] extends Type with AbstractType {
def contraMap(value: T): Any
}

Expand Down Expand Up @@ -652,7 +652,10 @@ object Field {
fieldType,
description,
arguments,
ctx => SubscriptionValue[Ctx, StreamSource, stream.StreamSource](resolve(ctx).asInstanceOf[stream.StreamSource[Any]], s),
ctx =>
SubscriptionValue[Ctx, StreamSource, stream.StreamSource](
resolve(ctx).asInstanceOf[stream.StreamSource[Any]],
s),
deprecationReason,
SubscriptionField[stream.StreamSource](s) +: tags,
complexity,
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/sangria/schema/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ package object schema {

val DefaultDeprecationReason = "No longer supported"

val ReasonArg: Argument[Option[String]] = Argument(
val ReasonArg: Argument[String] = Argument(
"reason",
OptionInputType(StringType),
description = "Explains why this element was deprecated, usually also including a " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ object SortedArraySet {
val first_from = members.get(from)
members.set(into, first_from)
into += 1
while({
from += 1
while ({
from += 1
(from < members.size() && members.get(from) == first_from)
}) ()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ class DeprecationTrackerSpec
var ctx: Option[Context[_, _]] = None

var enumValue: Option[Any] = None
var enum: Option[String] = None
var `enum`: Option[String] = None

def deprecatedFieldUsed[Ctx](ctx: Context[Ctx, _]) = {
times.incrementAndGet()

this.ctx = Some(ctx)
}

def deprecatedEnumValueUsed[T, Ctx](enum: EnumType[T], value: T, userContext: Ctx) = {
def deprecatedEnumValueUsed[T, Ctx](`enum`: EnumType[T], value: T, userContext: Ctx) = {
times.incrementAndGet()
this.enumValue = Some(value)
this.enum = Some(enum.name)
this.`enum` = Some(`enum`.name)
}
}

Expand Down Expand Up @@ -179,7 +179,7 @@ class DeprecationTrackerSpec
Executor.execute(schema, query, deprecationTracker = deprecationTracker).await

deprecationTracker.times.get should be(1)
deprecationTracker.enum should be(Some("TestEnum"))
deprecationTracker.`enum` should be(Some("TestEnum"))
deprecationTracker.enumValue should be(Some(2))
}

Expand Down Expand Up @@ -216,7 +216,7 @@ class DeprecationTrackerSpec
Executor.execute(schema, query, deprecationTracker = deprecationTracker).await

deprecationTracker.times.get should be(0)
deprecationTracker.enum should be(None)
deprecationTracker.`enum` should be(None)
deprecationTracker.enumValue should be(None)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ExceptionHandlingSpec
Field(
"success",
OptionType(StringType),
arguments = Argument("num", OptionInputType(IntType)) :: Nil,
arguments = List(Argument("num", OptionInputType(IntType))),
resolve = _ => "Yay"),
Field(
"errorInScalar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import scala.concurrent.ExecutionContext.Implicits.global
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import sangria.util.tag.@@ // Scala 3 issue workaround
import sangria.marshalling.FromInput.CoercedScalaResult

class ExecutorSchemaSpec extends AnyWordSpec with Matchers with FutureResultSupport {
case class Image(url: Option[String], width: Option[Int], height: Option[Int])

Expand Down Expand Up @@ -49,9 +52,11 @@ class ExecutorSchemaSpec extends AnyWordSpec with Matchers with FutureResultSupp
Field(
"pic",
OptionType(BlogImageType),
arguments = Argument("width", OptionInputType(IntType)) :: Argument(
"height",
OptionInputType(IntType)) :: Nil,
arguments = Argument[Option[Int @@ CoercedScalaResult]](
"width",
OptionInputType(IntType)) :: Argument[Option[
Int @@ CoercedScalaResult
]]("height", OptionInputType(IntType)) :: Nil,
resolve = ctx =>
for {
w <- ctx.argOpt[Int]("width")
Expand Down Expand Up @@ -87,7 +92,7 @@ class ExecutorSchemaSpec extends AnyWordSpec with Matchers with FutureResultSupp
Field(
"article",
OptionType(BlogArticleType),
arguments = Argument("id", OptionInputType(IDType)) :: Nil,
arguments = List(Argument("id", OptionInputType(IDType))),
resolve = ctx => ctx.argOpt[String]("id").flatMap(id => article(id.toInt))
),
Field(
Expand All @@ -103,7 +108,7 @@ class ExecutorSchemaSpec extends AnyWordSpec with Matchers with FutureResultSupp
Field(
"articleSubscribe",
OptionType(BlogArticleType),
arguments = Argument("id", OptionInputType(IDType)) :: Nil,
arguments = List(Argument("id", OptionInputType(IDType))),
resolve = ctx => ctx.argOpt[String]("id").flatMap(id => article(id.toInt))
))
)
Expand Down
13 changes: 10 additions & 3 deletions modules/core/src/test/scala/sangria/execution/ExecutorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import scala.concurrent.ExecutionContext.Implicits.global
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import sangria.util.tag.@@ // Scala 3 issue workaround
import sangria.marshalling.FromInput.CoercedScalaResult

class ExecutorSpec extends AnyWordSpec with Matchers with FutureResultSupport {
class TestSubject {
def a: Option[String] = Some("Apple")
Expand Down Expand Up @@ -123,8 +126,10 @@ class ExecutorSpec extends AnyWordSpec with Matchers with FutureResultSupport {
Field(
"pic",
OptionType(StringType),
arguments = Argument("size", OptionInputType(IntType)) :: Nil,
resolve = ctx => ctx.value.pic(ctx.argOpt[Int]("size"))),
arguments =
Argument[Option[Int @@ CoercedScalaResult]]("size", OptionInputType(IntType)) :: Nil,
resolve = ctx => ctx.value.pic(ctx.argOpt[Int]("size"))
),
Field("deep", OptionType(DeepDataType), resolve = _.value.deep),
Field("future", OptionType(DataType), resolve = _.value.future)
)
Expand Down Expand Up @@ -512,7 +517,9 @@ class ExecutorSpec extends AnyWordSpec with Matchers with FutureResultSupport {
fields[Unit, Unit](Field(
"b",
OptionType(StringType),
arguments = Argument("numArg", OptionInputType(IntType)) :: Argument(
arguments = Argument[Option[Int @@ CoercedScalaResult]](
"numArg",
OptionInputType(IntType)) :: Argument[Option[String @@ CoercedScalaResult]](
"stringArg",
OptionInputType(StringType)) :: Nil,
resolve = ctx => { resolvedArgs = ctx.args.raw; None }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class InputDocumentMaterializerSpec extends AnyWordSpec with Matchers with Strin
comments: Vector[Option[Comment]])

object MyJsonProtocol extends DefaultJsonProtocol {
implicit val commentFormat = jsonFormat2(Comment.apply)
implicit val articleFormat = jsonFormat4(Article.apply)
implicit val commentFormat: JsonFormat[Comment] = jsonFormat2(Comment.apply)
implicit val articleFormat: JsonFormat[Article] = jsonFormat4(Article.apply)
}

import MyJsonProtocol._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ import scala.concurrent.ExecutionContext.Implicits.global
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import sangria.util.tag.@@ // Scala 3 issue workaround
import sangria.marshalling.FromInput.CoercedScalaResult

class ScalarAliasSpec extends AnyWordSpec with Matchers with FutureResultSupport {
import ScalarAliasSpec._

case class User(id: UserId, id2: Option[UserId], name: String, num: Int Refined Positive)

case class RefineViolation(error: String) extends ValueCoercionViolation(error)

implicit val UserIdType = ScalarAlias[UserId, String](StringType, _.id, id => Right(UserId(id)))
implicit val UserIdType: ScalarAlias[UserId, String] =
ScalarAlias[UserId, String](StringType, _.id, id => Right(UserId(id)))

implicit val PositiveIntType = ScalarAlias[Int Refined Positive, Int](
IntType,
_.value,
i => refineV[Positive](i).left.map(RefineViolation))
implicit val PositiveIntType: ScalarAlias[Int Refined Positive, Int] =
ScalarAlias[Int Refined Positive, Int](
IntType,
_.value,
i => refineV[Positive](i).left.map(RefineViolation))

case object IDViolation extends ValueCoercionViolation("Invalid ID")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import scala.concurrent.ExecutionContext.Implicits.global
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import sangria.util.tag.@@ // Scala 3 issue workaround
import sangria.marshalling.FromInput.CoercedScalaResult

class ScalarMiddlewareSpec extends AnyWordSpec with Matchers with FutureResultSupport {
import ScalarMiddlewareSpec._

Expand Down Expand Up @@ -43,9 +46,15 @@ class ScalarMiddlewareSpec extends AnyWordSpec with Matchers with FutureResultSu
InputField("name", StringType)))

val IdArg = Argument("id", EncodedIdType)
val IdArgWithDefault = Argument("id", OptionInputType(EncodedIdType), defaultValue = "SOME_ID")
val IdArgWithDefault = Argument[Option[String @@ CoercedScalaResult], String](
"id",
OptionInputType(EncodedIdType),
defaultValue = "SOME_ID")
val IdArgWithValidDefault =
Argument("id", OptionInputType(EncodedIdType), defaultValue = "test-SOME_ID")
Argument[Option[String @@ CoercedScalaResult], String](
"id",
OptionInputType(EncodedIdType),
defaultValue = "test-SOME_ID")
val ComplexArg = Argument("c", ComplexInputType)
val ComplexArgWithDefault = Argument("c", ComplexInputWithDefaultType)
val ComplexArgWithValidDefault = Argument("c", ComplexInputWithValidDefaultType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import scala.concurrent.ExecutionContext.Implicits.global
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import sangria.util.tag.@@ // Scala 3 issue workaround
import sangria.marshalling.FromInput.CoercedScalaResult

class UnionInterfaceSpec
extends AnyWordSpec
with Matchers
Expand Down Expand Up @@ -369,7 +372,9 @@ class UnionInterfaceSpec
Field(
"quz",
OptionType(ListType(OptionType(QuzType))),
arguments = Argument("id", OptionInputType(ListInputType(StringType))) :: Nil,
arguments = Argument[Option[Seq[String @@ CoercedScalaResult]]](
"id",
OptionInputType(ListInputType(StringType))) :: Nil,
resolve = c =>
c.argOpt[Seq[String]]("id")
.map(queried => c.value.quz.filter(quz => queried.contains(quz.id)))
Expand Down
Loading

0 comments on commit a72721c

Please sign in to comment.