-
-
Notifications
You must be signed in to change notification settings - Fork 20
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 #4: Add code generator #15
Conversation
I didn't check the whether the naming of settings is consistent with the other plugins. Also Scalameta now is at version 2.0 so an upgrade might be in order. I still think we can get rid of Scalameta if we use another mean of formatting code as it is generated. |
Awesome :) I would add my changes once cleaned up to this pull request. I haven't used scala meta until now. Surely we could write our own |
object autoImport { | ||
val graphqlCodegenSchema = taskKey[File]("GraphQL schema file") | ||
val graphqlCodegenQueries = taskKey[Seq[File]]("GraphQL query documents") | ||
val graphqlCodegenPackage = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may change my settings to Codegen
instead of CodeGen
} | ||
|
||
result match { | ||
case Left(error) => sys.error("Failed to generate code: $error") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interpolation missing.
} | ||
) | ||
|
||
def parseIntrospectionSchemaFile(schemaFile: File): Result[Schema[_, _]] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try to refactor these things into one helper. There's already the SchemaLoader
which
I would enhance to match both, code and schema generation, use cases.
if (document == Builder.emptyDocumentResult) | ||
copy(document = validatedQuery) | ||
else | ||
copy(document = document.flatMap(doc => validatedQuery.map(doc.merge))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That puzzled me in the beginning a lot 😄
With this we get the "single module" structure in the output, right?
If we would like to build one module per query, we wouldn't need this?
/** | ||
* Generate code using Scalameta. | ||
*/ | ||
case class ScalametaGenerator(moduleName: Term.Name, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are not adding comments yet to the generated class. In the future we should do that
so users have a proper scaladoc ( if they use graphql comments).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, let's create a ticket.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in #16
|
||
val StarWarsDir = file(sys.props("codegen.samples.dir")) / "starwars" | ||
|
||
val server = project |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this example helped me a lot :) thanks
Ports the Scala code generator from sangria-codegen.
the 'file not exist' output is localized, which causes this test to fail on non-english machines.
Sorry, I rebased instead of merging from master :( |
Next round :) A couple of things
|
This PR is getting big. I'll make a review today. |
Indeed 😅 My deepest respect for figuring all this out |
And I want to incorporate your code generator along with the new "Apollo Style" generator. |
@@ -1 +1 @@ | |||
sbt.version=1.0.4 | |||
sbt.version=1.1.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.1.1 was recently released
@@ -17,58 +17,67 @@ object GraphQLQueryPlugin extends AutoPlugin { | |||
*/ | |||
val graphqlValidateQueries: TaskKey[Unit] = | |||
taskKey[Unit]("validate all queries in the graphql source directory") | |||
|
|||
val graphqlQueryDirectory: SettingKey[File] = | |||
settingKey[File]("graphql files") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description seems off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Will fix that
val schema = Schema.buildFromAst(schemaDocument) | ||
// TODO separate these into two auto plugins | ||
override def projectSettings: Seq[Setting[_]] = | ||
pluginSettings(Compile) ++ pluginSettings(IntegrationTest) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the goal of the it
settings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure at this point if the query validation is unit or integration test.
Then I decided to add them both, which is probably not the best idea. I'll remove the
integration test settings. I'll do that in #18
Nil | ||
val schema = Schema.buildFromAst(schemaDocument) | ||
|
||
log.info(s"Checking graphl files in ${graphqlQueryDirectory.value}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
graphl => graphql
@@ -1 +1 @@ | |||
sbt.version=1.0.2 | |||
sbt.version=1.1.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe 1.1.1
} | ||
|
||
result match { | ||
case Left(error) => sys.error("Failed to generate code: $error") | ||
case Left(error) => sys.error(s"Failed to generate code: $error") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
scalaVersion := "2.12.4", | ||
organization := "rocks.muki", | ||
libraryDependencies ++= Seq( | ||
"org.sangria-graphql" %% "sangria" % "1.3.2", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be useful to expose the sangria version used by the plugin for projects that want to keep them in sync?
@@ -17,5 +17,6 @@ TaskKey[Unit]("check") := { | |||
""".stripMargin.trim | |||
|
|||
assert(file.exists) | |||
assert(IO.read(file).trim == expected) | |||
val generated = IO.read(file).trim | |||
assert(generated == expected, s"Generated file:\n$generated") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
build.sbt
Outdated
@@ -9,7 +9,7 @@ libraryDependencies ++= Seq( | |||
"io.circe" %% "circe-core" % circeVersion, | |||
"io.circe" %% "circe-parser" % circeVersion, | |||
"org.scalaj" %% "scalaj-http" % "2.3.0", | |||
"org.scalameta" %% "scalameta" % "1.8.0", | |||
"org.scalameta" %% "scalameta" % "2.1.2", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's now also 3.x version. But I think that can wait.
/** | ||
* Generate code using Scalameta. | ||
*/ | ||
case class ScalametaGenerator(moduleName: Term.Name, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, let's create a ticket.
@jonas what do you think? Should we merge this and continue improving this in smaller commits :) |
Yes, please, let's merge already. 😀
…--
Jonas Fonseca
On Mar 15, 2018 08:47, "Nepomuk Seiler" <[email protected]> wrote:
@jonas <https://github.com/jonas> what do you think? Should we merge this
and continue improving this in smaller commits :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#15 (comment)>,
or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAg4Q-pGbc1gHGLhG64I5PDORf2M5cMks5temLggaJpZM4RauuN>
.
|
🎉 |
Yeah 🤩🤩 I'll start documenting ASAP. |
Ports the Scala code generator from sangria-codegen.
Refs #4