From fcd57279a04f3756be4aca45b5338323008290d8 Mon Sep 17 00:00:00 2001 From: Fabian Spillner Date: Mon, 27 Apr 2020 13:21:29 +0200 Subject: [PATCH] Update sangria to 2.0.0-RC1 and misc updates / fixes (#87) * Update sangria to 2.0.0-RC1 and misc updates / fixes * Fixed the doubled escape sign $ while generating the source --- .travis.yml | 2 +- build.sbt | 13 +-- project/build.properties | 2 +- .../codegen/ApolloSourceGenerator.scala | 14 +-- .../scala/rocks/muki/graphql/package.scala | 14 +-- .../generate-schema-and-code/build.sbt | 9 +- src/sbt-test/schema/schema-snippet/build.sbt | 4 +- src/sbt-test/validation/query/build.sbt | 7 +- src/sbt-test/validation/schema/build.sbt | 31 ++++--- src/sbt-test/validation/schema/test | 2 +- .../apollo/starwars-circe/schema.graphql | 7 ++ .../apollo/starwars-imports/schema.graphql | 7 ++ .../SearchQueryWithArgumentFragment.graphql | 20 ++++ .../SearchQueryWithArgumentFragment.scala | 32 +++++++ .../resources/apollo/starwars/schema.graphql | 7 ++ .../graphql/codegen/ScalametaUtilsSpec.scala | 5 +- .../style/apollo/ApolloCodegenBaseSpec.scala | 5 +- .../sangria/SangriaCodegenBaseSpec.scala | 5 +- test-project/build.sbt | 92 ++++++++++--------- 19 files changed, 186 insertions(+), 92 deletions(-) create mode 100644 src/test/resources/apollo/starwars/SearchQueryWithArgumentFragment.graphql create mode 100644 src/test/resources/apollo/starwars/SearchQueryWithArgumentFragment.scala diff --git a/.travis.yml b/.travis.yml index 7b5d3df..ac31c2a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: scala matrix: include: - jdk: openjdk8 - scala: 2.12.7 + scala: 2.12.11 env: COMMAND=validate script: diff --git a/build.sbt b/build.sbt index 07bfdf2..eb20a6c 100644 --- a/build.sbt +++ b/build.sbt @@ -3,17 +3,18 @@ organization := "rocks.muki" sbtPlugin := true enablePlugins(SbtPlugin) -val circeVersion = "0.11.1" -val catsVersion = "1.5.0" +val circeVersion = "0.13.0" +val catsVersion = "2.1.1" libraryDependencies ++= Seq( - "org.sangria-graphql" %% "sangria" % "1.4.2", + "org.sangria-graphql" %% "sangria" % "2.0.0-RC1", "io.circe" %% "circe-core" % circeVersion, "io.circe" %% "circe-jackson28" % circeVersion, "org.typelevel" %% "cats-core" % catsVersion, "org.typelevel" %% "cats-testkit" % catsVersion % Test, - "org.scalaj" %% "scalaj-http" % "2.3.0", - "org.scalameta" %% "scalameta" % "4.0.0", - "org.scalatest" %% "scalatest" % "3.0.5" % Test + "org.scalaj" %% "scalaj-http" % "2.4.2", + "org.scalameta" %% "scalameta" % "4.3.9", + "org.scalatest" %% "scalatest" % "3.1.1" % Test, + "org.typelevel" %% "cats-testkit-scalatest" % "1.0.1" % Test ) // scripted test settings diff --git a/project/build.properties b/project/build.properties index 7c58a83..797e7cc 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.2.6 +sbt.version=1.3.10 diff --git a/src/main/scala/rocks/muki/graphql/codegen/ApolloSourceGenerator.scala b/src/main/scala/rocks/muki/graphql/codegen/ApolloSourceGenerator.scala index b3a7047..79dc44c 100644 --- a/src/main/scala/rocks/muki/graphql/codegen/ApolloSourceGenerator.scala +++ b/src/main/scala/rocks/muki/graphql/codegen/ApolloSourceGenerator.scala @@ -83,22 +83,22 @@ case class ApolloSourceGenerator( val data = operation.selection.fields.flatMap(selectionStats(_, List.empty)) - // render the document into the query object. - // replacing single $ with $$ for escaping - val escapedDocumentString = - operation.original.renderPretty.replaceAll("\\$", "\\$\\$") + // render the operation into the query object. + val operationString = + operation.original.renderPretty // add the fragments to the query as well - val escapedFragmentString = Option(document.original.fragments) + val fragmentsString = Option(document.original.fragments) .filter(_.nonEmpty) .map { fragments => fragments.values - .map(_.renderPretty.replaceAll("\\$", "\\$\\$")) + .map(_.renderPretty) .mkString("\n\n", "\n", "") } .getOrElse("") - val documentString = escapedDocumentString + escapedFragmentString + // render the document into the query object + val documentString = operationString + fragmentsString val graphqlDocument = Term.Interpolate( Term.Name("graphql"), Lit.String(documentString) :: Nil, diff --git a/src/main/scala/rocks/muki/graphql/package.scala b/src/main/scala/rocks/muki/graphql/package.scala index 8541547..f5a79d4 100644 --- a/src/main/scala/rocks/muki/graphql/package.scala +++ b/src/main/scala/rocks/muki/graphql/package.scala @@ -28,7 +28,7 @@ package object graphql { val gqlSchema = graphqlSchemas.value val labels = gqlSchema.schemas.map(_.label) // create a dependent parser. A label can only be selected once - schemaLabelParser(labels).map(label => schemaOrError(label, gqlSchema)) + schemaLabelParser(labels).flatMap(label => schemaOrError(label, gqlSchema)) } /** @@ -41,8 +41,8 @@ package object graphql { // create a depended parser. A label can only be selected once schemaLabelParser(labels).flatMap { case selectedLabel if labels.contains(selectedLabel) => - success(schemaOrError(selectedLabel, gqlSchemas)) ~ schemaLabelParser(labels.filterNot(_ == selectedLabel)) - .map(label => schemaOrError(label, gqlSchemas)) + schemaOrError(selectedLabel, gqlSchemas) ~ schemaLabelParser(labels.filterNot(_ == selectedLabel)) + .flatMap(label => schemaOrError(label, gqlSchemas)) case selectedLabel => failure(s"$selectedLabel is not available. Use: [${labels.mkString(" | ")}]") } @@ -57,7 +57,9 @@ package object graphql { token(Space.? ~> schemaParser) } - private def schemaOrError(label: String, graphQLSchema: GraphQLSchemas): GraphQLSchema = - graphQLSchema.schemaByLabel.getOrElse(label, sys.error(s"The schema '$label' is not defined in graphqlSchemas")) - + private def schemaOrError(label: String, graphQLSchema: GraphQLSchemas): Parser[GraphQLSchema] = + graphQLSchema.schemaByLabel + .get(label) + .map(success(_)) + .getOrElse(failure(s"The schema '$label' is not defined in graphqlSchemas")) } diff --git a/src/sbt-test/codegen/generate-schema-and-code/build.sbt b/src/sbt-test/codegen/generate-schema-and-code/build.sbt index d61c06b..fe5cbd3 100644 --- a/src/sbt-test/codegen/generate-schema-and-code/build.sbt +++ b/src/sbt-test/codegen/generate-schema-and-code/build.sbt @@ -6,7 +6,7 @@ scalaVersion in ThisBuild := "2.12.4" val server = project .enablePlugins(GraphQLSchemaPlugin) .settings( - libraryDependencies += "org.sangria-graphql" %% "sangria" % "1.4.2", + libraryDependencies += "org.sangria-graphql" %% "sangria" % "2.0.0-RC1", graphqlSchemaSnippet := "com.example.starwars.TestSchema.StarWarsSchema" ) @@ -21,9 +21,12 @@ val client = project ) TaskKey[Unit]("check") := { - val files = (graphqlCodegen in client).value + val files = (graphqlCodegen in client).value - assert(files.length == 1, s"Sangria code should only generated one file, but got ${files.length}.\n${files.mkString("\n")}") + assert( + files.length == 1, + s"Sangria code should only generated one file, but got ${files.length}.\n${files.mkString("\n")}" + ) val file = files.head assert(file.exists, s"$file could not be found") diff --git a/src/sbt-test/schema/schema-snippet/build.sbt b/src/sbt-test/schema/schema-snippet/build.sbt index aa4cb6d..02eaf38 100644 --- a/src/sbt-test/schema/schema-snippet/build.sbt +++ b/src/sbt-test/schema/schema-snippet/build.sbt @@ -5,6 +5,6 @@ enablePlugins(GraphQLSchemaPlugin, GraphQLQueryPlugin) graphqlSchemaSnippet := "example.ProductSchema.schema" libraryDependencies ++= Seq( - "org.sangria-graphql" %% "sangria" % "1.4.2", - "org.sangria-graphql" %% "sangria-circe" % "1.1.0" + "org.sangria-graphql" %% "sangria" % "2.0.0-RC1", + "org.sangria-graphql" %% "sangria-circe" % "1.3.0" ) diff --git a/src/sbt-test/validation/query/build.sbt b/src/sbt-test/validation/query/build.sbt index 7e84a7f..b4d97f3 100644 --- a/src/sbt-test/validation/query/build.sbt +++ b/src/sbt-test/validation/query/build.sbt @@ -5,6 +5,7 @@ enablePlugins(GraphQLSchemaPlugin, GraphQLQueryPlugin) graphqlSchemaSnippet := "example.ProductSchema.schema" libraryDependencies ++= Seq( - "org.sangria-graphql" %% "sangria" % "1.4.2", - "org.sangria-graphql" %% "sangria-circe" % "1.1.0" -) \ No newline at end of file + "org.sangria-graphql" %% "sangria" % "2.0.0-RC1", + "org.sangria-graphql" %% "sangria-circe" % "1.3.0" +) + diff --git a/src/sbt-test/validation/schema/build.sbt b/src/sbt-test/validation/schema/build.sbt index 2ba749f..8900378 100644 --- a/src/sbt-test/validation/schema/build.sbt +++ b/src/sbt-test/validation/schema/build.sbt @@ -5,26 +5,31 @@ enablePlugins(GraphQLSchemaPlugin, GraphQLQueryPlugin) graphqlSchemaSnippet := "example.ProductSchema.schema" libraryDependencies ++= Seq( - "org.sangria-graphql" %% "sangria" % "1.4.2", - "org.sangria-graphql" %% "sangria-circe" % "1.1.0" + "org.sangria-graphql" %% "sangria" % "2.0.0-RC1", + "org.sangria-graphql" %% "sangria-circe" % "1.3.0" ) graphqlSchemas += GraphQLSchema( "product-schema", "fixed schema at schemas/product.graphql", - Def.task( - GraphQLSchemaLoader - .fromFile(baseDirectory.value / "schemas" / "product.graphql") - .loadSchema() - ).taskValue + Def + .task( + GraphQLSchemaLoader + .fromFile(baseDirectory.value / "schemas" / "product.graphql") + .loadSchema() + ) + .taskValue ) graphqlSchemas += GraphQLSchema( "product-schema-broken", "fixed schema at schemas/product-broken.graphql", - Def.task( - GraphQLSchemaLoader - .fromFile(baseDirectory.value / "schemas" / "product-broken.graphql") - .loadSchema() - ).taskValue -) \ No newline at end of file + Def + .task( + GraphQLSchemaLoader + .fromFile(baseDirectory.value / "schemas" / "product-broken.graphql") + .loadSchema() + ) + .taskValue +) + diff --git a/src/sbt-test/validation/schema/test b/src/sbt-test/validation/schema/test index e0693f2..7339bb3 100644 --- a/src/sbt-test/validation/schema/test +++ b/src/sbt-test/validation/schema/test @@ -1,3 +1,3 @@ # validation should fail with the broken.graphql file > graphqlValidateSchema build product-schema --> graphqlValidateSchema build product-schema-broken \ No newline at end of file +-> graphqlValidateSchema build product-schema-broken diff --git a/src/test/resources/apollo/starwars-circe/schema.graphql b/src/test/resources/apollo/starwars-circe/schema.graphql index 23f6d1e..5450fff 100644 --- a/src/test/resources/apollo/starwars-circe/schema.graphql +++ b/src/test/resources/apollo/starwars-circe/schema.graphql @@ -49,6 +49,12 @@ enum Episode { JEDI } +# Length units +enum LengthUnit { + METER + FEET +} + # A humanoid creature in the Star Wars universe. type Human implements Character { # The id of the human. @@ -73,6 +79,7 @@ type Human implements Character { type Starship { id: ID! name: String + length(unit: LengthUnit = METER): Float } # A search result can have different types diff --git a/src/test/resources/apollo/starwars-imports/schema.graphql b/src/test/resources/apollo/starwars-imports/schema.graphql index 23f6d1e..5450fff 100644 --- a/src/test/resources/apollo/starwars-imports/schema.graphql +++ b/src/test/resources/apollo/starwars-imports/schema.graphql @@ -49,6 +49,12 @@ enum Episode { JEDI } +# Length units +enum LengthUnit { + METER + FEET +} + # A humanoid creature in the Star Wars universe. type Human implements Character { # The id of the human. @@ -73,6 +79,7 @@ type Human implements Character { type Starship { id: ID! name: String + length(unit: LengthUnit = METER): Float } # A search result can have different types diff --git a/src/test/resources/apollo/starwars/SearchQueryWithArgumentFragment.graphql b/src/test/resources/apollo/starwars/SearchQueryWithArgumentFragment.graphql new file mode 100644 index 0000000..8406a30 --- /dev/null +++ b/src/test/resources/apollo/starwars/SearchQueryWithArgumentFragment.graphql @@ -0,0 +1,20 @@ +query SearchQueryWithArgumentFragment($text: String!, $lengthUnit: LengthUnit = METER) { + search(text: $text) { + ...StarshipDetail + ...HumanDetail + ...DroidDetail + } +} + +fragment StarshipDetail on Starship { + name + length(unit: $lengthUnit) +} + +fragment HumanDetail on Human { + name +} + +fragment DroidDetail on Droid { + name +} diff --git a/src/test/resources/apollo/starwars/SearchQueryWithArgumentFragment.scala b/src/test/resources/apollo/starwars/SearchQueryWithArgumentFragment.scala new file mode 100644 index 0000000..60244b1 --- /dev/null +++ b/src/test/resources/apollo/starwars/SearchQueryWithArgumentFragment.scala @@ -0,0 +1,32 @@ +import sangria.macros._ +import types._ +object SearchQueryWithArgumentFragment { + object SearchQueryWithArgumentFragment extends GraphQLQuery { + val document: sangria.ast.Document = graphql"""query SearchQueryWithArgumentFragment($$text: String!, $$lengthUnit: LengthUnit = METER) { + search(text: $$text) { + ...StarshipDetail + ...HumanDetail + ...DroidDetail + } +} + +fragment StarshipDetail on Starship { + name + length(unit: $$lengthUnit) +} +fragment HumanDetail on Human { + name +} +fragment DroidDetail on Droid { + name +}""" + case class Variables(text: String, lengthUnit: Option[LengthUnit]) + case class Data(search: List[Search]) + sealed trait Search { def name: Option[String] } + object Search { + case class Human(name: Option[String]) extends Search + case class Droid(name: Option[String]) extends Search + case class Starship(name: Option[String], length: Option[Float]) extends Search + } + } +} diff --git a/src/test/resources/apollo/starwars/schema.graphql b/src/test/resources/apollo/starwars/schema.graphql index 23f6d1e..5450fff 100644 --- a/src/test/resources/apollo/starwars/schema.graphql +++ b/src/test/resources/apollo/starwars/schema.graphql @@ -49,6 +49,12 @@ enum Episode { JEDI } +# Length units +enum LengthUnit { + METER + FEET +} + # A humanoid creature in the Star Wars universe. type Human implements Character { # The id of the human. @@ -73,6 +79,7 @@ type Human implements Character { type Starship { id: ID! name: String + length(unit: LengthUnit = METER): Float } # A search result can have different types diff --git a/src/test/scala/rocks/muki/graphql/codegen/ScalametaUtilsSpec.scala b/src/test/scala/rocks/muki/graphql/codegen/ScalametaUtilsSpec.scala index b0c529b..5a20e9b 100644 --- a/src/test/scala/rocks/muki/graphql/codegen/ScalametaUtilsSpec.scala +++ b/src/test/scala/rocks/muki/graphql/codegen/ScalametaUtilsSpec.scala @@ -1,10 +1,11 @@ package rocks.muki.graphql.codegen -import org.scalatest.{Matchers, WordSpec} +import org.scalatest.wordspec.AnyWordSpec +import org.scalatest.matchers.should.Matchers import scala.meta._ -class ScalametaUtilsSpec extends WordSpec with Matchers { +class ScalametaUtilsSpec extends AnyWordSpec with Matchers { "The ScalametaUtils" should { diff --git a/src/test/scala/rocks/muki/graphql/codegen/style/apollo/ApolloCodegenBaseSpec.scala b/src/test/scala/rocks/muki/graphql/codegen/style/apollo/ApolloCodegenBaseSpec.scala index 6d2bdd9..43ee368 100644 --- a/src/test/scala/rocks/muki/graphql/codegen/style/apollo/ApolloCodegenBaseSpec.scala +++ b/src/test/scala/rocks/muki/graphql/codegen/style/apollo/ApolloCodegenBaseSpec.scala @@ -15,7 +15,8 @@ */ package rocks.muki.graphql.codegen.style.apollo -import org.scalatest.{EitherValues, TryValues, WordSpec} +import org.scalatest.wordspec.AnyWordSpec +import org.scalatest.{EitherValues, TryValues} import java.io.File import rocks.muki.graphql.codegen.{ApolloSourceGenerator, DocumentLoader, TypedDocumentParser} @@ -26,7 +27,7 @@ import scala.meta._ import sbt._ abstract class ApolloCodegenBaseSpec(name: String, generator: String => ApolloSourceGenerator) - extends WordSpec + extends AnyWordSpec with EitherValues with TryValues { diff --git a/src/test/scala/rocks/muki/graphql/codegen/style/sangria/SangriaCodegenBaseSpec.scala b/src/test/scala/rocks/muki/graphql/codegen/style/sangria/SangriaCodegenBaseSpec.scala index fd07c19..5f0814e 100644 --- a/src/test/scala/rocks/muki/graphql/codegen/style/sangria/SangriaCodegenBaseSpec.scala +++ b/src/test/scala/rocks/muki/graphql/codegen/style/sangria/SangriaCodegenBaseSpec.scala @@ -18,7 +18,8 @@ package rocks.muki.graphql.codegen.style.sangria import java.io.File -import org.scalatest.{EitherValues, WordSpec} +import org.scalatest.wordspec.AnyWordSpec +import org.scalatest.EitherValues import rocks.muki.graphql.codegen.{DocumentLoader, ScalametaGenerator, TypedDocumentParser} import rocks.muki.graphql.schema.SchemaLoader import sangria.schema.Schema @@ -28,7 +29,7 @@ import scala.io.Source import scala.meta._ abstract class SangriaCodegenBaseSpec(name: String, schema: Option[Schema[_, _]] = None) - extends WordSpec + extends AnyWordSpec with EitherValues { def this(name: String, schema: Schema[_, _]) = this(name, Some(schema)) diff --git a/test-project/build.sbt b/test-project/build.sbt index d8f308a..4a7f503 100644 --- a/test-project/build.sbt +++ b/test-project/build.sbt @@ -1,61 +1,67 @@ +lazy val root = project + .in(file(".")) + .aggregate(server, client) -lazy val root = project.in(file(".")) - .aggregate(server, client) - -lazy val server = project.in(file("server")) - .enablePlugins(GraphQLSchemaPlugin, GraphQLQueryPlugin) - .settings(commonSettings) - .settings( - graphqlSchemaSnippet := "example.StarWarsSchema.schema" - ) - .settings( - addCommandAlias("validateStarWars", "graphqlValidateSchema build starwars") - ) +lazy val server = project + .in(file("server")) + .enablePlugins(GraphQLSchemaPlugin, GraphQLQueryPlugin) + .settings(commonSettings) + .settings( + graphqlSchemaSnippet := "example.StarWarsSchema.schema" + ) + .settings( + addCommandAlias("validateStarWars", "graphqlValidateSchema build starwars") + ) -lazy val client = project.in(file("client")) - .enablePlugins(GraphQLCodegenPlugin, GraphQLQueryPlugin) - .settings(commonSettings) - .settings( - graphqlCodegenStyle := Apollo, - graphqlCodegenJson := JsonCodec.Circe, - graphqlCodegenSchema := graphqlRenderSchema.toTask("starwars-local").value, - graphqlCodegenImports ++= List("java.time._"), - graphqlCodegenPackage := "rocks.muki.graphql", - graphqlSchemas += GraphQLSchema( - "starwars-local", - "starwars schema at server/src/main/resources", - Def.task( +lazy val client = project + .in(file("client")) + .enablePlugins(GraphQLCodegenPlugin, GraphQLQueryPlugin) + .settings(commonSettings) + .settings( + graphqlCodegenStyle := Apollo, + graphqlCodegenJson := JsonCodec.Circe, + graphqlCodegenSchema := graphqlRenderSchema.toTask("starwars-local").value, + graphqlCodegenImports ++= List("java.time._"), + graphqlCodegenPackage := "rocks.muki.graphql", + graphqlSchemas += GraphQLSchema( + "starwars-local", + "starwars schema at server/src/main/resources", + Def + .task( GraphQLSchemaLoader .fromFile((resourceDirectory in (server, Compile)).value / "schema.graphql") .loadSchema() - ).taskValue - ), - name in graphqlCodegen := "Api", - // includeFilter in graphqlCodegen := "product.graphql" - libraryDependencies ++= Seq( - "io.circe" %% "circe-core", - "io.circe" %% "circe-generic", - "io.circe" %% "circe-parser" - ).map(_ % "0.9.3") - ) + ) + .taskValue + ), + name in graphqlCodegen := "Api", + // includeFilter in graphqlCodegen := "product.graphql" + libraryDependencies ++= Seq( + "io.circe" %% "circe-core", + "io.circe" %% "circe-generic", + "io.circe" %% "circe-parser" + ).map(_ % "0.9.3") + ) lazy val commonSettings = Seq( version := "0.4", scalaVersion := "2.12.4", organization := "rocks.muki", libraryDependencies ++= Seq( - "org.sangria-graphql" %% "sangria" % "1.4.2", - "org.sangria-graphql" %% "sangria-circe" % "1.2.0" + "org.sangria-graphql" %% "sangria" % "2.0.0-RC1", + "org.sangria-graphql" %% "sangria-circe" % "1.3.0" ), // define schemas available in all builds graphqlSchemas += GraphQLSchema( "starwars", "starwars schema at http://try.sangria-graphql.org/graphql", - Def.task( - GraphQLSchemaLoader - .fromIntrospection("http://try.sangria-graphql.org/graphql", streams.value.log) - .withHeaders("User-Agent" -> s"sbt-graphql/${version.value}") - .loadSchema() - ).taskValue + Def + .task( + GraphQLSchemaLoader + .fromIntrospection("http://try.sangria-graphql.org/graphql", streams.value.log) + .withHeaders("User-Agent" -> s"sbt-graphql/${version.value}") + .loadSchema() + ) + .taskValue ) )