Skip to content

Commit

Permalink
Extend ApolloClientProtocol; update method signatures and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-perry committed Apr 6, 2021
1 parent c65fd1a commit 71235db
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The Apollo iOS client uses a new networking stack beginning with 0.34.0, so the
- 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.
The extension to ApolloClientProtocol (in the aptly named `ApolloClientExtensions`) includes methods whose inputs mirror the existing ApolloClientProtocol 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.

Expand Down
29 changes: 14 additions & 15 deletions Sources/ApolloCombine/ApolloClientExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import Apollo
import Combine
import Foundation

public extension ApolloClient {
public extension ApolloClientProtocol {

/// 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.
/// - cachePolicy: A cache policy that specifies when results should be fetched from the server and when data should be loaded from the local cache. Defaults to ``.returnCacheDataElseFetch`.
/// - contextIdentifier: [optional] A unique identifier for this request, to help with deduping cache hits for watchers. 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.
/// - Returns: A publisher that delivers results from the fetch operation.
func fetchPublisher<Query: GraphQLQuery>(query: Query,
cachePolicy: CachePolicy = .returnCacheDataElseFetch,
contextIdentifier: UUID? = nil,
Expand All @@ -24,23 +24,23 @@ public extension ApolloClient {
///
/// - Parameters:
/// - mutation: The mutation to perform.
/// - context: [optional] A context to use for the cache to work with results. Defaults to nil.
/// - publishResultToStore: If `true`, this will publish the result returned from the operation to the cache store. Default is `true`.
/// - 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.
/// - Returns: A publisher that delivers results from the perform operation.
func performPublisher<Mutation: GraphQLMutation>(mutation: Mutation,
publishResultToStore: Bool = true,
queue: DispatchQueue = .main) -> Publishers.ApolloPerform<Mutation> {
let config = Publishers.ApolloPerformConfiguration(client: self, mutation: mutation, queue: queue)
let config = Publishers.ApolloPerformConfiguration(client: self, mutation: mutation, publishResultToStore: publishResultToStore, 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.
/// - Returns: A publisher that delivers results from the upload operation.
func uploadPublisher<Operation: GraphQLOperation>(operation: Operation,
files: [GraphQLFile],
queue: DispatchQueue = .main) -> Publishers.ApolloUpload<Operation> {
Expand All @@ -52,9 +52,8 @@ public extension ApolloClient {
///
/// - 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.
/// - cachePolicy: A cache policy that specifies when results should be fetched from the server or from the local cache. Defaults to ``.returnCacheDataElseFetch`.
/// - Returns: A publisher that delivers results from the watch operation.
func watchPublisher<Query: GraphQLQuery>(query: Query,
cachePolicy: CachePolicy = .returnCacheDataElseFetch) -> Publishers.ApolloWatch<Query> {
let config = Publishers.ApolloWatchConfiguration(client: self, query: query, cachePolicy: cachePolicy)
Expand All @@ -66,7 +65,7 @@ public extension ApolloClient {
/// - 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.
/// - Returns: A publisher that delivers results from the subscribe operation.
func subscribePublisher<Subscription: GraphQLSubscription>(subscription: Subscription,
queue: DispatchQueue = .main) -> Publishers.ApolloSubscribe<Subscription> {
let config = Publishers.ApolloSubscribeConfiguration(client: self, subscription: subscription, queue: queue)
Expand All @@ -76,8 +75,8 @@ public extension ApolloClient {
/// Request to clear the cache
///
/// - Parameters:
/// - 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 cache operaion.
/// - callbackQueue: The queue to fall back on. Defaults to the main queue.
/// - Returns: A publisher that delivers results from the clear operaion.
func clearCachePublisher(queue: DispatchQueue = .main) -> Publishers.ApolloClearCache {
let config = Publishers.ApolloClearCacheConfiguration(client: self,
queue: queue)
Expand Down
14 changes: 8 additions & 6 deletions Sources/ApolloCombine/PublishersExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public extension Publishers {
}

struct ApolloFetchConfiguration<Query: GraphQLQuery> {
let client: ApolloClient
let client: ApolloClientProtocol
let query: Query
let cachePolicy: CachePolicy
let contextIdentifier: UUID?
Expand Down Expand Up @@ -91,8 +91,9 @@ public extension Publishers {
}

struct ApolloPerformConfiguration<Mutation: GraphQLMutation> {
let client: ApolloClient
let client: ApolloClientProtocol
let mutation: Mutation
let publishResultToStore: Bool
let queue: DispatchQueue
}

Expand All @@ -109,6 +110,7 @@ public extension Publishers {

func request(_ demand: Subscribers.Demand) {
task = configuration.client.perform(mutation: configuration.mutation,
publishResultToStore: configuration.publishResultToStore,
queue: configuration.queue)
{ [weak self] result in
switch result {
Expand Down Expand Up @@ -147,7 +149,7 @@ public extension Publishers {
}

struct ApolloUploadConfiguration<Operation: GraphQLOperation> {
let client: ApolloClient
let client: ApolloClientProtocol
let operation: Operation
let files: [GraphQLFile]
let queue: DispatchQueue
Expand Down Expand Up @@ -205,7 +207,7 @@ public extension Publishers {
}

struct ApolloWatchConfiguration<Query: GraphQLQuery> {
let client: ApolloClient
let client: ApolloClientProtocol
let query: Query
let cachePolicy: CachePolicy
}
Expand Down Expand Up @@ -254,7 +256,7 @@ public extension Publishers {
}

struct ApolloSubscribeConfiguration<Subscription: GraphQLSubscription> {
let client: ApolloClient
let client: ApolloClientProtocol
let subscription: Subscription
let queue: DispatchQueue
}
Expand Down Expand Up @@ -303,7 +305,7 @@ public extension Publishers {
}

struct ApolloClearCacheConfiguration {
let client: ApolloClient
let client: ApolloClientProtocol
let queue: DispatchQueue
}

Expand Down

0 comments on commit 71235db

Please sign in to comment.