From 71235db82b108774f86b699ebe3f0981a0e6c495 Mon Sep 17 00:00:00 2001 From: Joel Perry Date: Tue, 6 Apr 2021 14:03:39 -0400 Subject: [PATCH] Extend ApolloClientProtocol; update method signatures and documentation --- README.md | 2 +- .../ApolloClientExtensions.swift | 29 +++++++++---------- .../ApolloCombine/PublishersExtensions.swift | 14 +++++---- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6a80db1..adf6d8b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/Sources/ApolloCombine/ApolloClientExtensions.swift b/Sources/ApolloCombine/ApolloClientExtensions.swift index 293f496..0d6544f 100644 --- a/Sources/ApolloCombine/ApolloClientExtensions.swift +++ b/Sources/ApolloCombine/ApolloClientExtensions.swift @@ -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: Query, cachePolicy: CachePolicy = .returnCacheDataElseFetch, contextIdentifier: UUID? = nil, @@ -24,12 +24,13 @@ 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: Mutation, + publishResultToStore: Bool = true, queue: DispatchQueue = .main) -> Publishers.ApolloPerform { - 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) } @@ -37,10 +38,9 @@ public extension ApolloClient { /// /// - 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: Operation, files: [GraphQLFile], queue: DispatchQueue = .main) -> Publishers.ApolloUpload { @@ -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: Query, cachePolicy: CachePolicy = .returnCacheDataElseFetch) -> Publishers.ApolloWatch { let config = Publishers.ApolloWatchConfiguration(client: self, query: query, cachePolicy: cachePolicy) @@ -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: Subscription, queue: DispatchQueue = .main) -> Publishers.ApolloSubscribe { let config = Publishers.ApolloSubscribeConfiguration(client: self, subscription: subscription, queue: queue) @@ -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) diff --git a/Sources/ApolloCombine/PublishersExtensions.swift b/Sources/ApolloCombine/PublishersExtensions.swift index c371907..e5aed73 100644 --- a/Sources/ApolloCombine/PublishersExtensions.swift +++ b/Sources/ApolloCombine/PublishersExtensions.swift @@ -27,7 +27,7 @@ public extension Publishers { } struct ApolloFetchConfiguration { - let client: ApolloClient + let client: ApolloClientProtocol let query: Query let cachePolicy: CachePolicy let contextIdentifier: UUID? @@ -91,8 +91,9 @@ public extension Publishers { } struct ApolloPerformConfiguration { - let client: ApolloClient + let client: ApolloClientProtocol let mutation: Mutation + let publishResultToStore: Bool let queue: DispatchQueue } @@ -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 { @@ -147,7 +149,7 @@ public extension Publishers { } struct ApolloUploadConfiguration { - let client: ApolloClient + let client: ApolloClientProtocol let operation: Operation let files: [GraphQLFile] let queue: DispatchQueue @@ -205,7 +207,7 @@ public extension Publishers { } struct ApolloWatchConfiguration { - let client: ApolloClient + let client: ApolloClientProtocol let query: Query let cachePolicy: CachePolicy } @@ -254,7 +256,7 @@ public extension Publishers { } struct ApolloSubscribeConfiguration { - let client: ApolloClient + let client: ApolloClientProtocol let subscription: Subscription let queue: DispatchQueue } @@ -303,7 +305,7 @@ public extension Publishers { } struct ApolloClearCacheConfiguration { - let client: ApolloClient + let client: ApolloClientProtocol let queue: DispatchQueue }