Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: Tests

on:
push:
branches: [ main ]
paths-ignore: [ README.md ]
pull_request:
branches: [ main ]
paths-ignore: [ README.md ]
workflow_dispatch:

jobs:
formatlint:
name: Format linting
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Pull formatting docker image
run: docker pull ghcr.io/nicklockwood/swiftformat:latest
- name: Run format linting
run: docker run --rm -v ${{ github.workspace }}:/repo ghcr.io/nicklockwood/swiftformat:latest /repo --lint

macos:
name: Test on macOS
runs-on: macos-latest
steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
- uses: actions/checkout@v3
- name: Build and test
run: swift test --parallel --enable-test-discovery

linux:
name: Test on Linux
runs-on: ubuntu-latest
steps:
- uses: swift-actions/setup-swift@v2
- uses: actions/checkout@v3
- name: Test
run: swift test --parallel --enable-code-coverage
- name: Get test coverage html
run: |
llvm-cov show \
$(swift build --show-bin-path)/GraphQLRxSwiftPackageTests.xctest \
--instr-profile $(swift build --show-bin-path)/codecov/default.profdata \
--ignore-filename-regex="\.build|Tests" \
--format html \
--output-dir=.test-coverage
- name: Upload test coverage html
uses: actions/upload-artifact@v3
with:
name: test-coverage-report
path: .test-coverage

backcompat-ubuntu-22_04:
name: Test Swift ${{ matrix.swift }} on Ubuntu 22.04
runs-on: ubuntu-22.04
strategy:
matrix:
swift: ["5.7", "5.8", "5.9", "5.10", "6.0"]
steps:
- uses: swift-actions/setup-swift@v2
with:
swift-version: ${{ matrix.swift }}
- uses: actions/checkout@v3
- name: Test
run: swift test --parallel

# Swift versions older than 5.7 don't have builds for 22.04. https://www.swift.org/download/
backcompat-ubuntu-20_04:
name: Test Swift ${{ matrix.swift }} on Ubuntu 20.04
runs-on: ubuntu-20.04
strategy:
matrix:
swift: ["5.4", "5.5", "5.6"]
steps:
- uses: swift-actions/setup-swift@v2
with:
swift-version: ${{ matrix.swift }}
- uses: actions/checkout@v3
- name: Test
run: swift test --parallel
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
### SwiftPM ###
.build/
.swiftpm/

# VS Code
.vscode
38 changes: 28 additions & 10 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/GraphQLSwift/GraphQL.git", from: "2.0.0"),
.package(url: "https://github.com/GraphQLSwift/Graphiti.git", from: "1.0.0"),
.package(url: "https://github.com/ReactiveX/RxSwift.git", from: "6.1.0")
.package(url: "https://github.com/ReactiveX/RxSwift.git", from: "6.1.0"),
],
targets: [
.target(name: "GraphQLRxSwift", dependencies: ["GraphQL", "Graphiti", "RxSwift"]),
.testTarget(name: "GraphQLRxSwiftTests",dependencies: ["GraphQLRxSwift"]),
.testTarget(name: "GraphQLRxSwiftTests", dependencies: ["GraphQLRxSwift"]),
]
)
9 changes: 6 additions & 3 deletions Sources/GraphQLRxSwift/ObservableEventStream.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import GraphQL
import NIO
import RxSwift

// EventStream wrapper for Observable
public class ObservableEventStream<Element> : EventStream<Element> {
public class ObservableEventStream<Element>: EventStream<Element> {
public var observable: Observable<Element>
init(_ observable: Observable<Element>) {
self.observable = observable
}

override open func map<To>(_ closure: @escaping (Element) throws -> To) -> EventStream<To> {
return ObservableEventStream<To>(observable.map(closure))
}
}

// Convenience types
public typealias ObservableSubscriptionEventStream = ObservableEventStream<Future<GraphQLResult>>

extension Observable {
public extension Observable {
// Convenience method for wrapping Observables in EventStreams
public func toEventStream() -> ObservableEventStream<Element> {
func toEventStream() -> ObservableEventStream<Element> {
return ObservableEventStream(self)
}
}
66 changes: 34 additions & 32 deletions Tests/GraphQLRxSwiftTests/GraphQL/SubscriptionSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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) {

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
Expand All @@ -20,16 +21,17 @@ struct Email : Encodable {
}
}

struct Inbox : Encodable {
let emails:[Email]
struct Inbox: Encodable {
let emails: [Email]
}

struct EmailEvent : Encodable {
let email:Email
let inbox:Inbox
struct EmailEvent: Encodable {
let email: Email
let inbox: Inbox
}

// MARK: Schema

let EmailType = try! GraphQLObjectType(
name: "Email",
fields: [
Expand Down Expand Up @@ -62,7 +64,7 @@ let InboxType = try! GraphQLObjectType(
"unread": GraphQLField(
type: GraphQLInt,
resolve: { inbox, _, _, _ in
(inbox as! Inbox).emails.filter({$0.unread}).count
(inbox as! Inbox).emails.filter { $0.unread }.count
}
),
]
Expand All @@ -75,15 +77,15 @@ let EmailEventType = try! GraphQLObjectType(
),
"inbox": GraphQLField(
type: InboxType
)
),
]
)
let EmailQueryType = try! GraphQLObjectType(
name: "Query",
fields: [
"inbox": GraphQLField(
type: InboxType
)
),
]
)

Expand All @@ -95,40 +97,40 @@ class EmailDb {
var emails: [Email]
let publisher: PublishSubject<Any>
let disposeBag: DisposeBag

init() {
emails = [
Email(
from: "[email protected]",
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) {
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
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")
throw GraphQLError(message: "\(type(of: emailAny)) is not Email")
}
},
subscribe: {_, args, _, eventLoopGroup, _ throws -> EventLoopFuture<Any?> in
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 {
Expand All @@ -141,10 +143,10 @@ class EmailDb {
}
)
}

/// Generates a subscription to the database using the default schema and resolvers
func subscription (
query:String,
func subscription(
query: String,
variableValues: [String: Map] = [:]
) throws -> SubscriptionEventStream {
return try createSubscription(schema: defaultSchema(), query: query, variableValues: variableValues)
Expand All @@ -163,11 +165,11 @@ func emailSchemaWithResolvers(resolve: GraphQLFieldResolve? = nil, subscribe: Gr
args: [
"priority": GraphQLArgument(
type: GraphQLInt
)
),
],
resolve: resolve,
subscribe: subscribe
)
),
]
)
)
Expand All @@ -186,13 +188,13 @@ func createSubscription(
instrumentation: NoOpInstrumentation,
schema: schema,
request: query,
rootValue: Void(),
context: Void(),
rootValue: (),
context: (),
eventLoopGroup: eventLoopGroup,
variableValues: variableValues,
operationName: nil
).wait()

if let stream = result.stream {
return stream
} else {
Expand Down
Loading