Skip to content

Commit 46581cd

Browse files
Tell devs to make a real singleton instead of glomming it onto the App Delegate for the basic setup
1 parent 849c5be commit 46581cd

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

docs/source/initialization.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22
title: Creating a client
33
---
44

5-
In most cases, you'll want to create a single shared instance of `ApolloClient` and point it at your GraphQL server. The easiest way to do this is to define a global variable in `AppDelegate.swift`:
5+
## Basic Client Creation
6+
7+
In most cases, you'll want to create a single shared instance of `ApolloClient` and point it at your GraphQL server. The easiest way to do this is to create a singleton:
68

79
```swift
8-
let apollo = ApolloClient(url: URL(string: "http://localhost:8080/graphql")!)
10+
class Apollo {
11+
static let shared = Apollo()
12+
13+
private(set) lazy var client = ApolloClient(url: URL(string: "http://localhost:8080/graphql")!)
14+
}
915
```
1016

11-
## Adding additional headers
17+
Under the hood, this will create a client using `HTTPNetworkTransport` with a default configuration. You can then use this client from anywhere in your code with `Apollo.shared.client`.
18+
19+
## Advanced Client Creation
1220

1321
If you need to add additional headers to requests, to include authentication details for example, you can create your own `URLSessionConfiguration` and use this to configure an `HTTPNetworkTransport`. If you want to define the client as a global variable, you can use an immediately invoked closure here:
1422

0 commit comments

Comments
 (0)