Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
1801584810_4
class HTTPClient {
let manager = Alamofire.SessionManager()
/// Issue a GraphQL request based on the given `Query`
func request<T: Query>(query: T, completion: @escaping (Result<T.Response, Error>) -> ()) {
// all GraphQL requests go to the same endpoint
let url = URL(string: "\(baseUrl)/graphql")!
// under the hood, a GraphQL request is just a `POST` with a funky `body`
manager.request(url, method: .post, parameters: query.body).responseJSON {
response in
switch response.result {
case .success: completion(.success(try! T.decodeResponse(response.data)))
case .failure(let error): completion(.failure(error))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment