Browse code
init
Ed L authored on 29/10/2017 21:02:20
Showing 6 changed files
Showing 6 changed files
- .gitignore
- build.sbt
- project/Dependencies.scala
- project/build.properties
- src/main/scala/com/fiddlerwoaroof/experiments/graphql_addressbook/Hello.scala
- src/test/scala/com/fiddlerwoaroof/experiments/graphql_addressbook/HelloSpec.scala
0 | 3 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,16 @@ |
1 |
+organization := "com.fiddlerwoaroof.experiments" |
|
2 |
+scalaVersion := "2.12.3" |
|
3 |
+version := "0.1.0-SNAPSHOT" |
|
4 |
+ |
|
5 |
+name := "graphql-addressbook" |
|
6 |
+ |
|
7 |
+libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.3" |
|
8 |
+ |
|
9 |
+libraryDependencies += "org.sangria-graphql" %% "sangria" % "1.3.1" |
|
10 |
+libraryDependencies += "org.sangria-graphql" %% "sangria-circe" % "1.1.0" |
|
11 |
+ |
|
12 |
+libraryDependencies ++= Seq( |
|
13 |
+ "io.circe" %% "circe-core", |
|
14 |
+ "io.circe" %% "circe-generic", |
|
15 |
+ "io.circe" %% "circe-parser" |
|
16 |
+).map(_ % "0.8.0") |
0 | 2 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,90 @@ |
1 |
+package com.fiddlerwoaroof.experiments.graphql_addressbook |
|
2 |
+ |
|
3 |
+import sangria.macros.derive._ |
|
4 |
+import sangria.macros._ |
|
5 |
+import sangria.schema._ |
|
6 |
+ |
|
7 |
+import sangria.execution._ |
|
8 |
+import sangria.marshalling.circe._ |
|
9 |
+ |
|
10 |
+import io.circe.Json |
|
11 |
+ |
|
12 |
+trait Identifiable { |
|
13 |
+ def id: String |
|
14 |
+} |
|
15 |
+ |
|
16 |
+case class Picture(width: Int, height: Int, url: Option[String]) |
|
17 |
+ |
|
18 |
+case class Product(id: String, name: String, description: String) extends Identifiable { |
|
19 |
+ def picture(size: Int): Picture = |
|
20 |
+ Picture( |
|
21 |
+ width = size, |
|
22 |
+ height = size, |
|
23 |
+ url = Some(s"//cdn.com/$size/$id.jpg")) |
|
24 |
+} |
|
25 |
+ |
|
26 |
+object Hello extends Greeting with App { |
|
27 |
+ implicit val PictureType = |
|
28 |
+ deriveObjectType[Unit, Picture]( |
|
29 |
+ ObjectTypeDescription("The product picture"), |
|
30 |
+ DocumentField("url", "Picture CDN URL")) |
|
31 |
+ |
|
32 |
+ val IdentifiableType = |
|
33 |
+ InterfaceType( |
|
34 |
+ "Identifiable", |
|
35 |
+ "Entity that can be identified", |
|
36 |
+ fields[Unit, Identifiable]( |
|
37 |
+ Field("id", StringType, resolve = _.value.id))) |
|
38 |
+ |
|
39 |
+ val ProductType = |
|
40 |
+ deriveObjectType[Unit, Product]( |
|
41 |
+ Interfaces(IdentifiableType), |
|
42 |
+ IncludeMethods("picture")) |
|
43 |
+ |
|
44 |
+ class ProductRepo { |
|
45 |
+ private val Products = List( |
|
46 |
+ Product("1", "Cheescake", "Tasty"), |
|
47 |
+ Product("2", "HEalth Potion", "+50 HP"), |
|
48 |
+ ) |
|
49 |
+ |
|
50 |
+ def product(id: String): Option[Product] = |
|
51 |
+ Products find (_.id == id) |
|
52 |
+ |
|
53 |
+ def products: List[Product] = Products |
|
54 |
+ } |
|
55 |
+ |
|
56 |
+ val Id = Argument("id", StringType) |
|
57 |
+ |
|
58 |
+ val QueryType = |
|
59 |
+ ObjectType("Query", fields[ProductRepo, Unit]( |
|
60 |
+ Field("product", OptionType(ProductType), |
|
61 |
+ description = Some("Return product with specific `id`."), |
|
62 |
+ arguments = Id :: Nil, |
|
63 |
+ resolve = c => c.ctx.product(c arg Id)), |
|
64 |
+ |
|
65 |
+ Field("products", ListType(ProductType), |
|
66 |
+ description = Some("Returns all products"), |
|
67 |
+ resolve = _.ctx.products) |
|
68 |
+ )) |
|
69 |
+ |
|
70 |
+ val query = graphql""" |
|
71 |
+ query MyProduct { |
|
72 |
+ product(id: "2") { |
|
73 |
+ name |
|
74 |
+ description |
|
75 |
+ |
|
76 |
+ picture(size: 500) { |
|
77 |
+ width, height, url |
|
78 |
+ } |
|
79 |
+ } |
|
80 |
+ |
|
81 |
+ products { |
|
82 |
+ name |
|
83 |
+ } |
|
84 |
+ }""" |
|
85 |
+} |
|
86 |
+ |
|
87 |
+trait Greeting { |
|
88 |
+ lazy val greeting: String = "hello" |
|
89 |
+} |
|
90 |
+ |