11import Foundation
22import Dispatch
33
4- /// An object that can be used to cancel an in progress action.
5- public protocol Cancellable : class {
6- /// Cancel an in progress action.
7- func cancel( )
8- }
9-
104/// A cache policy that specifies whether results should be fetched from the server or loaded from the local cache.
115public enum CachePolicy {
126 /// Return data from the cache if available, else fetch results from the server.
@@ -82,20 +76,19 @@ public class ApolloClient {
8276 ///
8377 /// - Parameters:
8478 /// - query: The query to fetch.
85- /// - fetchHTTPMethod: The HTTP Method to be used.
8679 /// - cachePolicy: A cache policy that specifies when results should be fetched from the server and when data should be loaded from the local cache.
8780 /// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
8881 /// - resultHandler: An optional closure that is called when query results are available or when an error occurs.
8982 /// - Returns: An object that can be used to cancel an in progress fetch.
90- @discardableResult public func fetch< Query: GraphQLQuery > ( query: Query , fetchHTTPMethod : FetchHTTPMethod = . POST , cachePolicy: CachePolicy = . returnCacheDataElseFetch, context: UnsafeMutableRawPointer ? = nil , queue: DispatchQueue = DispatchQueue . main, resultHandler: GraphQLResultHandler < Query . Data > ? = nil ) -> Cancellable {
83+ @discardableResult public func fetch< Query: GraphQLQuery > ( query: Query , cachePolicy: CachePolicy = . returnCacheDataElseFetch, context: UnsafeMutableRawPointer ? = nil , queue: DispatchQueue = DispatchQueue . main, resultHandler: GraphQLResultHandler < Query . Data > ? = nil ) -> Cancellable {
9184 let resultHandler = wrapResultHandler ( resultHandler, queue: queue)
9285
9386 // If we don't have to go through the cache, there is no need to create an operation
9487 // and we can return a network task directly
9588 if cachePolicy == . fetchIgnoringCacheData || cachePolicy == . fetchIgnoringCacheCompletely {
96- return send ( operation: query, fetchHTTPMethod : fetchHTTPMethod , shouldPublishResultToStore: cachePolicy != . fetchIgnoringCacheCompletely, context: context, resultHandler: resultHandler)
89+ return send ( operation: query, shouldPublishResultToStore: cachePolicy != . fetchIgnoringCacheCompletely, context: context, resultHandler: resultHandler)
9790 } else {
98- let operation = FetchQueryOperation ( client: self , query: query, fetchHTTPMethod : fetchHTTPMethod , cachePolicy: cachePolicy, context: context, resultHandler: resultHandler)
91+ let operation = FetchQueryOperation ( client: self , query: query, cachePolicy: cachePolicy, context: context, resultHandler: resultHandler)
9992 operationQueue. addOperation ( operation)
10093 return operation
10194 }
@@ -110,8 +103,9 @@ public class ApolloClient {
110103 /// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
111104 /// - resultHandler: An optional closure that is called when query results are available or when an error occurs.
112105 /// - Returns: A query watcher object that can be used to control the watching behavior.
113- public func watch< Query: GraphQLQuery > ( query: Query , fetchHTTPMethod: FetchHTTPMethod = . POST, cachePolicy: CachePolicy = . returnCacheDataElseFetch, queue: DispatchQueue = DispatchQueue . main, resultHandler: @escaping GraphQLResultHandler < Query . Data > ) -> GraphQLQueryWatcher < Query > {
114- let watcher = GraphQLQueryWatcher ( client: self , query: query, fetchHTTPMethod: fetchHTTPMethod, resultHandler: wrapResultHandler ( resultHandler, queue: queue) )
106+
107+ public func watch< Query: GraphQLQuery > ( query: Query , cachePolicy: CachePolicy = . returnCacheDataElseFetch, queue: DispatchQueue = DispatchQueue . main, resultHandler: @escaping GraphQLResultHandler < Query . Data > ) -> GraphQLQueryWatcher < Query > {
108+ let watcher = GraphQLQueryWatcher ( client: self , query: query, resultHandler: wrapResultHandler ( resultHandler, queue: queue) )
115109 watcher. fetch ( cachePolicy: cachePolicy)
116110 return watcher
117111 }
@@ -124,8 +118,8 @@ public class ApolloClient {
124118 /// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
125119 /// - resultHandler: An optional closure that is called when mutation results are available or when an error occurs.
126120 /// - Returns: An object that can be used to cancel an in progress mutation.
127- @discardableResult public func perform< Mutation: GraphQLMutation > ( mutation: Mutation , fetchHTTPMethod : FetchHTTPMethod = . POST , context: UnsafeMutableRawPointer ? = nil , queue: DispatchQueue = DispatchQueue . main, resultHandler: GraphQLResultHandler < Mutation . Data > ? = nil ) -> Cancellable {
128- return send ( operation: mutation, fetchHTTPMethod : fetchHTTPMethod , shouldPublishResultToStore: true , context: context, resultHandler: wrapResultHandler ( resultHandler, queue: queue) )
121+ @discardableResult public func perform< Mutation: GraphQLMutation > ( mutation: Mutation , context: UnsafeMutableRawPointer ? = nil , queue: DispatchQueue = DispatchQueue . main, resultHandler: GraphQLResultHandler < Mutation . Data > ? = nil ) -> Cancellable {
122+ return send ( operation: mutation, shouldPublishResultToStore: true , context: context, resultHandler: wrapResultHandler ( resultHandler, queue: queue) )
129123 }
130124
131125 /// Subscribe to a subscription
@@ -136,12 +130,12 @@ public class ApolloClient {
136130 /// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
137131 /// - resultHandler: An optional closure that is called when mutation results are available or when an error occurs.
138132 /// - Returns: An object that can be used to cancel an in progress subscription.
139- @discardableResult public func subscribe< Subscription: GraphQLSubscription > ( subscription: Subscription , fetchHTTPMethod : FetchHTTPMethod = . POST , queue: DispatchQueue = DispatchQueue . main, resultHandler: @escaping GraphQLResultHandler < Subscription . Data > ) -> Cancellable {
140- return send ( operation: subscription, fetchHTTPMethod : fetchHTTPMethod , shouldPublishResultToStore: true , context: nil , resultHandler: wrapResultHandler ( resultHandler, queue: queue) )
133+ @discardableResult public func subscribe< Subscription: GraphQLSubscription > ( subscription: Subscription , queue: DispatchQueue = DispatchQueue . main, resultHandler: @escaping GraphQLResultHandler < Subscription . Data > ) -> Cancellable {
134+ return send ( operation: subscription, shouldPublishResultToStore: true , context: nil , resultHandler: wrapResultHandler ( resultHandler, queue: queue) )
141135 }
142136
143- fileprivate func send< Operation: GraphQLOperation > ( operation: Operation , fetchHTTPMethod : FetchHTTPMethod , shouldPublishResultToStore: Bool , context: UnsafeMutableRawPointer ? , resultHandler: @escaping GraphQLResultHandler < Operation . Data > ) -> Cancellable {
144- return networkTransport. send ( operation: operation, fetchHTTPMethod : fetchHTTPMethod ) { ( response, error) in
137+ fileprivate func send< Operation: GraphQLOperation > ( operation: Operation , shouldPublishResultToStore: Bool , context: UnsafeMutableRawPointer ? , resultHandler: @escaping GraphQLResultHandler < Operation . Data > ) -> Cancellable {
138+ return networkTransport. send ( operation: operation) { ( response, error) in
145139 guard let response = response else {
146140 resultHandler ( nil , error)
147141 return
@@ -189,17 +183,15 @@ private func wrapResultHandler<Data>(_ resultHandler: GraphQLResultHandler<Data>
189183private final class FetchQueryOperation < Query: GraphQLQuery > : AsynchronousOperation , Cancellable {
190184 let client : ApolloClient
191185 let query : Query
192- let fetchHTTPMethod : FetchHTTPMethod
193186 let cachePolicy : CachePolicy
194187 let context : UnsafeMutableRawPointer ?
195188 let resultHandler : GraphQLResultHandler < Query . Data >
196189
197190 private var networkTask : Cancellable ?
198191
199- init ( client: ApolloClient , query: Query , fetchHTTPMethod : FetchHTTPMethod , cachePolicy: CachePolicy , context: UnsafeMutableRawPointer ? , resultHandler: @escaping GraphQLResultHandler < Query . Data > ) {
192+ init ( client: ApolloClient , query: Query , cachePolicy: CachePolicy , context: UnsafeMutableRawPointer ? , resultHandler: @escaping GraphQLResultHandler < Query . Data > ) {
200193 self . client = client
201194 self . query = query
202- self . fetchHTTPMethod = fetchHTTPMethod
203195 self . cachePolicy = cachePolicy
204196 self . context = context
205197 self . resultHandler = resultHandler
@@ -244,7 +236,7 @@ private final class FetchQueryOperation<Query: GraphQLQuery>: AsynchronousOperat
244236 }
245237
246238 func fetchFromNetwork( ) {
247- networkTask = client. send ( operation: query, fetchHTTPMethod : fetchHTTPMethod , shouldPublishResultToStore: true , context: context) { ( result, error) in
239+ networkTask = client. send ( operation: query, shouldPublishResultToStore: true , context: context) { ( result, error) in
248240 self . resultHandler ( result, error)
249241 self . state = . finished
250242 return
0 commit comments