-
-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
16 changed files
with
340 additions
and
6 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
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
113 changes: 113 additions & 0 deletions
113
src/main/scala/rocks/muki/graphql/codegen/PreProcessors.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,113 @@ | ||
package rocks.muki.graphql.codegen | ||
|
||
import sbt._ | ||
|
||
import cats.syntax.either._ | ||
|
||
object PreProcessors { | ||
|
||
/** | ||
* Applies all preprocessors and returns a new file with the processed content | ||
* | ||
* @param graphqlFile the input graphql file | ||
* @param targetDir target directory where the processed file should be written | ||
* @param preProcessors the list of preprocessors | ||
* @return a new file with the processed content | ||
*/ | ||
def apply(graphqlFile: File, | ||
targetDir: File, | ||
preProcessors: Seq[PreProcessor]): Result[File] = { | ||
val processedFile = targetDir / graphqlFile.getName | ||
|
||
for { | ||
input <- Either.catchNonFatal(IO.read(graphqlFile)).leftMap { error => | ||
Failure(s"Failed to read $graphqlFile: ${error.getMessage}") | ||
} | ||
// poor-mans traverse | ||
processedContent <- preProcessors.foldLeft(Right(input): Result[String]) { | ||
case (Right(processedContent), processor) => | ||
processor(processedContent) | ||
case (error, _) => error | ||
} | ||
} yield { | ||
IO.write(processedFile, processedContent) | ||
processedFile | ||
} | ||
} | ||
|
||
/** | ||
* Applies the preprocessors to all given files | ||
* | ||
* @param graphqlFiles graphql input files | ||
* @param targetDir target directory where the processed file should be written | ||
* @param preProcessors the list of preprocessors | ||
* @return a list of new files with the processed content | ||
*/ | ||
def apply(graphqlFiles: Seq[File], | ||
targetDir: File, | ||
preProcessors: Seq[PreProcessor]): Result[Seq[File]] = { | ||
graphqlFiles | ||
.map(file => apply(file, targetDir, preProcessors)) | ||
.foldLeft[Result[List[File]]](Right(List.empty)) { | ||
case (Left(failure), Left(nextFailure)) => | ||
Left(Failure(failure.message + "\n" + nextFailure.message)) | ||
case (Left(failure), _) => Left(failure) | ||
case (_, Left(failure)) => Left(failure) | ||
case (Right(files), Right(file)) => Right(files :+ file) | ||
} | ||
} | ||
|
||
/** | ||
* Allows to import other graphql files (e.g. fragments) into an graphql file. | ||
* | ||
* Allowed characters in the path are | ||
* - everything that matches `\w`, e.g. characters, decimals and _ | ||
* - forward slashes `/` | ||
* - dots `.` | ||
* | ||
* @example {{{ | ||
* #import fragments/foo.graphql | ||
* }}} | ||
* @param rootDirectories a list of directories that should be used to resolve magic imports | ||
* @return | ||
*/ | ||
def magicImports(rootDirectories: Seq[File]): PreProcessor = graphQLFile => { | ||
// match the import file path | ||
val Import = "#import\\s*([\\w\\/\\.]*)".r | ||
|
||
val processed = graphQLFile.split(IO.Newline).map { | ||
case Import(filePath) => | ||
rootDirectories.map(dir => dir / filePath).find(_.exists()) match { | ||
case Some(importedGraphQLFile) => | ||
val importedGraphQLContent = IO.read(importedGraphQLFile) | ||
for { | ||
recursiveProcessed <- magicImports(rootDirectories)( | ||
importedGraphQLContent) | ||
} yield recursiveProcessed + IO.Newline | ||
case None => | ||
Left(Failure(s"Could not resolve $filePath in $rootDirectories")) | ||
} | ||
case line => Right(line) | ||
} | ||
|
||
// poor mans cats.Validated | ||
val errors = processed.collect { | ||
case Left(error) => error | ||
} | ||
|
||
if (errors.isEmpty) { | ||
Right( | ||
processed | ||
.collect { | ||
case Right(line) => line | ||
} | ||
.mkString(IO.Newline)) | ||
} else { | ||
Left(errors.reduce[Failure] { | ||
case (reduced, nextFailure) => | ||
Failure(reduced.message + IO.Newline + reduced.message) | ||
}) | ||
} | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name := "test" | ||
enablePlugins(GraphQLCodegenPlugin) | ||
scalaVersion := "2.12.4" | ||
|
||
libraryDependencies ++= Seq( | ||
"org.sangria-graphql" %% "sangria" % "1.3.0" | ||
) | ||
|
||
graphqlCodegenStyle := Apollo | ||
|
||
TaskKey[Unit]("check") := { | ||
val generatedFiles = (graphqlCodegen in Compile).value | ||
val interfacesFile = generatedFiles.find(_.getName == "Interfaces.scala") | ||
|
||
assert(interfacesFile.isDefined, s"Could not find generated scala class. Available files\n ${generatedFiles.mkString("\n ")}") | ||
} |
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 @@ | ||
addSbtPlugin("rocks.muki" % "sbt-graphql" % sys.props("project.version")) |
13 changes: 13 additions & 0 deletions
13
src/sbt-test/codegen/apollo-magic-imports/src/main/graphql/HeroFragmentQuery.graphql
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,13 @@ | ||
query HeroFragmentQuery { | ||
hero { | ||
...CharacterInfo | ||
} | ||
human(id: "Lea") { | ||
homePlanet | ||
...CharacterInfo | ||
} | ||
} | ||
|
||
#import fragments/CharacterFriends.fragment.graphql | ||
#import fragments/CharacterInfo.fragment.graphql | ||
|
12 changes: 12 additions & 0 deletions
12
...t-test/codegen/apollo-magic-imports/src/main/graphql/HeroFragmentWithAbsolutQuery.graphql
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,12 @@ | ||
query HeroFragmentWithAbsolutQuery { | ||
hero { | ||
...CharacterInfoWithAbsolutImport | ||
} | ||
human(id: "Lea") { | ||
homePlanet | ||
...CharacterInfoWithAbsolutImport | ||
} | ||
} | ||
|
||
#import fragments/CharacterInfoWithAbsolutImport.fragment.graphql | ||
|
3 changes: 3 additions & 0 deletions
3
...codegen/apollo-magic-imports/src/main/graphql/fragments/CharacterFriends.fragment.graphql
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,3 @@ | ||
fragment CharacterFriends on Character { | ||
name | ||
} |
6 changes: 6 additions & 0 deletions
6
...st/codegen/apollo-magic-imports/src/main/graphql/fragments/CharacterInfo.fragment.graphql
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,6 @@ | ||
fragment CharacterInfo on Character { | ||
name | ||
friends { | ||
...CharacterFriends | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...-magic-imports/src/main/graphql/fragments/CharacterInfoWithAbsolutImport.fragment.graphql
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,8 @@ | ||
#import fragments/CharacterFriends.fragment.graphql | ||
|
||
fragment CharacterInfoWithAbsolutImport on Character { | ||
name | ||
friends { | ||
...CharacterFriends | ||
} | ||
} |
Oops, something went wrong.