Skip to content

Commit 899d297

Browse files
calvincestarigh-action-runner
authored and
gh-action-runner
committed
feature: Configure URLRequest timeout interval (apollographql/apollo-ios-dev#618)
1 parent 5c98f56 commit 899d297

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Sources/Apollo/HTTPRequest.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,14 @@ open class HTTPRequest<Operation: GraphQLOperation>: Hashable {
8585
/// - Throws: Any error in creating the request
8686
/// - Returns: The URL request, ready to send to your server.
8787
open func toURLRequest() throws -> URLRequest {
88-
var request = URLRequest(url: self.graphQLEndpoint)
89-
88+
var request: URLRequest
89+
90+
if let configContext = context as? any RequestContextTimeoutConfigurable {
91+
request = URLRequest(url: self.graphQLEndpoint, timeoutInterval: configContext.requestTimeout)
92+
} else {
93+
request = URLRequest(url: self.graphQLEndpoint)
94+
}
95+
9096
for (fieldName, value) in additionalHeaders {
9197
request.addValue(value, forHTTPHeaderField: fieldName)
9298
}

Sources/Apollo/RequestContext.swift

+12
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ import ApolloAPI
1010
/// This allows the various interceptors to make modifications, or perform actions, with information
1111
/// that they cannot get just from the existing operation. It can be anything that conforms to this protocol.
1212
public protocol RequestContext {}
13+
14+
/// A request context specialization protocol that specifies options for configuring the timeout of a `URLRequest`.
15+
///
16+
/// A `RequestContext` object can conform to this protocol to provide a custom `requestTimeout` for an individual request.
17+
/// If the `RequestContext` for a request does not conform to this protocol, the default request timeout of `URLRequest` will be used.
18+
public protocol RequestContextTimeoutConfigurable: RequestContext {
19+
/// The timeout interval specifies the limit on the idle interval allotted to a request in the process of
20+
/// loading. This timeout interval is measured in seconds.
21+
///
22+
/// The value of this property will be set as the `timeoutInterval` on the `URLRequest` created for this GraphQL request.
23+
var requestTimeout: TimeInterval { get }
24+
}

0 commit comments

Comments
 (0)