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

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)

provides the following classes that conform to the

:

ClassDescription
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
    .
Previous
Downloading a schema
Next
Fetching queries
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company