@@ -4,14 +4,15 @@ import NIO
44import RxSwift
55
66// MARK: Types
7- struct Email : Encodable {
8- let from : String
9- let subject : String
10- let message : String
11- let unread : Bool
12- let priority : Int
13-
14- init ( from: String , subject: String , message: String , unread: Bool , priority: Int = 0 ) {
7+
8+ struct Email : Encodable {
9+ let from : String
10+ let subject : String
11+ let message : String
12+ let unread : Bool
13+ let priority : Int
14+
15+ init ( from: String , subject: String , message: String , unread: Bool , priority: Int = 0 ) {
1516 self . from = from
1617 self . subject = subject
1718 self . message = message
@@ -20,16 +21,17 @@ struct Email : Encodable {
2021 }
2122}
2223
23- struct Inbox : Encodable {
24- let emails : [ Email ]
24+ struct Inbox : Encodable {
25+ let emails : [ Email ]
2526}
2627
27- struct EmailEvent : Encodable {
28- let email : Email
29- let inbox : Inbox
28+ struct EmailEvent : Encodable {
29+ let email : Email
30+ let inbox : Inbox
3031}
3132
3233// MARK: Schema
34+
3335let EmailType = try ! GraphQLObjectType (
3436 name: " Email " ,
3537 fields: [
@@ -62,7 +64,7 @@ let InboxType = try! GraphQLObjectType(
6264 " unread " : GraphQLField (
6365 type: GraphQLInt,
6466 resolve: { inbox, _, _, _ in
65- ( inbox as! Inbox ) . emails. filter ( { $0. unread} ) . count
67+ ( inbox as! Inbox ) . emails. filter { $0. unread } . count
6668 }
6769 ) ,
6870 ]
@@ -75,15 +77,15 @@ let EmailEventType = try! GraphQLObjectType(
7577 ) ,
7678 " inbox " : GraphQLField (
7779 type: InboxType
78- )
80+ ) ,
7981 ]
8082)
8183let EmailQueryType = try ! GraphQLObjectType (
8284 name: " Query " ,
8385 fields: [
8486 " inbox " : GraphQLField (
8587 type: InboxType
86- )
88+ ) ,
8789 ]
8890)
8991
@@ -95,40 +97,40 @@ class EmailDb {
9597 var emails : [ Email ]
9698 let publisher : PublishSubject < Any >
9799 let disposeBag : DisposeBag
98-
100+
99101 init ( ) {
100102 emails = [
101103 Email (
102104103105 subject: " Hello " ,
104106 message: " Hello World " ,
105107 unread: false
106- )
108+ ) ,
107109 ]
108110 publisher = PublishSubject < Any > ( )
109111 disposeBag = DisposeBag ( )
110112 }
111-
113+
112114 /// Adds a new email to the database and triggers all observers
113- func trigger( email: Email ) {
115+ func trigger( email: Email ) {
114116 emails. append ( email)
115117 publisher. onNext ( email)
116118 }
117-
119+
118120 /// Returns the default email schema, with standard resolvers.
119121 func defaultSchema( ) -> GraphQLSchema {
120122 return emailSchemaWithResolvers (
121- resolve: { emailAny, _, _, eventLoopGroup, _ throws -> EventLoopFuture < Any ? > in
123+ resolve: { emailAny, _, _, eventLoopGroup, _ throws -> EventLoopFuture < Any ? > in
122124 if let email = emailAny as? Email {
123125 return eventLoopGroup. next ( ) . makeSucceededFuture ( EmailEvent (
124126 email: email,
125127 inbox: Inbox ( emails: self . emails)
126128 ) )
127129 } else {
128- throw GraphQLError ( message: " \( type ( of: emailAny) ) is not Email " )
130+ throw GraphQLError ( message: " \( type ( of: emailAny) ) is not Email " )
129131 }
130132 } ,
131- subscribe: { _, args, _, eventLoopGroup, _ throws -> EventLoopFuture < Any ? > in
133+ subscribe: { _, args, _, eventLoopGroup, _ throws -> EventLoopFuture < Any ? > in
132134 let priority = args [ " priority " ] . int ?? 0
133135 let filtered = self . publisher. filter { emailAny throws in
134136 if let email = emailAny as? Email {
@@ -141,10 +143,10 @@ class EmailDb {
141143 }
142144 )
143145 }
144-
146+
145147 /// Generates a subscription to the database using the default schema and resolvers
146- func subscription (
147- query: String ,
148+ func subscription(
149+ query: String ,
148150 variableValues: [ String : Map ] = [ : ]
149151 ) throws -> SubscriptionEventStream {
150152 return try createSubscription ( schema: defaultSchema ( ) , query: query, variableValues: variableValues)
@@ -163,11 +165,11 @@ func emailSchemaWithResolvers(resolve: GraphQLFieldResolve? = nil, subscribe: Gr
163165 args: [
164166 " priority " : GraphQLArgument (
165167 type: GraphQLInt
166- )
168+ ) ,
167169 ] ,
168170 resolve: resolve,
169171 subscribe: subscribe
170- )
172+ ) ,
171173 ]
172174 )
173175 )
@@ -186,13 +188,13 @@ func createSubscription(
186188 instrumentation: NoOpInstrumentation,
187189 schema: schema,
188190 request: query,
189- rootValue: Void ( ) ,
190- context: Void ( ) ,
191+ rootValue: ( ) ,
192+ context : ( ) ,
191193 eventLoopGroup : eventLoopGroup ,
192194 variableValues : variableValues ,
193195 operationName : nil
194196 ) . wait( )
195-
197+
196198 if let stream = result. stream {
197199 return stream
198200 } else {
0 commit comments