ApolloAlamofire 0.6.0

ApolloAlamofire 0.6.0

Maintained by Max Desiatov.



 
Depends on:
Alamofire~> 4.9.1
Apollo~> 0.19.0
 

  • By
  • Max Desiatov

ApolloAlamofire

Alamofire transport implementation for Apollo GraphQL iOS library.

CI Status Version License Platform

What's This For?

If you 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

  • Xcode 10.0 or later
  • Swift 4.2 or later
  • iOS 9.0 deployment target or later.

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

If you need Xcode 9 and Swift 4.0 support in your project you can use earlier version of ApolloAlamofire: 0.3.0.

Installation

CocoaPods

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. You can install it with the following command:

$ gem install cocoapods

Navigate to the project directory and create Podfile with the following command:

$ pod install

Inside of your Podfile, specify the ApolloAlamofire pod:

pod 'ApolloAlamofire', '~> 0.6.0'

Then, run the following command:

$ pod install

Open the the YourApp.xcworkspace file that was created. This should be the file you use everyday to create your app, instead of the YourApp.xcodeproj file.

Carthage

Carthage is a dependency manager that builds your dependencies and provides you with binary frameworks.

Carthage can be installed with Homebrew using the following command:

$ brew update
$ brew install carthage

Inside of your Cartfile, add GitHub path to ApolloAlamofire:

github "graphql-community/ApolloAlamofire" ~> 0.6.0

Then, run the following command to build the framework:

$ carthage update

Drag the built framework into your Xcode project.

Maintainer

Max Desiatov

License

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