Skip to content

Commit

Permalink
docs: Updates sample code for Advanced Network Configuration document…
Browse files Browse the repository at this point in the history
  • Loading branch information
calvincestari authored Sep 15, 2023
1 parent 6e3bc47 commit f79e659
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions docs/source/networking/request-pipeline.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,16 @@ The following example snippets demonstrate how to use an advanced request pipeli
This example interceptor checks whether the active user is logged in. If so, it asynchronously renews that user's access token if it's expired. Finally, it adds the access token to an `Authorization` header before proceeding to the next interceptor in the request chain.

```swift
import Foundation
import Apollo

class UserManagementInterceptor: ApolloInterceptor {

enum UserError: Error {
case noUserLoggedIn
}

public var id: String = UUID().uuidString

/// Helper function to add the token then move on to the next step
private func addTokenAndProceed<Operation: GraphQLOperation>(
_ token: Token,
Expand All @@ -311,9 +313,12 @@ class UserManagementInterceptor: ApolloInterceptor {
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void
) {
request.addHeader(name: "Authorization", value: "Bearer \(token.value)")
chain.proceedAsync(request: request,
response: response,
completion: completion)
chain.proceedAsync(
request: request,
response: response,
interceptor: self,
completion: completion
)
}

func interceptAsync<Operation: GraphQLOperation>(
Expand Down Expand Up @@ -384,10 +389,13 @@ class UserManagementInterceptor: ApolloInterceptor {
This example interceptor logs the outgoing request using the hypothetical `Logger` class, then proceeds to the next interceptor in the request chain:

```swift
import Foundation
import Apollo

class RequestLoggingInterceptor: ApolloInterceptor {

public var id: String = UUID().uuidString

func interceptAsync<Operation: GraphQLOperation>(
chain: RequestChain,
request: HTTPRequest<Operation>,
Expand All @@ -398,6 +406,7 @@ class RequestLoggingInterceptor: ApolloInterceptor {
chain.proceedAsync(
request: request,
response: response,
interceptor: self,
completion: completion
)
}
Expand All @@ -411,6 +420,7 @@ This example interceptor uses the hypothetical `Logger` class to log the request
This is an example of an interceptor that can both proceed _and_ throw an error. We don't necessarily want to stop processing if this interceptor was added in wrong place, but we _do_ want to know about that error.

```swift
import Foundation
import Apollo

class ResponseLoggingInterceptor: ApolloInterceptor {
Expand All @@ -419,6 +429,8 @@ class ResponseLoggingInterceptor: ApolloInterceptor {
case notYetReceived
}

public var id: String = UUID().uuidString

func interceptAsync<Operation: GraphQLOperation>(
chain: RequestChain,
request: HTTPRequest<Operation>,
Expand All @@ -430,6 +442,7 @@ class ResponseLoggingInterceptor: ApolloInterceptor {
chain.proceedAsync(
request: request,
response: response,
interceptor: self,
completion: completion
)
}
Expand Down

0 comments on commit f79e659

Please sign in to comment.