You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/tutorial/tutorial-mutations.md
+64-41Lines changed: 64 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,72 +8,95 @@ In this section, you'll learn how to build authenticated mutations and handle in
8
8
9
9
Before you can book a trip, you need to be able to pass your authentication token along to the example server. To do that, let's dig a little deeper into how Apollo Client works.
10
10
11
-
The `ApolloClient` uses something called a `NetworkTransport` under the hood. By default, the client creates an `HTTPNetworkTransport` instance to handle talking over HTTP to your server.
11
+
The `ApolloClient` uses something called a `NetworkTransport` under the hood. By default, the client creates a `RequestChainNetworkTransport` instance to handle talking over HTTP to your server.
12
12
13
-
If you need to do anything before a request hits the wire but after Apollo has done most of the configuration for you, there's a delegate protocol called `HTTPNetworkTransportPreflightDelegate` that allows you to do that.
13
+
A `RequestChain` runs your request through an array of `ApolloInterceptor` objects which can mutate the request and/or check the cache before it hits the network, and then do additional work after a response is received from the network.
14
14
15
-
Open `Network.swift` and add an extension to conform to that delegate:
15
+
The `RequestChainNetworkTransport` uses an object that conforms to the `InterceptorProivder` protocol in order to create that array of interceptors for each operation it executes. There are a couple of providers that are set up by default, which return a fairly standard array of interceptors.
The nice thing is that you can also add your own interceptors to the chain anywhere you need to perform custom actions. In this case, you want to have an interceptor that will add
19
18
20
-
}
21
-
```
19
+
First, create the new interceptor. Go to **File > New > File...** and create a new **Swift File**. Name it **TokenAddingInterceptor.swift**. Open that file, and add the
22
20
23
-
You'll get an error telling you that protocol stubs must be implemented, and asking you if you want to fix this. Click **Fix**.
21
+
```swift:title=TokenAddingInterceptor.swift
22
+
importFoundation
23
+
importApollo
24
24
25
-
<imgsrc="images/preflight_delegate_add_protocol_stubs.png"class="screenshot"alt="Do you wish to add protocol stubs with fix button"/>
Two protocol methods will be added: `networkTransport(_:shouldSend:)` and `networkTransport(_:willSend:)`.
37
+
Next, import `KeychainSwift` at the top of the file so you can access the key you've just stored in the keychain.
28
38
29
-
The `shouldSend` method enables you to make sure a request should go out to the network at all. This is useful for things like checking that your user is logged in before trying to make a request.
39
+
```swift:title=TokenAddingInterceptor.swift
40
+
importKeychainSwift
41
+
```
30
42
31
-
However, you're not going to use that functionality in this application. Update the method to have it return `true` all the time:
43
+
Then, replace the `TODO` within the `interceptAsync` method with code to get the token from the keychain, and add it to your headers if it exists:
The `willSend` request is the last thing that can manipulate the request before it goes out to the network. Because the request is passed as an `inout` variable, you can manipulate its contents directly.
41
-
42
-
Update the `willSend` method to add your token as the value for the `Authorization` header:
56
+
Next, since you're only adding one interceptor that can run at the very beginning of other interceptors, you can subclass the existing `LegacyInterceptorProvider` (which is the default interceptor provider).
Go to **File > New > File...** and create a new **Swift File**. Name it **NetworkInterceptorProvider.swift**. Add an initial Add code which inserts your `TokenAddingInterceptor` before the other interceptors provided by the `LegacyInterceptorProvider`:
53
59
54
-
Then, import `KeychainSwift` at the top of the file:
Next, you need to make sure that Apollo knows that this delegate exists. To do that, you need to do something that Apollo Client has thus far been doing for you under the hood: instantiating the `HTTPNetworkTransport`.
73
+
> Another way to do this would be to copy the interceptors provided by the `LegacyInterceptorProvider` (which are all public), and then place your interceptors in the points in the array where you want them. However, since in this case we can run this interceptor first, it's just as simple to subclass.
61
74
62
-
In the primary declaration of`Network`, update your `lazy var` to create this transport and set the `Network` object as its delegate, then pass it through to the `ApolloClient`:
75
+
Next, go back to your`Network` class. Replace the `ApolloClient` with an updated `lazy var` which creates the `RequestChainNetworkTransport` manually, using your custom interceptor provider:
63
76
64
77
```swift:title=Network.swift
65
-
private(set) lazyvar apollo: ApolloClient = {
66
-
let httpNetworkTransport =HTTPNetworkTransport(url: URL(string: "https://apollo-fullstack-tutorial.herokuapp.com/")!)
let provider =NetworkInterceptorProvider(client: client, store: store)
86
+
let url =URL(string: "https://apollo-fullstack-tutorial.herokuapp.com/")!
87
+
let transport =RequestChainNetworkTransport(interceptorProvider: provider,
88
+
endpointURL: url)
89
+
returnApolloClient(networkTransport: transport)
90
+
}()
91
+
}
70
92
```
71
93
94
+
Now, go back to **TokenAddingInterceptor.swift**.
72
95
Click on the line numbers to add a breakpoint at the line where you're instantiating the `Keychain`:
73
96
74
-
<imgalt="adding a breakpoint"class="screenshot"src="images/preflight_delegate_breakpoint.png"/>
97
+
<imgalt="adding a breakpoint"class="screenshot"src="images/interceptor_breakpoint.png"/>
75
98
76
-
Build and run the application. Whenever a network request goes out, that breakpoint should now get hit. If you're logged in, your token will be sent to the server whenever you make a request.
99
+
Build and run the application. Whenever a network request goes out, that breakpoint should now get hit. If you're logged in, your token will be sent to the server whenever you make a request!
0 commit comments