git.fiddlerwoaroof.com
Browse code

update demo to actually sort of work

Ed Langley authored on 30/10/2017 17:27:08
Showing 1 changed files
... ...
@@ -32,12 +32,12 @@ class AddressBook {
32 32
   private val Contacts = List(
33 33
     Contact("1",
34 34
       EnglishName("John", Some("Apple"), "Seed"),
35
-      Map("home" -> Address("1103 Foo St.", "Ventura", "CA", "93003", "USA")),
36
-      Map("home" -> pn"333-444-3333")),
35
+      Address("1103 Foo St.", "Ventura", "CA", "93003", "USA"),
36
+      pn"333-444-3333"),
37 37
     Contact("1",
38 38
       EnglishName("Bob", None, "Marley"),
39
-      Map("home" -> Address("1103 Maricopa Ave.", "Ventura", "CA", "93003", "USA")),
40
-      Map("home" -> pn"435-2039")),
39
+      Address("1103 Maricopa Ave.", "Ventura", "CA", "93003", "USA"),
40
+      pn"435-2039"),
41 41
   )
42 42
 
43 43
   def contact(id: String): Option[Contact] =
... ...
@@ -46,9 +46,9 @@ class AddressBook {
46 46
   def addressesByPartialName(name: String, addressType: String = "home"): Seq[Address] =
47 47
     Contacts
48 48
       .filter(_.name.name.toLowerCase contains name)
49
-      .flatMap(_.addresses get addressType)
49
+      .map(_.address)
50 50
 
51
-  def addresses: List[Contact] = Contacts
51
+  def contacts: List[Contact] = Contacts
52 52
 }
53 53
 
54 54
 object AddressBook {
... ...
@@ -69,7 +69,11 @@ object AddressBook {
69 69
 
70 70
 case class Picture(width: Int, height: Int, url: Option[String])
71 71
 
72
-case class PhoneNumber(countryCode: Int, areaCode: Int, prefix: Int, suffix: Int)
72
+case class PhoneNumber(countryCode: Int, areaCode: Int, prefix: Int, suffix: Int) {
73
+  def formatted = f"(${areaCode}%03d) ${prefix}%03d-${suffix}%04d"
74
+  def localFormatted = f"${prefix}%03d-${suffix}%04d"
75
+  def intlFormatted = f"+$countryCode (${areaCode}%03d) ${prefix}%03d-${suffix}%04d"
76
+}
73 77
 
74 78
 case class Address(address: String, city: String, state: String, zip: String, country: String)
75 79
 
... ...
@@ -85,7 +89,7 @@ case class EnglishName(first: String, middle: Option[String], last: String) exte
85 89
   def sortName: String = s"$last, $first"
86 90
 }
87 91
 
88
-case class Contact(id: String, name: Name, addresses: Map[String, Address], phoneNumbers: Map[String, PhoneNumber]) extends Identifiable {
92
+case class Contact(id: String, name: Name, address: Address, phoneNumber: PhoneNumber) extends Identifiable {
89 93
   def picture(size: Int): Picture =
90 94
     Picture(
91 95
       width = size,
... ...
@@ -94,22 +98,42 @@ case class Contact(id: String, name: Name, addresses: Map[String, Address], phon
94 98
 }
95 99
 
96 100
 object Hello extends App {
97
-  implicit val PictureType =
101
+  implicit val PictureType: ObjectType[Unit, Picture] =
98 102
     deriveObjectType[Unit, Picture](
99 103
       ObjectTypeDescription("The product picture"),
100 104
       DocumentField("url", "Picture CDN URL"))
101 105
 
102
-  val IdentifiableType =
106
+  val IdentifiableType: InterfaceType[Unit, Identifiable] =
103 107
     InterfaceType(
104 108
       "Identifiable",
105 109
       "Entity that can be identified",
106 110
       fields[Unit, Identifiable](
107 111
         Field("id", StringType, resolve = _.value.id)))
108 112
 
109
-  val AddressType =
113
+  implicit val AddressType: ObjectType[Unit, Address] =
110 114
     deriveObjectType[Unit, Address](
115
+      ObjectTypeDescription("A Contact's address"))
116
+
117
+  implicit val PhoneNumberType: ObjectType[Unit, PhoneNumber] =
118
+    deriveObjectType[Unit, PhoneNumber](
119
+      IncludeMethods("formatted", "localFormatted", "intlFormatted"))
120
+
121
+  val NameType: InterfaceType[Unit, Name] =
122
+    InterfaceType(
123
+      "Name",
124
+      "An interface for things that represent a name",
125
+      fields[Unit, Name](
126
+        Field("name", StringType, resolve = _.value.name),
127
+        Field("sortName", StringType, resolve = _.value.sortName)))
128
+
129
+  implicit val EnglishNameType: ObjectType[Unit, EnglishName] =
130
+    deriveObjectType[Unit, EnglishName](
131
+      Interfaces(NameType))
132
+
133
+  val ContactType =
134
+    deriveObjectType[Unit, Contact](
111 135
       Interfaces(IdentifiableType),
112
-      IncludeMethods("picture", "sortName", "name"))
136
+      IncludeMethods("picture"))
113 137
 
114 138
   val Id = Argument("id", StringType)
115 139
 
... ...
@@ -120,14 +144,9 @@ object Hello extends App {
120 144
         arguments = Id :: Nil,
121 145
         resolve = c => c.ctx.contact(c arg Id)),
122 146
 
123
-      Field("addressByPartialName", ListType(AddressType),
124
-        description = Some("Return product with specific `id`."),
125
-        arguments = Id :: Nil,
126
-        resolve = c => c.ctx.addressByPartialName(c arg Id)),
127
-
128
-      Field("addresses", ListType(AddressType),
147
+      Field("contacts", ListType(ContactType),
129 148
         description = Some("Returns all products"),
130
-        resolve = _.ctx.addresses)
149
+        resolve = _.ctx.contacts)
131 150
     ))
132 151
 
133 152
   val schema = Schema(QueryType)