Skip to content

Commit 25082a4

Browse files
author
federico silva
committedDec 27, 2018
replace graphiql with playground, add docker-compose with postgresql and init scripts
1 parent 6b60fce commit 25082a4

File tree

12 files changed

+623
-171
lines changed

12 files changed

+623
-171
lines changed
 

‎build.sbt

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,6 @@ lazy val core = project
105105
"org.http4s" %% "http4s-circe" % http4sVersion,
106106
"io.circe" %% "circe-optics" % circeVersion,
107107
"org.slf4j" % "slf4j-simple" % slf4jVersion
108-
)
108+
),
109+
109110
)

‎docker-compose.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Use postgres/example user/password credentials
2+
version: '3.1'
3+
4+
services:
5+
6+
db:
7+
image: postgres
8+
restart: always
9+
environment:
10+
POSTGRES_USER: user
11+
POSTGRES_PASSWORD: password
12+
POSTGRES_DB: world
13+
ports:
14+
- 5432:5432
15+
volumes:
16+
- ./init/:/docker-entrypoint-initdb.d/
17+
18+
adminer:
19+
image: adminer
20+
restart: always
21+
ports:
22+
- 8686:8080

‎init/01-tables.sql

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
create table cities
2+
(
3+
id serial primary key not null,
4+
name varchar(100) not null,
5+
country_id varchar(2) not null,
6+
district varchar(20) not null,
7+
population bigint
8+
);

‎init/02-cities-data.sql

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
INSERT INTO cities (name, country_id, district, population)
3+
values
4+
('Montevideo', 'UY', 'Montevideo', 150000),
5+
('Paysandú', 'UY', 'Paysandú', 75000),
6+
('New York', 'US', 'New York', 8623000000),
7+
('San Francisco', 'US', 'California', 884363 )

‎modules/core/src/main/resources/assets/graphiql.html

-155
This file was deleted.

‎modules/core/src/main/resources/assets/playground.html

+557
Large diffs are not rendered by default.

‎modules/core/src/main/scala/GraphQL.scala

+8-5
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,15 @@ object GraphQL {
106106
}
107107

108108
// Parse `query` and execute.
109-
def query(query: String, operationName: Option[String], variables: JsonObject): F[Either[Json, Json]] =
110-
QueryParser.parse(query) match {
111-
case Success(ast) => exec(schema, userContext, ast, operationName, variables)(blockingExecutionContext)
112-
case Failure(e @ SyntaxError(_, _, pe)) => fail(formatSyntaxError(e))
113-
case Failure(e) => fail(formatThrowable(e))
109+
def query(query: String, operationName: Option[String], variables: JsonObject): F[Either[Json, Json]] = {
110+
val q = QueryParser.parse(query)
111+
println(q)
112+
q match {
113+
case Success(ast) => exec(schema, userContext, ast, operationName, variables)(blockingExecutionContext)
114+
case Failure(e@SyntaxError(_, _, pe)) => fail(formatSyntaxError(e))
115+
case Failure(e) => fail(formatThrowable(e))
114116
}
117+
}
115118

116119
// Lift a `Json` into the error side of our effect.
117120
def fail(j: Json): F[Either[Json, Json]] =

‎modules/core/src/main/scala/Main.scala

+13-6
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@ import schema._
1212
import org.http4s._
1313
import org.http4s.circe._
1414
import org.http4s.dsl._
15+
import org.http4s.headers.Location
1516
import org.http4s.implicits._
1617
import org.http4s.server.Server
1718
import org.http4s.server.blaze._
19+
1820
import scala.concurrent.ExecutionContext
1921

2022
object Main extends IOApp {
2123

22-
// Construct a transctor for connecting to the database.
24+
// Construct a transactor for connecting to the database.
2325
def transactor[F[_]: Async: ContextShift] =
2426
Transactor.fromDriverManager[F](
2527
"org.postgresql.Driver",
2628
"jdbc:postgresql:world",
27-
"postgres",
28-
""
29+
"user",
30+
"password"
2931
)
3032

3133
// Construct a GraphQL implementation based on our Sangria definitions.
@@ -44,20 +46,25 @@ object Main extends IOApp {
4446
graphQL: GraphQL[F],
4547
blockingContext: ExecutionContext
4648
): HttpRoutes[F] = {
49+
4750
object dsl extends Http4sDsl[F]; import dsl._
51+
4852
HttpRoutes.of[F] {
4953

50-
case GET -> Root =>
54+
case GET -> Root / "playground.html" =>
5155
StaticFile
52-
.fromResource[F]("/assets/graphiql.html", blockingContext)
56+
.fromResource[F]("/assets/playground.html", blockingContext)
5357
.getOrElseF(NotFound())
54-
58+
5559
case req @ POST -> Root / "graphql"
5660
req.as[Json].flatMap(graphQL.query).flatMap {
5761
case Right(json) => Ok(json)
5862
case Left(json) => BadRequest(json)
5963
}
6064

65+
case _ =>
66+
PermanentRedirect(Location(Uri.uri("/playground.html")))
67+
6168
}
6269
}
6370

‎modules/core/src/main/scala/model/City.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ final case class City(
99
name: String,
1010
countryId: String,
1111
district: String,
12-
population: Int,
12+
population: Long,
1313
)

‎modules/core/src/main/scala/repo/CityRepo.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ object CityRepo {
2222

2323
val select: Fragment =
2424
fr"""
25-
select id, name, countrycode, district, population
26-
from city
25+
select id, name, country_id, district, population
26+
from cities
2727
"""
2828

2929
def fetchById(id: Int): F[Option[City]] =

‎modules/core/src/main/scala/schema/CitySchema.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object CitySchema {
1919
Field("id", IntType, resolve = _.value.id),
2020
Field("name", StringType, resolve = _.value.name),
2121
Field("district", StringType, resolve = _.value.district),
22-
Field("population", IntType, resolve = _.value.population)
22+
Field("population", LongType, resolve = _.value.population)
2323
)
2424
)
2525

‎project/plugins.sbt

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.3.4")
22
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.0.0")
3+
addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")
4+

0 commit comments

Comments
 (0)
Please sign in to comment.