Skip to content

Commit

Permalink
Update sangria to 2.0.0-RC1 and misc updates / fixes (#87)
Browse files Browse the repository at this point in the history
* Update sangria to 2.0.0-RC1 and misc updates / fixes

* Fixed the doubled escape sign $ while generating the source
  • Loading branch information
fspillner authored Apr 27, 2020
1 parent 7162a01 commit fcd5727
Show file tree
Hide file tree
Showing 19 changed files with 186 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: scala
matrix:
include:
- jdk: openjdk8
scala: 2.12.7
scala: 2.12.11
env: COMMAND=validate

script:
Expand Down
13 changes: 7 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.2.6
sbt.version=1.3.10
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions src/main/scala/rocks/muki/graphql/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

/**
Expand All @@ -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(" | ")}]")
}
Expand All @@ -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"))
}
9 changes: 6 additions & 3 deletions src/sbt-test/codegen/generate-schema-and-code/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/sbt-test/schema/schema-snippet/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
7 changes: 4 additions & 3 deletions src/sbt-test/validation/query/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
"org.sangria-graphql" %% "sangria" % "2.0.0-RC1",
"org.sangria-graphql" %% "sangria-circe" % "1.3.0"
)

31 changes: 18 additions & 13 deletions src/sbt-test/validation/schema/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Def
.task(
GraphQLSchemaLoader
.fromFile(baseDirectory.value / "schemas" / "product-broken.graphql")
.loadSchema()
)
.taskValue
)

2 changes: 1 addition & 1 deletion src/sbt-test/validation/schema/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# validation should fail with the broken.graphql file
> graphqlValidateSchema build product-schema
-> graphqlValidateSchema build product-schema-broken
-> graphqlValidateSchema build product-schema-broken
7 changes: 7 additions & 0 deletions src/test/resources/apollo/starwars-circe/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/apollo/starwars-imports/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
}
}
7 changes: 7 additions & 0 deletions src/test/resources/apollo/starwars/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))

Expand Down
Loading

0 comments on commit fcd5727

Please sign in to comment.