Skip to content

dearprakash/ApolloAlamofire

 
 

Repository files navigation

ApolloAlamofire

CI Status Version License Platform

What's This For?

If you've used Apollo iOS library, you may have stumbled upon a few limitations of a standard HTTPNetworkTransport provided with the library:

Fortunately, Apollo iOS provides a public NetworkTransport protocol that allows us to override behaviour that's limited. Looks like Alamofire is the most popular iOS networking library and all of the mentioned limitations can be solved with it. You also probably use Alamofire anyway to acquire authentication tokens for your GraphQL API, so it makes sense to integrate both Alamofire and Apollo iOS.

This package bundles a NetworkTransport implementation that wraps Alamofire and solves these limitations.

Example

When initialising a new ApolloClient instance instead of

let u = URL(string: "http://localhost:8080/graphql")!
let client = ApolloClient(url: u)

or instead of

let u = URL(string: "http://localhost:8080/graphql")!
let client = ApolloClient(networkTransport: HTTPNetworkTransport(url: u))

use

import ApolloAlamofire

//...
let u = URL(string: "http://localhost:8080/graphql")!
let client = ApolloClient(networkTransport: AlamofireTransport(url: u))

There are additional parameters available for AlamofireTransport initialiser, e.g. for a background session you can use it like this:

let c = URLSessionConfiguration.background(withIdentifier: "your-id")
let u = URL(string: "http://localhost:8080/graphql")!
let s = SessionManager(configuration: c)
let t = AlamofireTransport(url: u, sessionManager: s)
let client = ApolloClient(networkTransport: t)

like this for auth headers:

let token = "blah"
let u = URL(string: "http://localhost:8080/graphql")!
let h = ["Authorization": "Bearer \(token)"]
let t = AlamofireTransport(url: u, headers: h)
let client = ApolloClient(networkTransport: t)

or like this for request and response logging:

let u = URL(string: "http://localhost:8080/graphql")!
let t = AlamofireTransport(url: u, loggingEnabled: true)
let client = ApolloClient(networkTransport: t)

Both headers and loggingEnabled are also variable properties of AlamofireTransport. This allows you to change headers without instantiating a new transport, e.g. when a user logs out and a different user logs in you can swap authentication headers. If you switch logging dynamically, loggingEnabled property can be controlled in the same way without creating a new AlamofireTransport instance.

Nice feature of Alamofire is that request logging prints a ready for use curl command, which you can directly copy and paste in terminal to test a request.

All of the initialiser parameters except url have sensible default values and can be used in a combination that works best for you.

To run the example project, clone the repo, and open Example/ApolloAlamofire.xcworkspace in Xcode.

Requirements

The library is tested with Xcode 9.3 and Swift 4.1. It should compile in any other version of Xcode 9 and should be compatible with Swift 4.0, but is not tested with those version. Feel free to submit a PR to enable a better Travis CI testing matrix.

If you integrate the library with CocoaPods, Alamofire and Apollo iOS dependencies are pulled automatically. Currently tested versions that should be compatible are Alamofire 4.x and Apollo iOS 0.8.x.

Installation

ApolloAlamofire is available through CocoaPods. To install it, simply add the following line to your target configuration in your Podfile:

pod 'ApolloAlamofire'

Author

Max Desiatov

License

ApolloAlamofire is available under the MIT license. See the LICENSE file for more info.

About

Alamofire transport for Apollo iOS GraphQL library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 58.5%
  • Ruby 41.5%