Join us for GraphQL Summit, October 10-12 in San Diego. Use promo code ODYSSEY for $400 off your pass.
Launch GraphOS Studio
You're viewing documentation for a previous version of this software. Switch to the latest stable version.

Creating a client

Before you can execute GraphQL s 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:

import Foundation
import Apollo
class Network {
static let shared = Network()
private(set) lazy var apollo = ApolloClient(url: URL(string: "http://localhost:4000/graphql")!)

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 s.

Advanced client creation

For advanced use cases, you can use a different ApolloClient initializer that enables you to customize your client's network transport:

public init(networkTransport: NetworkTransport,
store: ApolloStore)

Apollo iOS provides the following classes that conform to the NetworkTransport protocol:

RequestChainNetworkTransportPasses 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.
WebSocketTransportTransmits all GraphQL operations via WebSocket. Requires the Apollo/WebSocket sub-spec.
SplitNetworkTransportTransmits 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 WebSocketTransport and SplitNetworkTransport, see Subscriptions.
Downloading a schema
Fetching queries
Edit on GitHubEditForumsDiscord