Swiftpack.co - Package - joel-perry/ApolloCombine

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

joel-perry/ApolloCombine

A collection of Combine publishers for the Apollo iOS client

ApolloCombine

GitHub release (latest SemVer) Swift Package Manager Compatible GitHub

A collection of Combine publishers for the Apollo iOS client.

Versions

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

Usage

The extension to ApolloClient (in the aptly named ApolloClientExtensions) includes methods whose inputs mirror the existing ApolloClient operation methods. Instead of including a result handler, though, these methods return Combine publishers that deliver the results of the operation to subscribers.

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

fetchPublisher, performPublisher, uploadPublisher, and clearCachePublisher will send a completion when the operation has completed.

import ApolloCombine

let client = ApolloClient(...)

let fetchSubscription = client.fetchPublisher(query: MyQuery(), cachePolicy: .fetchIgnoringCacheData)
  .sink(receiveCompletion: { completion in
    // handle .finished or .failure 
    }, receiveValue: { graphQLResult in
      // handle returned fetch data      
  })

// Cancelling the Combine subscription will also cancel the underlying Apollo operation
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.

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 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.

License

ApolloCombine is released under the MIT license. See LICENSE for details.

Github

link
Stars: 26
Last commit: 5 days ago

Releases

- 2021-02-19T15:21:24

  • Update Apollo dependency versioning #3
  • Fix watchPublisher and subscribePublisher so they don't send completion on error #4

Thanks, @AF-cgi, @RoryKelly, and @MaxDesiatov