Swiftpack.co - Package - graphql-community/ApolloAlamofire

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

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

pod 'ApolloAlamofire', '~> 0.4.0'

Maintainer

Max Desiatov

License

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

Github

link
Stars: 41
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

0.4.0 - Apr 7, 2019

This update fixes compatibility with Xcode 10.2 and Swift 5.0. This release also supports Swift 4.2 and Xcode 10.0 and 10.1, but unfortunately maintaining a suite of tests and Xcode project settings that support Swift 4.0 and Xcode 9 as well does not seem feasible (PRs that fix this are welcome!).

No other changes were made and ApolloAlamofire 0.3.0 is recommended if you need Xcode 9 support.

0.3.0 - Apr 5, 2019

Updated project for Swift 4.2 and Apollo 0.9. Also bumped Example project Alamofire to 4.7.3.

0.2.1 - May 3, 2018

This release has no code changes and is only tagged to ship new README.md to cocoapods.org web page.

0.2.0 - May 3, 2018

headers and loggingEnabled property made public variables on 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.