RxApollo
RxApollo copied to clipboard
Fetch should return Observable
Not sure whether this repo is dead or not, but gonna put it up here in case someone else encounters the same problem.
If you set the cache policy to .returnCacheDataAndFetch
and there is some local cache the resultHandler
actually gets called twice - first with the immediate local cache and second time with the updates from server.
For that reason it can't be Maybe
(it can only return 0 or 1 times) but it has to be Observable. This results in a strange bug that causes your data not to reload but only use the cache from the previous request.
public func fetch<Query: GraphQLQuery>(
query: Query,
cachePolicy: CachePolicy = .returnCacheDataElseFetch,
queue: DispatchQueue = DispatchQueue.main) -> Observable<Query.Data> {
return Observable.create { observable in
let cancellable = self.client.fetch(query: query, cachePolicy: cachePolicy, queue: queue) { result, error in
if let error = error {
observable.onError(error)
} else if let errors = result?.errors {
observable.onError(RxApolloError.graphQLErrors(errors))
} else if let data = result?.data {
observable.onNext(data)
} else {
observable.onCompleted()
}
}
return Disposables.create {
cancellable.cancel()
}
}
}