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

FIX #18 Remove the IntegrationTest scope from the GraphQLQueryPlugin #20

Merged
merged 6 commits into from
May 21, 2018
Merged
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
1 change: 0 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ libraryDependencies ++= Seq(

// scripted test settings
scriptedLaunchOpts += "-Dproject.version=" + version.value
scriptedLaunchOpts += "-Dcodegen.samples.dir=" + ((baseDirectory in ThisBuild).value / "src/test/resources")
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I isolated the test cases as it was hard to refactor things due to the inter-dependencies.
Also the scripted tests acted as unit tests as well, which is too much IMHO.


// project meta data
licenses := Seq(
Expand Down
94 changes: 44 additions & 50 deletions src/main/scala/rocks/muki/graphql/GraphQLQueryPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,60 @@ object GraphQLQueryPlugin extends AutoPlugin {
val graphqlValidateQueries: TaskKey[Unit] =
taskKey[Unit]("validate all queries in the graphql source directory")

val graphqlQueryDirectory: SettingKey[File] =
settingKey[File]("directory that contains all graphql queries")
}

import autoImport._
import GraphQLSchemaPlugin.autoImport._

// TODO separate these into two auto plugins
override def projectSettings: Seq[Setting[_]] =
pluginSettings(Compile) ++ pluginSettings(IntegrationTest)
override def projectSettings: Seq[Setting[_]] = Seq(
sourceDirectory in (Test, graphqlValidateQueries) := (sourceDirectory in Test).value / "graphql",
graphqlValidateQueries := {
val log = streams.value.log
val schemaFile = IO.read(graphqlSchemaGen.value)
val schemaDocument = QueryParser
.parse(schemaFile)
.getOrElse(
sys.error(
"Invalid graphql schema generated by `graphqlSchemaGen` task")
)
val schema = Schema.buildFromAst(schemaDocument)

private def pluginSettings(config: Configuration): Seq[Setting[_]] =
inConfig(config)(
Seq(
graphqlQueryDirectory := (sourceDirectory in Compile).value / "graphql",
graphqlValidateQueries := {
val log = streams.value.log
val schemaFile = IO.read(graphqlSchemaGen.value)
val schemaDocument = QueryParser
.parse(schemaFile)
.getOrElse(
sys.error(
"Invalid graphql schema generated by `graphqlSchemaGen` task")
val graphqlQueryDirectory =
(sourceDirectory in (Test, graphqlValidateQueries)).value
log.info(s"Checking graphql files in $graphqlQueryDirectory")
val graphqlFiles = (graphqlQueryDirectory ** "*.graphql").get
val violations = graphqlFiles.flatMap {
file =>
log.info(s"Validate ${file.getPath}")
val query = IO.read(file)
val violations = QueryParser
.parse(query)
.fold(
error => Vector(InvalidQueryValidation(error)),
query => QueryValidator.default.validateQuery(schema, query)
)
val schema = Schema.buildFromAst(schemaDocument)

log.info(s"Checking graphql files in ${graphqlQueryDirectory.value}")
val graphqlFiles = (graphqlQueryDirectory.value ** "*.graphql").get
val violations = graphqlFiles.flatMap {
file =>
log.info(s"Validate ${file.getPath}")
val query = IO.read(file)
val violations = QueryParser
.parse(query)
.fold(
error => Vector(InvalidQueryValidation(error)),
query => QueryValidator.default.validateQuery(schema, query)
)
if (violations.nonEmpty) {
log.error(s"File: ${file.getAbsolutePath}")
log.error("## Query ##")
log.error(query)
log.error("## Violations ##")
violations.foreach(v => log.error(v.errorMessage))
List(QueryViolations(file, query, violations))
} else {
Nil
}
}

if (violations.nonEmpty) {
log.error("Validation errors in")
violations.foreach { queryViolation =>
log.error(s"File: ${queryViolation.file.getAbsolutePath}")
}
quietError("Some queries contain validation violations")
log.error(s"File: ${file.getAbsolutePath}")
log.error("## Query ##")
log.error(query)
log.error("## Violations ##")
violations.foreach(v => log.error(v.errorMessage))
List(QueryViolations(file, query, violations))
} else {
Nil
}
log.success(s"All ${graphqlFiles.size} graphql files are valid")
}

if (violations.nonEmpty) {
log.error("Validation errors in")
violations.foreach { queryViolation =>
log.error(s"File: ${queryViolation.file.getAbsolutePath}")
}
))
quietError("Some queries contain validation violations")
}
log.success(s"All ${graphqlFiles.size} graphql files are valid")
}
)

/**
* Aggregates violations for a single file
Expand Down
21 changes: 9 additions & 12 deletions src/sbt-test/codegen/generate-named/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ scalaVersion := "2.12.4"

graphqlCodegenStyle := Sangria

val StarWarsDir = file(sys.props("codegen.samples.dir")) / "starwars"

graphqlCodegenSchema := StarWarsDir / "schema.graphql"
resourceDirectories in graphqlCodegen += StarWarsDir
graphqlCodegenSchema := baseDirectory.value / "schema.graphql"
includeFilter in graphqlCodegen := "MultiQuery.graphql"
name in graphqlCodegen := "MultiQueryApi"


TaskKey[Unit]("check") := {
val file = graphqlCodegen.value.head
val expected = StarWarsDir / "MultiQuery.scala"
val files = graphqlCodegen.value

assert(files.length == 1, s"Sangria code should only generated one file, but got ${files.length}.\n${files.mkString("\n")}")

assert(file.exists)
val file = files.head
assert(file.exists, s"$file could not be found")

// Drop the package line before comparing
val compare = IO.readLines(file).drop(1).mkString("\n").trim == IO.read(expected).trim
if (!compare)
s"diff -u $expected $file".!
assert(compare, s"$file does not equal $expected")
val content = IO.read(file)
assert(content.contains("object MultiQueryApi"), s"MultiQueryApi not presented in generated code\n${content}")
}
83 changes: 83 additions & 0 deletions src/sbt-test/codegen/generate-named/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# A character in the Star Wars Trilogy
interface Character {
# The id of the character.
id: String!

# The name of the character.
name: String

# The friends of the character, or an empty list if they have none.
friends: [Character]

# Which movies they appear in.
appearsIn: [Episode]

# Where are they from and how they came to be who they are.
secretBackstory: String
}

# A mechanical creature in the Star Wars universe.
type Droid implements Character {
# The id of the droid.
id: String!

# The name of the droid.
name: String

# The friends of the droid, or an empty list if they have none.
friends: [Character]

# Which movies they appear in.
appearsIn: [Episode]

# The primary function of the droid.
primaryFunction: String

# Where are they from and how they came to be who they are.
secretBackstory: String
}

# One of the films in the Star Wars Trilogy
enum Episode {
# Released in 1977.
NEWHOPE

# Released in 1980.
EMPIRE

# Released in 1983.
JEDI
}

# A humanoid creature in the Star Wars universe.
type Human implements Character {
# The id of the human.
id: String!

# The name of the human.
name: String

# The friends of the human, or an empty list if they have none.
friends: [Character]

# Which movies they appear in.
appearsIn: [Episode]

# The home planet of the human, or null if unknown.
homePlanet: String

# Where are they from and how they came to be who they are.
secretBackstory: String
}

type Query {
hero(
# If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.
episode: Episode): Character!
human(
# id of the character
id: String!): Human
droid(
# id of the character
id: String!): Droid!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
query HeroAndFriends {
hero {
name
friends {
name
}
}
}

query HeroAndNestedFriends {
hero {
name
friends {
name
friends {
name
friends {
name
friends {
name
}
}
}
}
}
}

query FragmentExample {
human(id: "1003") {
...Common
homePlanet
}

droid(id: "2001") {
...Common
primaryFunction
}
}

query VariableExample($humanId: String!){
human(id: $humanId) {
name,
homePlanet,
friends {
name
}
}
}

fragment Common on Character {
name
appearsIn
}
19 changes: 7 additions & 12 deletions src/sbt-test/codegen/generate-schema-and-code/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import scala.sys.process._
name := "test"
scalaVersion in ThisBuild := "2.12.4"

val StarWarsDir = file(sys.props("codegen.samples.dir")) / "starwars"

val server = project
.enablePlugins(GraphQLSchemaPlugin)
.settings(
Expand All @@ -18,21 +16,18 @@ val client = project
.settings(
graphqlCodegenStyle := Sangria,
graphqlCodegenSchema := (graphqlSchemaGen in server).value,
resourceDirectories in graphqlCodegen += StarWarsDir,
includeFilter in graphqlCodegen := "MultiQuery.graphql",
graphqlCodegenPackage := "com.example.client.api",
name in graphqlCodegen := "MultiQueryApi"
)

TaskKey[Unit]("check") := {
val file = (graphqlCodegen in client).value.head
val expected = StarWarsDir / "MultiQuery.scala"
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(file.exists)
val file = files.head
assert(file.exists, s"$file could not be found")

// Drop the package line before comparing
val compare = IO.readLines(file).drop(1).mkString("\n").trim == IO.read(expected).trim
if (!compare)
s"diff -u $expected $file".!
assert(compare, s"$file does not equal $expected")
val content = IO.read(file)
assert(content.contains("object MultiQueryApi"), s"MultiQueryApi not presented in generated code\n${content}")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
query HeroAndFriends {
hero {
name
friends {
name
}
}
}

query HeroAndNestedFriends {
hero {
name
friends {
name
friends {
name
friends {
name
friends {
name
}
}
}
}
}
}

query FragmentExample {
human(id: "1003") {
...Common
homePlanet
}

droid(id: "2001") {
...Common
primaryFunction
}
}

query VariableExample($humanId: String!){
human(id: $humanId) {
name,
homePlanet,
friends {
name
}
}
}

fragment Common on Character {
name
appearsIn
}
Loading