Skip to content

Commit

Permalink
Fixes joel-perry#4
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-perry committed Feb 19, 2021
1 parent ed24e6c commit 0162312
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
A collection of Combine publishers for the [Apollo iOS client](https://github.com/apollographql/apollo-ios).

## Versions
The Apollo iOS client uses a new networking stack beginning with 0.34.0-beta2, so the version of ApolloCombine you should use depends on whether you have adopted this change.
The Apollo iOS client uses a new networking stack beginning with 0.34.0, so the version of ApolloCombine you should use depends on whether you have adopted this change.

- Use ApolloCombine release 0.2.2 if you have not upgraded to the new networking stack
- Use ApolloCombine release 0.3.0 and above if you are using the new networking stack
Expand All @@ -16,7 +16,7 @@ The extension to ApolloClient (in the aptly named `ApolloClientExtensions`) incl

When `cancel()` is invoked on a subscription, the underlying Apollo operation is also cancelled.

`fetchPublisher`, `performPublisher`, and `uploadPublisher` subscriptions will send `.finished` completions whey they are done.
`fetchPublisher`, `performPublisher`, `uploadPublisher`, and `clearCachePublisher` will send a completion when the operation has completed.

```swift
import ApolloCombine
Expand All @@ -34,6 +34,30 @@ let fetchSubscription = client.fetchPublisher(query: MyQuery(), cachePolicy: .fe
fetchSubscription.cancel()

```

`watchPublisher` and `subscribePublisher` will not send a completion, instead delivering data and errors as the value of the subscription. This allows these operations to remain open after an error has been encountered. As such, it is important to explicitly cancel these subscriptions when you are done with them to avoid memory leaks.

```swift
import ApolloCombine

let client = ApolloClient(...)

let watchSubscription = client.watchPublisher(query: MyQuery())
.sink(receiveValue: { operationResult in
switch operationResult {
case .success(let graphQLResult):
// handle returned data

case .failure(let error):
// handle error
})

// Don't forget to cancel when you're done
watchSubscription.cancel()

```


## Installation
### Swift Package Manager
The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler. Use Xcode’s Swift Packages option, which is located within the File menu.
Expand Down
28 changes: 8 additions & 20 deletions Sources/ApolloCombine/PublishersExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ public extension Publishers {

// MARK: - Watch
struct ApolloWatch<Query: GraphQLQuery>: Publisher {
public typealias Output = GraphQLResult<Query.Data>
public typealias Failure = Error
public typealias Output = Result<GraphQLResult<Query.Data>, Error>
public typealias Failure = Never

private let configuration: ApolloWatchConfiguration<Query>

Expand All @@ -211,7 +211,7 @@ public extension Publishers {
}

private final class ApolloWatchSubscription<S: Subscriber, Query: GraphQLQuery>: Subscription
where S.Failure == Error, S.Input == GraphQLResult<Query.Data> {
where S.Failure == Never, S.Input == Result<GraphQLResult<Query.Data>, Error> {
private let configuration: ApolloWatchConfiguration<Query>
private var subscriber: S?
private var task: GraphQLQueryWatcher<Query>?
Expand All @@ -225,13 +225,7 @@ public extension Publishers {
task = configuration.client.watch(query: configuration.query,
cachePolicy: configuration.cachePolicy)
{ [weak self] result in
switch result {
case .success(let data):
_ = self?.subscriber?.receive(data)

case .failure(let error):
self?.subscriber?.receive(completion: .failure(error))
}
_ = self?.subscriber?.receive(result)
}
}

Expand All @@ -244,8 +238,8 @@ public extension Publishers {

// MARK: - Subscribe
struct ApolloSubscribe<Subscription: GraphQLSubscription>: Publisher {
public typealias Output = GraphQLResult<Subscription.Data>
public typealias Failure = Error
public typealias Output = Result<GraphQLResult<Subscription.Data>, Error>
public typealias Failure = Never

private let configuration: ApolloSubscribeConfiguration<Subscription>

Expand All @@ -266,7 +260,7 @@ public extension Publishers {
}

private final class ApolloSubscribeSubscription<S: Subscriber, Sub: GraphQLSubscription>: Subscription
where S.Failure == Error, S.Input == GraphQLResult<Sub.Data> {
where S.Failure == Never, S.Input == Result<GraphQLResult<Sub.Data>, Error> {
private let configuration: ApolloSubscribeConfiguration<Sub>
private var subscriber: S?
private var task: Apollo.Cancellable?
Expand All @@ -280,13 +274,7 @@ public extension Publishers {
task = configuration.client.subscribe(subscription: configuration.subscription,
queue: configuration.queue)
{ [weak self] result in
switch result {
case .success(let data):
_ = self?.subscriber?.receive(data)

case .failure(let error):
self?.subscriber?.receive(completion: .failure(error))
}
_ = self?.subscriber?.receive(result)
}
}

Expand Down

0 comments on commit 0162312

Please sign in to comment.