@@ -4,14 +4,15 @@ import NIO
4
4
import RxSwift
5
5
6
6
// 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 ) {
15
16
self . from = from
16
17
self . subject = subject
17
18
self . message = message
@@ -20,16 +21,17 @@ struct Email : Encodable {
20
21
}
21
22
}
22
23
23
- struct Inbox : Encodable {
24
- let emails : [ Email ]
24
+ struct Inbox : Encodable {
25
+ let emails : [ Email ]
25
26
}
26
27
27
- struct EmailEvent : Encodable {
28
- let email : Email
29
- let inbox : Inbox
28
+ struct EmailEvent : Encodable {
29
+ let email : Email
30
+ let inbox : Inbox
30
31
}
31
32
32
33
// MARK: Schema
34
+
33
35
let EmailType = try ! GraphQLObjectType (
34
36
name: " Email " ,
35
37
fields: [
@@ -62,7 +64,7 @@ let InboxType = try! GraphQLObjectType(
62
64
" unread " : GraphQLField (
63
65
type: GraphQLInt,
64
66
resolve: { inbox, _, _, _ in
65
- ( inbox as! Inbox ) . emails. filter ( { $0. unread} ) . count
67
+ ( inbox as! Inbox ) . emails. filter { $0. unread } . count
66
68
}
67
69
) ,
68
70
]
@@ -75,15 +77,15 @@ let EmailEventType = try! GraphQLObjectType(
75
77
) ,
76
78
" inbox " : GraphQLField (
77
79
type: InboxType
78
- )
80
+ ) ,
79
81
]
80
82
)
81
83
let EmailQueryType = try ! GraphQLObjectType (
82
84
name: " Query " ,
83
85
fields: [
84
86
" inbox " : GraphQLField (
85
87
type: InboxType
86
- )
88
+ ) ,
87
89
]
88
90
)
89
91
@@ -95,40 +97,40 @@ class EmailDb {
95
97
var emails : [ Email ]
96
98
let publisher : PublishSubject < Any >
97
99
let disposeBag : DisposeBag
98
-
100
+
99
101
init ( ) {
100
102
emails = [
101
103
Email (
102
104
103
105
subject: " Hello " ,
104
106
message: " Hello World " ,
105
107
unread: false
106
- )
108
+ ) ,
107
109
]
108
110
publisher = PublishSubject < Any > ( )
109
111
disposeBag = DisposeBag ( )
110
112
}
111
-
113
+
112
114
/// Adds a new email to the database and triggers all observers
113
- func trigger( email: Email ) {
115
+ func trigger( email: Email ) {
114
116
emails. append ( email)
115
117
publisher. onNext ( email)
116
118
}
117
-
119
+
118
120
/// Returns the default email schema, with standard resolvers.
119
121
func defaultSchema( ) -> GraphQLSchema {
120
122
return emailSchemaWithResolvers (
121
- resolve: { emailAny, _, _, eventLoopGroup, _ throws -> EventLoopFuture < Any ? > in
123
+ resolve: { emailAny, _, _, eventLoopGroup, _ throws -> EventLoopFuture < Any ? > in
122
124
if let email = emailAny as? Email {
123
125
return eventLoopGroup. next ( ) . makeSucceededFuture ( EmailEvent (
124
126
email: email,
125
127
inbox: Inbox ( emails: self . emails)
126
128
) )
127
129
} else {
128
- throw GraphQLError ( message: " \( type ( of: emailAny) ) is not Email " )
130
+ throw GraphQLError ( message: " \( type ( of: emailAny) ) is not Email " )
129
131
}
130
132
} ,
131
- subscribe: { _, args, _, eventLoopGroup, _ throws -> EventLoopFuture < Any ? > in
133
+ subscribe: { _, args, _, eventLoopGroup, _ throws -> EventLoopFuture < Any ? > in
132
134
let priority = args [ " priority " ] . int ?? 0
133
135
let filtered = self . publisher. filter { emailAny throws in
134
136
if let email = emailAny as? Email {
@@ -141,10 +143,10 @@ class EmailDb {
141
143
}
142
144
)
143
145
}
144
-
146
+
145
147
/// Generates a subscription to the database using the default schema and resolvers
146
- func subscription (
147
- query: String ,
148
+ func subscription(
149
+ query: String ,
148
150
variableValues: [ String : Map ] = [ : ]
149
151
) throws -> SubscriptionEventStream {
150
152
return try createSubscription ( schema: defaultSchema ( ) , query: query, variableValues: variableValues)
@@ -163,11 +165,11 @@ func emailSchemaWithResolvers(resolve: GraphQLFieldResolve? = nil, subscribe: Gr
163
165
args: [
164
166
" priority " : GraphQLArgument (
165
167
type: GraphQLInt
166
- )
168
+ ) ,
167
169
] ,
168
170
resolve: resolve,
169
171
subscribe: subscribe
170
- )
172
+ ) ,
171
173
]
172
174
)
173
175
)
@@ -186,13 +188,13 @@ func createSubscription(
186
188
instrumentation: NoOpInstrumentation,
187
189
schema: schema,
188
190
request: query,
189
- rootValue: Void ( ) ,
190
- context: Void ( ) ,
191
+ rootValue: ( ) ,
192
+ context : ( ) ,
191
193
eventLoopGroup : eventLoopGroup ,
192
194
variableValues : variableValues ,
193
195
operationName : nil
194
196
) . wait( )
195
-
197
+
196
198
if let stream = result. stream {
197
199
return stream
198
200
} else {
0 commit comments