Skip to content

Commit

Permalink
Feature/sangria support (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
smiklos authored Jan 5, 2022
1 parent 8dc446a commit f2c88ed
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
run: sbt ++${{ matrix.scala }} test

- name: Compress target directories
run: tar cf targets.tar modules/tests/target modules/tethys/target modules/pureconfig/target modules/catsTagless/target modules/ciris/target modules/scalacheck/target modules/circeMagnolia/target modules/circe/target target modules/tethysMagnolia/target modules/cats/target modules/core/target modules/reactivemongo/target project/target
run: tar cf targets.tar modules/tests/target modules/tethys/target modules/pureconfig/target modules/catsTagless/target modules/ciris/target modules/scalacheck/target modules/circeMagnolia/target modules/circe/target target modules/tethysMagnolia/target modules/cats/target modules/core/target modules/sangria/target modules/reactivemongo/target project/target

- name: Upload target directories
uses: actions/upload-artifact@v2
Expand Down
10 changes: 10 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ lazy val derevo = project
scalacheck,
tethys,
tethysMagnolia,
sangria,
tests,
)

Expand Down Expand Up @@ -154,6 +155,15 @@ lazy val scalacheck =
)
.dependsOn(core)

lazy val sangria =
(project in file("modules/sangria"))
.settings(publishSettings)
.settings(
name := "derevo-sangria",
libraryDependencies ++= Seq(Dependencies.sangria),
)
.dependsOn(core)

lazy val tests =
(project in file("modules/tests"))
.settings(noPublishSettings)
Expand Down
4 changes: 4 additions & 0 deletions modules/core/src/main/scala/derevo/Derevo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class Derevo(val c: blackbox.Context) {
def delegateParam[TC[_], I, Arg](arg: c.Expr[Arg]): c.Expr[TC[I]] =
c.Expr(delegation(c.prefix.tree, Some((method, args) => q"$method($arg, ..$args)")))

def delegateParamsV[TC[_], I, Arg](arg: c.Expr[Arg]*): c.Expr[TC[I]] = {
c.Expr(delegation(c.prefix.tree, Some((method, args) => q"$method(..${arg.map(_.tree) ++ args})")))

}
def delegateParams2[TC[_], I, Arg1, Arg2](arg1: c.Expr[Arg1], arg2: c.Expr[Arg2]): c.Expr[TC[I]] =
c.Expr(delegation(c.prefix.tree, Some((method, args) => q"$method($arg1, $arg2, ..$args)")))

Expand Down
29 changes: 29 additions & 0 deletions modules/sangria/src/main/scala/derevo/sangria/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package derevo

import derevo.Derivation
import derevo.delegating
import derevo.Derevo
import _root_.sangria.schema._
import _root_.sangria.macros.derive._

package object sangria {

type Ctx = Any
type CtxObjectType[A] = ObjectType[Ctx, A]
type DeriveObjectSettingNoContext[A] = DeriveObjectSetting[Ctx, A]

@delegating("sangria.macros.derive.deriveInputObjectType")
object inputObjectType extends Derivation[InputObjectType] {

def apply[A](arg: DeriveInputObjectSetting*): InputObjectType[A] =
macro Derevo.delegateParamsV[InputObjectType, A, DeriveInputObjectSetting]

}

@delegating("sangria.macros.derive.deriveObjectType")
object objectType extends Derivation[CtxObjectType] {

def apply[A](arg: DeriveObjectSettingNoContext[A]*): CtxObjectType[A] =
macro Derevo.delegateParamsV[CtxObjectType, A, DeriveObjectSettingNoContext[A]]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package derevo.sangria

import derevo.derive
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import sangria.schema.InputObjectType
import sangria.macros.derive.InputObjectTypeName
import sangria.macros.derive.ExcludeInputFields
import sangria.macros.derive.ObjectTypeName
import sangria.macros.derive.RenameField

class SangriaDerivationSpec extends AnyFlatSpec with Matchers {
"Sangria input object derivation" should "derive input object without customization" in {
val derived = implicitly[InputObjectType[SangriaDerivationSpec.Input]]

assertResult("Input")(derived.name)
derived.fields.map(_.name) should contain theSameElementsAs List("stringParam", "intParam")
}

it should "derive input object with customization" in {
val derived =
implicitly[InputObjectType[SangriaDerivationSpec.InputCustom]]

assertResult("Renamed")(derived.name)
derived.fields.map(_.name) should contain theSameElementsAs List("stringParam", "fooInner")
}

"Sangria object derivation" should "derive object without customization" in {
val derived =
implicitly[CtxObjectType[SangriaDerivationSpec.Response]]

assertResult("Response")(derived.name)
derived.fields.map(_.name) should contain theSameElementsAs List("data")
}

"Sangria object derivation" should "derive object with customization" in {
val derived =
implicitly[CtxObjectType[SangriaDerivationSpec.ResponseCustom]]

assertResult("RenamedResponse")(derived.name)
derived.fields.map(_.name) should contain theSameElementsAs List("content")
}
}

object SangriaDerivationSpec {
@derive(inputObjectType()) case class Input(stringParam: String, intParam: Int)
@derive(inputObjectType(InputObjectTypeName("Renamed"), ExcludeInputFields("intParam"))) case class InputCustom(
stringParam: String,
intParam: Int,
fooInner: Input
)

@derive(objectType())
case class Response(data: String)

@derive(objectType(ObjectTypeName("RenamedResponse"), RenameField("data", "content")))
case class ResponseCustom(data: String)
}
3 changes: 3 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ object Dependencies {
val kindProjector = "0.13.2"

val macroParadise = "2.1.1"

val sangria = "2.1.6"
}

lazy val magnolia = "com.propensive" %% "magnolia" % Version.magnolia
Expand All @@ -56,6 +58,7 @@ object Dependencies {
lazy val tethysCore = "com.tethys-json" %% "tethys-core" % Version.tethys
lazy val tethysDerivation = "com.tethys-json" %% "tethys-derivation" % Version.tethys
lazy val scalacheck = "org.scalacheck" %% "scalacheck" % Version.scalacheck
lazy val sangria = "org.sangria-graphql" %% "sangria" % Version.sangria

lazy val circeParser = "io.circe" %% "circe-parser" % Version.circe
lazy val tethysJackson = "com.tethys-json" %% "tethys-jackson" % Version.tethys
Expand Down

0 comments on commit f2c88ed

Please sign in to comment.