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