-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix #4: Add code generator Ports the Scala code generator from sangria-codegen. * Fix formatting * Language resilient BuilderSpec the 'file not exist' output is localized, which causes this test to fail on non-english machines. * Upgrade to sbt 1.1.0 * Use sbt.IO and SchemaLoader * Refactor GraphQLSchemaPlugin into two separate plugins * Use newest feature in the test-project * fix autocompletion in graphqlSchema parser * Fix test-project * Scalafmt * Fix test by replacing tabs * Add newline in generated module and add better assertion output * Remove tabs from rebase * Add new scala source generator * Add GraphQLQuery trait generation * Generate additional types * Add test for input types * Add more documentation * Add apollo codegen style test and codegen style setting * Fix Sangria code generator and scripted tests * Scalafmt formatting
- Loading branch information
Showing
98 changed files
with
5,206 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.0.4 | ||
sbt.version=1.1.0 |
70 changes: 70 additions & 0 deletions
70
src/main/scala/rocks/muki/graphql/GraphQLCodegenPlugin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package rocks.muki.graphql | ||
|
||
import rocks.muki.graphql.codegen._ | ||
import rocks.muki.graphql.schema.SchemaLoader | ||
import sbt.Keys._ | ||
import sbt.{Result => _, _} | ||
|
||
import scala.meta._ | ||
|
||
object GraphQLCodegenPlugin extends AutoPlugin { | ||
|
||
override def requires: Plugins = GraphQLPlugin | ||
|
||
object autoImport { | ||
val graphqlCodegenSchema = taskKey[File]("GraphQL schema file") | ||
val graphqlCodegenQueries = taskKey[Seq[File]]("GraphQL query documents") | ||
|
||
val graphqlCodegenStyle = | ||
settingKey[CodeGenStyles.Style]("The resulting code generation style") | ||
|
||
val graphqlCodegenPackage = | ||
settingKey[String]("Package for the generated code") | ||
val graphqlCodegen = taskKey[Seq[File]]("Generate GraphQL API code") | ||
|
||
val Apollo = CodeGenStyles.Apollo | ||
val Sangria = CodeGenStyles.Sangria | ||
|
||
} | ||
import autoImport._ | ||
|
||
override def projectSettings: Seq[Setting[_]] = Seq( | ||
graphqlCodegenStyle := Apollo, | ||
graphqlCodegenSchema := (resourceDirectory in Compile).value / "schema.graphql", | ||
resourceDirectories in graphqlCodegen := (resourceDirectories in Compile).value, | ||
includeFilter in graphqlCodegen := "*.graphql", | ||
excludeFilter in graphqlCodegen := HiddenFileFilter, | ||
graphqlCodegenQueries := Defaults | ||
.collectFiles(resourceDirectories in graphqlCodegen, | ||
includeFilter in graphqlCodegen, | ||
excludeFilter in graphqlCodegen) | ||
.value, | ||
sourceGenerators in Compile += graphqlCodegen.taskValue, | ||
graphqlCodegenPackage := "graphql.codegen", | ||
name in graphqlCodegen := "GraphQLCodegen", | ||
graphqlCodegen := { | ||
val log = streams.value.log | ||
val targetDir = sourceManaged.value / "sbt-graphql" | ||
//val generator = ScalametaGenerator((name in graphqlCodegen).value) | ||
val queries = graphqlCodegenQueries.value | ||
log.info(s"Generate code for ${queries.length} queries") | ||
log.info( | ||
s"Use schema ${graphqlCodegenSchema.value} for query validation") | ||
|
||
val packageName = graphqlCodegenPackage.value | ||
val schema = | ||
SchemaLoader.fromFile(graphqlCodegenSchema.value).loadSchema() | ||
|
||
val moduleName = (name in graphqlCodegen).value | ||
val context = CodeGenContext(schema, | ||
targetDir, | ||
queries, | ||
packageName, | ||
moduleName, | ||
log) | ||
|
||
graphqlCodegenStyle.value(context) | ||
} | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package rocks.muki.graphql | ||
|
||
import sbt.Keys._ | ||
import sbt._ | ||
import rocks.muki.graphql.schema.{GraphQLSchemas, SchemaLoader} | ||
|
||
/** | ||
* == GraphQL Plugin == | ||
* | ||
* Root plugin for all other graphql plugins. Provides a schema registry that can be used for | ||
* | ||
* - validating queries against a specific schema | ||
* - comparing schemas | ||
* - code generation based on a specific schema | ||
* | ||
*/ | ||
object GraphQLPlugin extends AutoPlugin { | ||
|
||
object autoImport { | ||
|
||
/** | ||
* Helper to load schemas from different places | ||
*/ | ||
val GraphQLSchemaLoader: SchemaLoader.type = | ||
rocks.muki.graphql.schema.SchemaLoader | ||
|
||
val GraphQLSchema: rocks.muki.graphql.schema.GraphQLSchema.type = | ||
rocks.muki.graphql.schema.GraphQLSchema | ||
|
||
/** | ||
* Contains all schemas available in this build. | ||
* | ||
* @example Adding a new schema | ||
* {{{ | ||
* graphqlSchemas += GraphQLSchema( | ||
* "temporary", | ||
* "schema loaded from schema.json in the base directory", | ||
* SchemaLoader.fromFile(baseDirectory.value / "schema.json")), | ||
* }}} | ||
* | ||
*/ | ||
val graphqlSchemas: SettingKey[GraphQLSchemas] = | ||
settingKey[GraphQLSchemas]("all schemas available in this build") | ||
|
||
/** | ||
* Renders the given schema into a graphql file. | ||
* The input is the label in the graphqlSchemas setting. | ||
*/ | ||
val graphqlRenderSchema: InputKey[File] = | ||
inputKey[File]("renders the given schema to a graphql file") | ||
|
||
} | ||
import autoImport._ | ||
|
||
override def projectSettings: Seq[Setting[_]] = Seq( | ||
graphqlSchemas := GraphQLSchemas(), | ||
// schema rendering | ||
target in graphqlRenderSchema := (target in Compile).value / "graphql", | ||
graphqlRenderSchema := graphqlRenderSchemaTask.evaluated | ||
) | ||
|
||
private val graphqlRenderSchemaTask = Def.inputTaskDyn[File] { | ||
val log = streams.value.log | ||
val schemaDefinition = singleGraphQLSchemaParser.parsed | ||
val file = (target in graphqlRenderSchema).value / s"${schemaDefinition.label}.graphql" | ||
log.info(s"Rendering schema to: ${file.getPath}") | ||
|
||
Def.task { | ||
val schema = schemaDefinition.schemaTask.value | ||
IO.write(file, schema.renderPretty) | ||
file | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.