Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-perry committed Jun 29, 2020
0 parents commit 7420c64
Show file tree
Hide file tree
Showing 8 changed files with 570 additions and 0 deletions.
91 changes: 91 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
Packages/
Package.pins
Package.resolved
*.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
.swiftpm

.build/
.DS_Store

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build/

# Accio dependency management
Dependencies/
.accio/

# fastlane
#
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Joel Perry

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "ApolloCombine",
platforms: [
.iOS(.v13)
],
products: [
.library(name: "ApolloCombine",targets: ["ApolloCombine"]),
],
dependencies: [
.package(name: "Apollo",
url: "https://github.com/apollographql/apollo-ios.git", from: "0.28.0")
],
targets: [
.target(name: "ApolloCombine", dependencies: [.product(name: "Apollo", package: "Apollo")]),
.testTarget(name: "ApolloCombineTests", dependencies: ["ApolloCombine"])
]
)
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# ApolloCombine
![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/joel-perry/ApolloCombine)
[![Swift Package Manager Compatible](https://img.shields.io/badge/SPM-compatible-brightgreen.svg)](https://swift.org/package-manager/)
![GitHub](https://img.shields.io/github/license/joel-perry/ApolloCombine)

A collection of Combine publishers for the [Apollo iOS client](https://github.com/apollographql/apollo-ios).

## 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`, and `uploadPublisher` subscriptions will send `.finished` completions whey they are done.

```swift
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()

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

## License
ApolloCombine is released under the MIT license. [See LICENSE](LICENSE) for details.
79 changes: 79 additions & 0 deletions Sources/ApolloCombine/ApolloClientExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import Apollo
import Combine
import Foundation

public extension ApolloClient {

/// Fetches a query from the server or from the local cache, depending on the current contents of the cache and the specified cache policy.
///
/// - Parameters:
/// - query: The query to fetch.
/// - cachePolicy: A cache policy that specifies when results should be fetched from the server and when data should be loaded from the local cache.
/// - context: [optional] A context to use for the cache to work with results. Defaults to nil.
/// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
/// - Returns: A publisher that delivers results from the fetch operaion.
func fetchPublisher<Query: GraphQLQuery>(query: Query,
cachePolicy: CachePolicy = .returnCacheDataElseFetch,
context: UnsafeMutableRawPointer? = nil,
queue: DispatchQueue = .main) -> Publishers.ApolloFetch<Query> {
let config = Publishers.ApolloFetchConfiguration(client: self, query: query, cachePolicy: cachePolicy, context: context, queue: queue)
return Publishers.ApolloFetch(with: config)
}

/// Performs a mutation by sending it to the server.
///
/// - Parameters:
/// - mutation: The mutation to perform.
/// - context: [optional] A context to use for the cache to work with results. Defaults to nil.
/// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
/// - Returns: A publisher that delivers results from the perform operaion.
func performPublisher<Mutation: GraphQLMutation>(mutation: Mutation,
context: UnsafeMutableRawPointer? = nil,
queue: DispatchQueue = .main) -> Publishers.ApolloPerform<Mutation> {
let config = Publishers.ApolloPerformConfiguration(client: self, mutation: mutation, context: context, queue: queue)
return Publishers.ApolloPerform(with: config)
}

/// Uploads the given files with the given operation.
///
/// - Parameters:
/// - operation: The operation to send
/// - context: [optional] A context to use for the cache to work with results. Should default to nil.
/// - files: An array of `GraphQLFile` objects to send.
/// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
/// - Returns: A publisher that delivers results from the upload operaion.
func uploadPublisher<Operation: GraphQLOperation>(operation: Operation,
context: UnsafeMutableRawPointer? = nil,
files: [GraphQLFile],
queue: DispatchQueue = .main) -> Publishers.ApolloUpload<Operation> {
let config = Publishers.ApolloUploadConfiguration(client: self, operation: operation, context: context, files: files, queue: queue)
return Publishers.ApolloUpload(with: config)
}

/// Watches a query by first fetching an initial result from the server or from the local cache, depending on the current contents of the cache and the specified cache policy. After the initial fetch, the returned publisher will get notified whenever any of the data the query result depends on changes in the local cache, and delivers the new result.
///
/// - Parameters:
/// - query: The query to watch.
/// - cachePolicy: A cache policy that specifies when results should be fetched from the server or from the local cache.
/// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
/// - Returns: A publisher that delivers results from the watch operaion.
func watchPublisher<Query: GraphQLQuery>(query: Query,
cachePolicy: CachePolicy = .returnCacheDataElseFetch,
queue: DispatchQueue = .main) -> Publishers.ApolloWatch<Query> {
let config = Publishers.ApolloWatchConfiguration(client: self, query: query, cachePolicy: cachePolicy, queue: queue)
return Publishers.ApolloWatch(with: config)
}

/// Subscribe to a subscription
///
/// - Parameters:
/// - subscription: The subscription to subscribe to.
/// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
/// - Returns: A publisher that delivers results from the subscribe operaion.
func subscribePublisher<Subscription: GraphQLSubscription>(subscription: Subscription,
queue: DispatchQueue = .main) -> Publishers.ApolloSubscribe<Subscription> {
let config = Publishers.ApolloSubscribeConfiguration(client: self, subscription: subscription, queue: queue)
return Publishers.ApolloSubscribe(with: config)
}
}

Loading

0 comments on commit 7420c64

Please sign in to comment.