forked from marioosh/graphql-akka-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage4-stage5.patch
62 lines (59 loc) · 1.71 KB
/
stage4-stage5.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
diff --git a/queries/stage5.graphql b/queries/stage5.graphql
new file mode 100644
index 0000000..82a1f99
--- /dev/null
+++ b/queries/stage5.graphql
@@ -0,0 +1,16 @@
+mutation stage5{
+
+ addCategory(id: "foo", name: "FOO"){
+ id
+ name
+ }
+
+
+}
+
+query AllCats {
+ allCategories {
+ id
+ name
+ }
+}
diff --git a/src/main/scala/SchemaDef.scala b/src/main/scala/SchemaDef.scala
index 1604f31..b3ab278 100644
--- a/src/main/scala/SchemaDef.scala
+++ b/src/main/scala/SchemaDef.scala
@@ -97,7 +97,17 @@ object SchemaDef {
)
)
- val ShopSchema = Schema(QueryType) //define entry point
+ val IdArg = Argument("id", IntType)
+ val NameArg = Argument("name", StringType)
+
+ val Mutation = ObjectType("Mutation", fields[ShopRepository, Unit](
+ Field("addCategory", CategoryType,
+ arguments = IdArg :: NameArg :: Nil,
+ resolve = c => c.ctx.addCategory(c.arg(IdArg), c.arg(NameArg))
+ )
+ ))
+
+ val ShopSchema = Schema(QueryType, Some(Mutation)) //define entry point
def constantComplexity[Ctx](complexity: Double) =
Some((_: Ctx, _: Args, child: Double) ⇒ child + complexity)
diff --git a/src/main/scala/ShopRepository.scala b/src/main/scala/ShopRepository.scala
index 23c552c..154741d 100644
--- a/src/main/scala/ShopRepository.scala
+++ b/src/main/scala/ShopRepository.scala
@@ -41,6 +41,12 @@ class ShopRepository(db: Database) {
case (_, categories) ⇒ categories.map(_._1.productId) → categories.head._2
}
}
+
+ def addCategory(id: CategoryId, name: String): Future[Category] = {
+ val cat: Category = Category(id, name)
+ db.run(Categories.insertOrUpdate(cat)).map(_ => cat)
+ }
+
}
object ShopRepository {