Creating a client
Before you can execute GraphQL operations in your app, you need to initialize an ApolloClient
instance.
Basic client creation
For a basic client with a default configuration, you can initialize ApolloClient
just by providing the URL
to your GraphQL server.
1let client = ApolloClient(url: URL(string: "http://localhost:4000/graphql")!)
The default client configuration uses:
An
InMemoryNormalizedCache
for caching response data in-memory.A
RequestChainNetworkTransport
configured to communicate with a spec-compliant GraphQL server over HTTP.A
DefaultInterceptorProvider
that supports:Reading/writing response data to the normalized cache.
Sending network requests using
URLSession
.Parsing GraphQL response data in JSON format
For more information on the in-memory normalized cache, see its documentation in Cache Setup.
To learn more about the request chain or interceptor providers, check out our Advanced networking configuration documentation.
Custom client creation
If you need to customize your networking client configuration use the designated initializer.
ApolloClient
is initialized with a NetworkTransport
object and an ApolloStore
.
1public init(
2 networkTransport: NetworkTransport,
3 store: ApolloStore
4)
Network Transport
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 ApolloWebSocket library. |
SplitNetworkTransport | Transmits subscription operations via WebSocket and other operations via HTTP. Requires the ApolloWebSocket library. |
For more information on
RequestChainNetworkTransport
, learn about Advanced networking configuration.For more information on
WebSocketTransport
andSplitNetworkTransport
, see Enabling subscription support.
Apollo Store
The ApolloStore
is the cache used for GraphQL response data. The default cache stores response data in-memory. This data is not persisted between application runs.
To persist cache data on-disk or configure the cache further, you can provide a custom ApolloStore
.
Learn more about customizing your cache with our Caching documentation.