Skip to content

Commit

Permalink
Add a React integration test case for plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
trungfinity committed Dec 2, 2017
1 parent ae42bab commit 77a5e3d
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 3 deletions.
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
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)
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")
)
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
}
}
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
}
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()
}
}
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

0 comments on commit 77a5e3d

Please sign in to comment.