Created
November 14, 2018 16:30
-
-
Save attheodo/f7345345eb1f9051b965a3017481eb28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@discardableResult | |
public func mutate<T: GraphQLMutation>(_ mutation: T, | |
completion: @escaping RequestResult<GQLResponse<T.Data>>) -> Cancellable | |
{ | |
DispatchQueue.main.async { | |
UIApplication.shared.isNetworkActivityIndicatorVisible = true | |
} | |
return gqlClient.perform(mutation: mutation) { (result, error) in | |
self.add(graphQLOperationWithId: mutation.id) | |
self.handleGQLResponse(operation: mutation, | |
result: result, | |
error: error as? GraphQLHTTPResponseError, | |
completion: completion) | |
} | |
} | |
// MARK: - Private Methods | |
fileprivate func handleGQLResponse<T: GraphQLOperation>(operation: T, | |
result: GraphQLResult<T.Data>?, | |
error: GraphQLHTTPResponseError?, | |
completion: @escaping RequestResult<GQLResponse<T.Data>>) | |
{ | |
DispatchQueue.main.async { | |
UIApplication.shared.isNetworkActivityIndicatorVisible = false | |
} | |
if let error = error { | |
let statusCode = error.response.statusCode | |
// TODO: Implement retry logic | |
if operation.operationType == .query { | |
self.query(operation, completion: completion) | |
} else if operation.operationType == .mutation { | |
self.mutate(operation, completion: completion) | |
} | |
completion(.failure(error)) | |
return | |
} | |
remove(graphQLOperationWithId: operation.id) | |
if let gqlError = result?.errors?.first { | |
let e = Error.graphQLErrorEnum(forGraphQLError: gqlError) as Error | |
completion(.failure(e)) | |
return | |
} | |
if let data = result?.data { | |
let r = GQLResponse(data: data, cacheHit: result?.source == .cache) | |
completion(.success(r)) | |
return | |
} else { | |
completion(.failure(Error.gqlError(.emptyResponse))) | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment