1 package domain.graphql
2
3 import sangria.execution.Executor
4 import sangria.marshalling.json4s.native._
5 import sangria.parser.QueryParser
6 import sangria.schema._
7
8 import scala.util.{Failure, Success}
9
10 object ChrisDebug {
11
12 trait IF {
13 def id: String
14 }
15
16 case class A(id: String, num: Int) extends IF
17
18 case class B(id: String, bool: Boolean) extends IF
19
20 type IFRepo = Seq[IF]
21
22 val myIFRepo: IFRepo = Seq(
23 A("foo", 12),
24 A("bar", 13)
25 )
26
27 val IFType: InterfaceType[Unit, IF] = InterfaceType(
28 name = "IFType",
29 description = "Base trait",
30 fieldsFn = () => fields[Unit, IF](
31 Field("id", StringType, Some("id"), resolve = _.value.id)))
32
33 val AType = ObjectType(
34 "AType",
35 "A",
36 interfaces[Unit, A](IFType),
37 fields[Unit, A](
38 Field("id", StringType,
39 Some("id"),
40 resolve = _.value.id),
41 Field("num", IntType,
42 Some("num"),
43 resolve = _.value.num)
44 ))
45
46 val IFQueryType = ObjectType(
47 "IFQueryType",
48 "IFQueryType",
49 fields[IFRepo, Unit](
50 Field("getIFs", ListType(IFType),
51 resolve = (ctx) => ctx.ctx)
52 )
53 )
54 val IFSchema = Schema(IFQueryType)
55
56 val AQueryType = ObjectType(
57 "AQueryType",
58 "AQueryType",
59 fields[IFRepo, Unit](
60 Field("getIFs", ListType(AType),
61 resolve = (ctx) => ctx.ctx.asInstanceOf[Seq[A]])
62 )
63 )
64 val ASchema = Schema(AQueryType)
65
66 def doQuery() = {
67 val query = "{ getIFs { id } }"
68 QueryParser.parse(query) match {
69 case Success(queryAst) =>
70 import scala.concurrent.ExecutionContext.Implicits.global // scalastyle:off
71 val res =
72 if (true) Executor.execute(IFSchema, queryAst, myIFRepo)
73 else Executor.execute(ASchema, queryAst, myIFRepo)
74 res
75 case Failure(err) => throw err
76 }
77 }
78
79 }