forked from trungfinity/scalajs-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a React integration test case for plugin
- Loading branch information
1 parent
ae42bab
commit 77a5e3d
Showing
7 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
4 changes: 1 addition & 3 deletions
4
modules/codegen/sbt-codegen/src/sbt-test/sbt-graphql-codegen/01-code-generation/test
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,4 +1,2 @@ | ||
> graphql-codegen | ||
$ pause | ||
> run | ||
$ exists target/scala-2.12/src_managed/main/sbt-graphql-codegen/anduin/graphql/example/GetSomeoneNameAndAgeQuery.scala | ||
$ exists target/scala-2.12/src_managed/main/sbt-graphql-codegen/anduin/graphql/example/GetSomeoneQuery.scala |
26 changes: 26 additions & 0 deletions
26
modules/codegen/sbt-codegen/src/sbt-test/sbt-graphql-codegen/02-react-integration/build.sbt
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,26 @@ | ||
// Copyright (C) 2017 Anduin Transactions, Inc. | ||
|
||
lazy val libraryVersion = System.getProperty("plugin.version") | ||
|
||
lazy val root = project | ||
.in(file(".")) | ||
.settings( | ||
scalaVersion := "2.12.4", | ||
libraryDependencies ++= { | ||
if (libraryVersion != null) { // scalastyle:ignore null | ||
Seq( | ||
// scalastyle:off multiple.string.literals | ||
"com.anduintransact" %%% "scalajs-graphql-tools" % libraryVersion, | ||
"com.anduintransact" %%% "scalajs-apollo-link-mock" % libraryVersion, | ||
"com.anduintransact" %%% "scalajs-apollo-cache-inmemory" % libraryVersion | ||
// scalastyle:on multiple.string.literals | ||
) | ||
} else { | ||
throw new RuntimeException("Library version is not specified.") | ||
} | ||
}, | ||
graphqlCodegenPackage in Compile := Some("anduin.graphql.example"), | ||
scalaJSUseMainModuleInitializer in Compile := true | ||
) | ||
.enablePlugins(GraphqlCodegenPlugin) | ||
.enablePlugins(ScalaJSBundlerPlugin) |
16 changes: 16 additions & 0 deletions
16
...gen/sbt-codegen/src/sbt-test/sbt-graphql-codegen/02-react-integration/project/plugins.sbt
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 @@ | ||
// Copyright (C) 2017 Anduin Transactions, Inc. | ||
|
||
lazy val pluginVersion = System.getProperty("plugin.version") | ||
|
||
{ | ||
if (pluginVersion != null) { // scalastyle:ignore null | ||
addSbtPlugin("com.anduintransact" % "sbt-graphql-codegen" % pluginVersion) | ||
} else { | ||
throw new RuntimeException("Plugin version is not specified.") | ||
} | ||
} | ||
|
||
addSbtPlugin( | ||
"ch.epfl.scala" % "sbt-scalajs-bundler" % "0.9.0" | ||
exclude ("org.scala-js", "sbt-scalajs") | ||
) |
9 changes: 9 additions & 0 deletions
9
.../sbt-graphql-codegen/02-react-integration/src/main/resources/person-by-name-query.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,9 @@ | ||
query GetPersonByName($name: NameInput!) { | ||
personByName(name: $name) { | ||
name { | ||
firstName { value } | ||
lastName | ||
} | ||
age | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...n/src/sbt-test/sbt-graphql-codegen/02-react-integration/src/main/resources/schema.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,37 @@ | ||
type FirstName { | ||
value: String! | ||
} | ||
|
||
type Name { | ||
firstName: FirstName! | ||
lastName: String | ||
} | ||
|
||
input FirstNameInput { | ||
value: String! | ||
} | ||
|
||
input NameInput { | ||
firstName: FirstNameInput! | ||
lastName: String | ||
} | ||
|
||
enum Gender { | ||
MALE | ||
FEMALE | ||
UNKNOWN | ||
} | ||
|
||
type Person { | ||
name: Name | ||
gender: Gender | ||
age: Int | ||
} | ||
|
||
type Query { | ||
personByName(name: NameInput!): Person | ||
} | ||
|
||
schema { | ||
query: Query | ||
} |
131 changes: 131 additions & 0 deletions
131
...sbt-graphql-codegen/02-react-integration/src/main/scala/anduin/graphql/example/Main.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,131 @@ | ||
// Copyright (C) 2017 Anduin Transactions, Inc. | ||
|
||
package anduin.graphql.example | ||
|
||
import scala.scalajs.js | ||
|
||
import japgolly.scalajs.react.raw.{React, ReactClassUntyped, ReactDOMServer} | ||
|
||
import anduin.scalajs.apollo.cache.internal.{ApolloInMemoryCache, ApolloInMemoryCacheOptions} | ||
import anduin.scalajs.apollo.client.internal.{ApolloClient, ApolloClientOptions} | ||
import anduin.scalajs.apollo.graphqltools.internal.{ | ||
ExecutableSchemaOptions, | ||
GraphqlTools, | ||
SchemaMockFunctionsOptions | ||
} | ||
import anduin.scalajs.apollo.link.internal.{ApolloMockLink, ApolloMockLinkOptions} | ||
import anduin.scalajs.react.apollo.{ApolloQueryProps, ReactApollo} | ||
import anduin.scalajs.react.apollo.internal.{ | ||
ApolloProvider => InternalApolloProvider, | ||
ApolloProviderProps => InternalApolloProviderProps, | ||
ReactApollo => InternalReactApollo | ||
} | ||
|
||
// scalastyle:off underscore.import | ||
import japgolly.scalajs.react._ | ||
import japgolly.scalajs.react.vdom.html_<^._ | ||
// scalastyle:on underscore.import | ||
|
||
object Main { | ||
|
||
def testPersonByNameQuery(): Unit = { | ||
val component = ScalaComponent | ||
.builder[ApolloQueryProps[GetPersonByNameQuery.Data]]("PersonByName") | ||
.render_P { props => | ||
props.data.personByName.fold( | ||
<.div("Loading") | ||
) { person => | ||
<.div( | ||
person.name.whenDefined { name => | ||
<.div( | ||
s"First name: ${name.firstName.value}", | ||
<.br(), | ||
name.lastName.whenDefined { lastName => | ||
s"Last name: $lastName", | ||
} | ||
) | ||
}, | ||
person.age.whenDefined { age => | ||
s"Age: $age" | ||
} | ||
) | ||
} | ||
} | ||
.build | ||
|
||
val graphqlComponent = ReactApollo.graphql(GetPersonByNameQuery).apply(component) | ||
|
||
val schema = GraphqlTools.makeExecutableSchema( | ||
new ExecutableSchemaOptions( | ||
// Fix it later | ||
"""type FirstName { | ||
| value: String! | ||
|} | ||
| | ||
|type Name { | ||
| firstName: FirstName! | ||
| lastName: String | ||
|} | ||
| | ||
|input FirstNameInput { | ||
| value: String! | ||
|} | ||
| | ||
|input NameInput { | ||
| firstName: FirstNameInput! | ||
| lastName: String | ||
|} | ||
| | ||
|enum Gender { | ||
| MALE | ||
| FEMALE | ||
| UNKNOWN | ||
|} | ||
| | ||
|type Person { | ||
| name: Name | ||
| gender: Gender | ||
| age: Int | ||
|} | ||
| | ||
|type Query { | ||
| personByName(name: NameInput!): Person | ||
| peopleByGender(gender: Gender!): [Person] | ||
|} | ||
| | ||
|schema { | ||
| query: Query | ||
|} | ||
""".stripMargin | ||
) | ||
) | ||
|
||
GraphqlTools.addMockFunctionsToSchema(new SchemaMockFunctionsOptions(schema)) | ||
|
||
val client = new ApolloClient( | ||
new ApolloClientOptions( | ||
link = new ApolloMockLink(new ApolloMockLinkOptions(schema)), | ||
cache = new ApolloInMemoryCache(new ApolloInMemoryCacheOptions(addTypename = true)), | ||
ssrMode = true | ||
) | ||
) | ||
|
||
val firstNameInput = FirstNameInput("Trung") | ||
val nameInput = NameInput(firstNameInput, Some("Nguyen")) | ||
|
||
val root = React.createElement( | ||
InternalApolloProvider.asInstanceOf[ReactClassUntyped], // scalastyle:ignore token | ||
new InternalApolloProviderProps(client), | ||
graphqlComponent(GetPersonByNameQuery.Variables(nameInput)).raw // scalastyle:ignore magic.number | ||
) | ||
|
||
InternalReactApollo.getDataFromTree(root).`then`[Unit] { _ => | ||
val markup = ReactDOMServer.renderToStaticMarkup(root) | ||
assert(markup.contains("<div>First name: Hello World<br/>Last name: Hello World</div>")) | ||
} | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
testPersonByNameQuery() | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
modules/codegen/sbt-codegen/src/sbt-test/sbt-graphql-codegen/02-react-integration/test
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,2 @@ | ||
> run | ||
$ exists target/scala-2.12/src_managed/main/sbt-graphql-codegen/anduin/graphql/example/GetPersonByNameQuery.scala |