-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSubscriptionSchema.swift
203 lines (186 loc) · 5.68 KB
/
SubscriptionSchema.swift
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import GraphQL
import GraphQLRxSwift
import NIO
import RxSwift
// MARK: Types
struct Email: Encodable {
let from: String
let subject: String
let message: String
let unread: Bool
let priority: Int
init(from: String, subject: String, message: String, unread: Bool, priority: Int = 0) {
self.from = from
self.subject = subject
self.message = message
self.unread = unread
self.priority = priority
}
}
struct Inbox: Encodable {
let emails: [Email]
}
struct EmailEvent: Encodable {
let email: Email
let inbox: Inbox
}
// MARK: Schema
let EmailType = try! GraphQLObjectType(
name: "Email",
fields: [
"from": GraphQLField(
type: GraphQLString
),
"subject": GraphQLField(
type: GraphQLString
),
"message": GraphQLField(
type: GraphQLString
),
"unread": GraphQLField(
type: GraphQLBoolean
),
]
)
let InboxType = try! GraphQLObjectType(
name: "Inbox",
fields: [
"emails": GraphQLField(
type: GraphQLList(EmailType)
),
"total": GraphQLField(
type: GraphQLInt,
resolve: { inbox, _, _, _ in
(inbox as! Inbox).emails.count
}
),
"unread": GraphQLField(
type: GraphQLInt,
resolve: { inbox, _, _, _ in
(inbox as! Inbox).emails.filter { $0.unread }.count
}
),
]
)
let EmailEventType = try! GraphQLObjectType(
name: "EmailEvent",
fields: [
"email": GraphQLField(
type: EmailType
),
"inbox": GraphQLField(
type: InboxType
),
]
)
let EmailQueryType = try! GraphQLObjectType(
name: "Query",
fields: [
"inbox": GraphQLField(
type: InboxType
),
]
)
// MARK: Test Helpers
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
class EmailDb {
var emails: [Email]
let publisher: PublishSubject<Any>
let disposeBag: DisposeBag
init() {
emails = [
Email(
from: "joe@graphql.org",
subject: "Hello",
message: "Hello World",
unread: false
),
]
publisher = PublishSubject<Any>()
disposeBag = DisposeBag()
}
/// Adds a new email to the database and triggers all observers
func trigger(email: Email) {
emails.append(email)
publisher.onNext(email)
}
/// Returns the default email schema, with standard resolvers.
func defaultSchema() -> GraphQLSchema {
return emailSchemaWithResolvers(
resolve: { emailAny, _, _, eventLoopGroup, _ throws -> EventLoopFuture<Any?> in
if let email = emailAny as? Email {
return eventLoopGroup.next().makeSucceededFuture(EmailEvent(
email: email,
inbox: Inbox(emails: self.emails)
))
} else {
throw GraphQLError(message: "\(type(of: emailAny)) is not Email")
}
},
subscribe: { _, args, _, eventLoopGroup, _ throws -> EventLoopFuture<Any?> in
let priority = args["priority"].int ?? 0
let filtered = self.publisher.filter { emailAny throws in
if let email = emailAny as? Email {
return email.priority >= priority
} else {
return true
}
}
return eventLoopGroup.next().makeSucceededFuture(filtered.toEventStream())
}
)
}
/// Generates a subscription to the database using the default schema and resolvers
func subscription(
query: String,
variableValues: [String: Map] = [:]
) throws -> SubscriptionEventStream {
return try createSubscription(schema: defaultSchema(), query: query, variableValues: variableValues)
}
}
/// Generates an email schema with the specified resolve and subscribe methods
func emailSchemaWithResolvers(resolve: GraphQLFieldResolve? = nil, subscribe: GraphQLFieldResolve? = nil) -> GraphQLSchema {
return try! GraphQLSchema(
query: EmailQueryType,
subscription: try! GraphQLObjectType(
name: "Subscription",
fields: [
"importantEmail": GraphQLField(
type: EmailEventType,
args: [
"priority": GraphQLArgument(
type: GraphQLInt
),
],
resolve: resolve,
subscribe: subscribe
),
]
)
)
}
/// Generates a subscription from the given schema and query. It's expected that the resolver/database interactions are configured by the caller.
func createSubscription(
schema: GraphQLSchema,
query: String,
variableValues: [String: Map] = [:]
) throws -> SubscriptionEventStream {
let result = try graphqlSubscribe(
queryStrategy: SerialFieldExecutionStrategy(),
mutationStrategy: SerialFieldExecutionStrategy(),
subscriptionStrategy: SerialFieldExecutionStrategy(),
instrumentation: NoOpInstrumentation,
schema: schema,
request: query,
rootValue: (),
context: (),
eventLoopGroup: eventLoopGroup,
variableValues: variableValues,
operationName: nil
).wait()
if let stream = result.stream {
return stream
} else {
throw result.errors.first! // We may have more than one...
}
}