Creating a client
Before you can execute GraphQL operations in your app, you need to initialize an ApolloClient instance.
Basic client creation
In most cases, you can create a single shared instance of ApolloClient and point it at your GraphQL server. The recommended way to do this is to create a singleton like so:
1import Foundation
2import Apollo
3
4class Network {
5 static let shared = Network()
6
7 private(set) lazy var apollo = ApolloClient(url: URL(string: "http://localhost:4000/graphql")!)
8}Under the hood, this creates a client using the default network transport (RequestChainNetworkTransport) and default configuration. You can then use this client from anywhere in your code with Network.shared.apollo.
You should use this initializer unless you need to customize your client's network communication, such as to enable subscription operations.
Advanced client creation
For advanced use cases, you can use a different ApolloClient initializer that enables you to customize your client's network transport:
1public init(networkTransport: NetworkTransport,
2 store: ApolloStore)Apollo iOS provides the following classes that conform to the NetworkTransport protocol:
| Class | Description |
|---|---|
RequestChainNetworkTransport | Passes a request through a chain of interceptors that can interact with the request both before and after it's transmitted. Uses standard HTTP requests to communicate with the server. |
WebSocketTransport | Transmits all GraphQL operations via WebSocket. Requires the Apollo/WebSocket sub-spec. |
SplitNetworkTransport | Transmits subscription operations via WebSocket and other operations via HTTP. Requires the Apollo/WebSocket sub-spec. |
For more information on
RequestChainNetworkTransport, see Request pipeline in Apollo iOS.For more information on
WebSocketTransportandSplitNetworkTransport, see Subscriptions.