Skip to content

Commit

Permalink
moar docs!
Browse files Browse the repository at this point in the history
  • Loading branch information
designatednerd committed Aug 4, 2020
1 parent e246d86 commit 6172015
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
41 changes: 40 additions & 1 deletion Sources/Apollo/HTTPRequest.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
import Foundation

/// Encapsulation of all information about a request before it hits the network
open class HTTPRequest<Operation: GraphQLOperation> {

public enum HTTPRequestError: Error {
case noRequestConstructed
}

/// The endpoint to make a GraphQL request to
open var graphQLEndpoint: URL

/// The GraphQL Operation to execute
open var operation: Operation

/// The `Content-Type` header's value
open var contentType: String

/// Any additional headers you wish to add by default to this request
open var additionalHeaders: [String: String]

/// [optional] The name of the current client, defaults to nil
open var clientName: String? = nil

/// [optional] The version of the current client, defaults to nil
open var clientVersion: String? = nil

/// How many times this request has been retried. Must be incremented manually. Defaults to zero.
open var retryCount: Int = 0

/// The `CachePolicy` to use for this request.
public let cachePolicy: CachePolicy


/// Designated Initializer
///
/// - Parameters:
/// - graphQLEndpoint: The endpoint to make a GraphQL request to
/// - operation: The GraphQL Operation to execute
/// - contentType: The `Content-Type` header's value. Should usually be set for you by a subclass.
/// - additionalHeaders: Any additional headers you wish to add by default to this request.
/// - cachePolicy: The `CachePolicy` to use for this request. Defaults to the `.default` policy
public init(graphQLEndpoint: URL,
operation: Operation,
contentType: String,
Expand Down Expand Up @@ -61,6 +84,10 @@ open class HTTPRequest<Operation: GraphQLOperation> {
self.additionalHeaders[name] = value
}

/// Converts this object to a fully fleshed-out `URLRequest`
///
/// - Throws: Any error in creating the request
/// - Returns: The URL request, ready to send to your server.
open func toURLRequest() throws -> URLRequest {
var request = URLRequest(url: self.graphQLEndpoint)

Expand All @@ -80,3 +107,15 @@ open class HTTPRequest<Operation: GraphQLOperation> {
}
}

extension HTTPRequest: Equatable {

public static func == (lhs: HTTPRequest<Operation>, rhs: HTTPRequest<Operation>) -> Bool {
lhs.graphQLEndpoint == rhs.graphQLEndpoint
&& lhs.additionalHeaders == rhs.additionalHeaders
&& lhs.cachePolicy == rhs.cachePolicy
&& lhs.contentType == rhs.contentType
&& lhs.operation.queryDocument == rhs.operation.queryDocument
&& lhs.clientName == rhs.clientName
&& lhs.clientVersion == rhs.clientVersion
}
}
13 changes: 12 additions & 1 deletion Sources/Apollo/JSONRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ public class JSONRequest<Operation: GraphQLOperation>: HTTPRequest<Operation> {
public var isPersistedQueryRetry = false

public let serializationFormat = JSONSerializationFormat.self


/// Designated initializer
///
/// - Parameters:
/// - operation: The GraphQL Operation to execute
/// - graphQLEndpoint: The endpoint to make a GraphQL request to
/// - additionalHeaders: Any additional headers you wish to add by default to this request
/// - cachePolicy: The `CachePolicy` to use for this request.
/// - autoPersistQueries: `true` if Auto-Persisted Queries should be used. Defaults to `false`.
/// - useGETForQueries: `true` if Queries should use `GET` instead of `POST` for HTTP requests. Defaults to `false`.
/// - useGETForPersistedQueryRetry: `true` if when an Auto-Persisted query is retried, it should use `GET` instead of `POST` to send the query. Defaults to `false`.
/// - requestCreator: An object conforming to the `RequestCreator` protocol to assist with creating the request body. Defaults to the provided `ApolloRequestCreator` implementation.
public init(operation: Operation,
graphQLEndpoint: URL,
additionalHeaders: [String: String] = [:],
Expand Down
4 changes: 3 additions & 1 deletion Tests/ApolloTests/RequestChainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class RequestChainTests: XCTestCase {
let url = URL(string: "http://localhost:8080/graphql")!

let store = ApolloStore(cache: InMemoryNormalizedCache())
let transport = RequestChainNetworkTransport(interceptorProvider: LegacyInterceptorProvider(store: store), endpointURL: url)
let provider = LegacyInterceptorProvider(store: store)
let transport = RequestChainNetworkTransport(interceptorProvider: provider,
endpointURL: url)

return ApolloClient(networkTransport: transport)
}()
Expand Down

0 comments on commit 6172015

Please sign in to comment.