Skip to content

[v2] [10/X] Remove RequestContext #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v2-Interceptor-Improvements
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions Tests/ApolloInternalTestHelpers/MockNetworkTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@ public final class MockNetworkTransport: NetworkTransport {
public func send<Query>(
query: Query,
cachePolicy: CachePolicy,
context: (any RequestContext)?
) throws -> AsyncThrowingStream<GraphQLResult<Query.Data>, any Error> where Query: GraphQLQuery {
try requestChainTransport.send(
query: query,
cachePolicy: cachePolicy,
context: context
cachePolicy: cachePolicy
)
}

public func send<Mutation>(
mutation: Mutation,
cachePolicy: CachePolicy,
context: (any RequestContext)?
) throws -> AsyncThrowingStream<GraphQLResult<Mutation.Data>, any Error> where Mutation: GraphQLMutation {
try requestChainTransport.send(
mutation: mutation,
cachePolicy: cachePolicy,
context: context
cachePolicy: cachePolicy
)
}

Expand Down
1 change: 0 additions & 1 deletion Tests/ApolloTests/Network/ApolloURLSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class ApolloURLSessionTests: XCTestCase, MockResponseProvider {
var operation = MockQuery<MockSelectionSet>.mock()
var additionalHeaders: [String: String] = [:]
var cachePolicy: Apollo.CachePolicy = .fetchIgnoringCacheCompletely
var context: (any RequestContext)? = nil
var clientAwarenessMetadata: ClientAwarenessMetadata = .none

var urlRequest: URLRequest
Expand Down
21 changes: 4 additions & 17 deletions apollo-ios/Sources/Apollo/ApolloClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
}

/// The `ApolloClient` class implements the core API for Apollo by conforming to `ApolloClientProtocol`.
public final class ApolloClient: ApolloClientProtocol, Sendable {

Check failure on line 46 in apollo-ios/Sources/Apollo/ApolloClient.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

type 'ApolloClient' does not conform to protocol 'ApolloClientProtocol'

Check failure on line 46 in apollo-ios/Sources/Apollo/ApolloClient.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

type 'ApolloClient' does not conform to protocol 'ApolloClientProtocol'

let networkTransport: any NetworkTransport

Expand Down Expand Up @@ -117,13 +117,11 @@
public func fetch<Query: GraphQLQuery>(
query: Query,
cachePolicy: CachePolicy? = nil,
context: (any RequestContext)? = nil
) async throws -> GraphQLResult<Query.Data>

Check failure on line 120 in apollo-ios/Sources/Apollo/ApolloClient.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

unexpected ',' separator

Check failure on line 120 in apollo-ios/Sources/Apollo/ApolloClient.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

unexpected ',' separator
where Query.ResponseFormat == SingleResponseFormat {
for try await result in try self.networkTransport.send(
query: query,
cachePolicy: cachePolicy ?? self.defaultCachePolicy,
context: context
) {
return result
}
Expand All @@ -132,14 +130,12 @@

public func fetch<Query: GraphQLQuery>(
query: Query,
cachePolicy: CachePolicy? = nil,
context: (any RequestContext)? = nil
cachePolicy: CachePolicy? = nil
) throws -> AsyncThrowingStream<GraphQLResult<Query.Data>, any Error>
where Query.ResponseFormat == IncrementalDeferredResponseFormat {
return try self.networkTransport.send(
query: query,
cachePolicy: cachePolicy ?? self.defaultCachePolicy,
context: context
cachePolicy: cachePolicy ?? self.defaultCachePolicy
)
}

Expand All @@ -158,15 +154,13 @@
query: Query,
cachePolicy: CachePolicy? = nil,
refetchOnFailedUpdates: Bool = true,
context: (any RequestContext)? = nil,
callbackQueue: DispatchQueue = .main,
resultHandler: @escaping GraphQLResultHandler<Query.Data>
) -> GraphQLQueryWatcher<Query> {
let watcher = GraphQLQueryWatcher(
client: self,
query: query,
refetchOnFailedUpdates: refetchOnFailedUpdates,
context: context,
callbackQueue: callbackQueue,
resultHandler: resultHandler
)
Expand All @@ -178,7 +172,6 @@
public func perform<Mutation: GraphQLMutation>(
mutation: Mutation,
publishResultToStore: Bool = true,
context: (any RequestContext)? = nil,
queue: DispatchQueue = .main,
resultHandler: GraphQLResultHandler<Mutation.Data>? = nil
) -> (any Cancellable) {
Expand All @@ -187,7 +180,6 @@
try self.networkTransport.send(
mutation: mutation,
cachePolicy: publishResultToStore ? self.defaultCachePolicy : .networkOnly, // TODO: should be NoCache
context: context
)
},
callbackQueue: queue,
Expand All @@ -199,7 +191,6 @@
public func upload<Operation: GraphQLOperation>(
operation: Operation,
files: [GraphQLFile],
context: (any RequestContext)? = nil,
queue: DispatchQueue = .main,
resultHandler: GraphQLResultHandler<Operation.Data>? = nil
) -> (any Cancellable) {
Expand All @@ -217,8 +208,7 @@
{
try uploadingTransport.upload(
operation: operation,
files: files,
context: context
files: files
)
},
callbackQueue: queue,
Expand All @@ -228,7 +218,6 @@

public func subscribe<Subscription: GraphQLSubscription>(
subscription: Subscription,
context: (any RequestContext)? = nil,
queue: DispatchQueue = .main,
resultHandler: @escaping GraphQLResultHandler<Subscription.Data>
) -> any Cancellable {
Expand All @@ -247,7 +236,6 @@
try networkTransport.send(
subscription: subscription,
cachePolicy: self.defaultCachePolicy, // TODO: should this just be networkOnly?
context: context
)
},
callbackQueue: queue,
Expand Down Expand Up @@ -280,8 +268,7 @@
{
try self.networkTransport.send(
query: query,
cachePolicy: cachePolicy ?? self.defaultCachePolicy,
context: context
cachePolicy: cachePolicy ?? self.defaultCachePolicy
)
},
callbackQueue: queue,
Expand Down
5 changes: 0 additions & 5 deletions apollo-ios/Sources/Apollo/ApolloClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public protocol ApolloClientProtocol: AnyObject {
/// - Returns: An object that can be used to cancel an in progress fetch.
func fetch<Query: GraphQLQuery>(query: Query,
cachePolicy: CachePolicy?,
context: (any RequestContext)?,
queue: DispatchQueue,
resultHandler: GraphQLResultHandler<Query.Data>?) -> (any Cancellable)

Expand All @@ -43,7 +42,6 @@ public protocol ApolloClientProtocol: AnyObject {
/// - Returns: A query watcher object that can be used to control the watching behavior.
func watch<Query: GraphQLQuery>(query: Query,
cachePolicy: CachePolicy?,
context: (any RequestContext)?,
callbackQueue: DispatchQueue,
resultHandler: @escaping GraphQLResultHandler<Query.Data>) -> GraphQLQueryWatcher<Query>

Expand All @@ -58,7 +56,6 @@ public protocol ApolloClientProtocol: AnyObject {
/// - Returns: An object that can be used to cancel an in progress mutation.
func perform<Mutation: GraphQLMutation>(mutation: Mutation,
publishResultToStore: Bool,
context: (any RequestContext)?,
queue: DispatchQueue,
resultHandler: GraphQLResultHandler<Mutation.Data>?) -> (any Cancellable)

Expand All @@ -73,7 +70,6 @@ public protocol ApolloClientProtocol: AnyObject {
/// - Returns: An object that can be used to cancel an in progress request.
func upload<Operation: GraphQLOperation>(operation: Operation,
files: [GraphQLFile],
context: (any RequestContext)?,
queue: DispatchQueue,
resultHandler: GraphQLResultHandler<Operation.Data>?) -> (any Cancellable)

Expand All @@ -87,7 +83,6 @@ public protocol ApolloClientProtocol: AnyObject {
/// - resultHandler: An optional closure that is called when mutation results are available or when an error occurs.
/// - Returns: An object that can be used to cancel an in progress subscription.
func subscribe<Subscription: GraphQLSubscription>(subscription: Subscription,
context: (any RequestContext)?,
queue: DispatchQueue,
resultHandler: @escaping GraphQLResultHandler<Subscription.Data>) -> any Cancellable
}
60 changes: 40 additions & 20 deletions apollo-ios/Sources/Apollo/GraphQLQueryWatcher.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation

#if !COCOAPODS
import ApolloAPI
import ApolloAPI
#endif

/// A `GraphQLQueryWatcher` is responsible for watching the store, and calling the result handler with a new result whenever any of the data the previous result depends on changes.
Expand All @@ -23,7 +24,6 @@
private let callbackQueue: DispatchQueue

private let contextIdentifier = UUID()
private let context: (any RequestContext)?

private actor WeakFetchTaskContainer {
weak var cancellable: (any Cancellable)?
Expand Down Expand Up @@ -52,24 +52,24 @@
/// - context: [optional] A context that is being passed through the request chain. Defaults to `nil`.
/// - callbackQueue: The queue for the result handler. Defaults to the main queue.
/// - resultHandler: The result handler to call with changes.
public init(client: any ApolloClientProtocol,
query: Query,
refetchOnFailedUpdates: Bool = true,
context: (any RequestContext)? = nil,
callbackQueue: DispatchQueue = .main,
resultHandler: @escaping GraphQLResultHandler<Query.Data>) {
public init(
client: any ApolloClientProtocol,
query: Query,
refetchOnFailedUpdates: Bool = true,
callbackQueue: DispatchQueue = .main,
resultHandler: @escaping GraphQLResultHandler<Query.Data>
) {
self.client = client
self.query = query
self.refetchOnFailedUpdates = refetchOnFailedUpdates
self.resultHandler = resultHandler
self.callbackQueue = callbackQueue
self.context = context

client.store.subscribe(self)
}

/// Refetch a query from the server.
public func refetch(cachePolicy: CachePolicy = .fetchIgnoringCacheData) {

Check failure on line 72 in apollo-ios/Sources/Apollo/GraphQLQueryWatcher.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

type 'CachePolicy' has no member 'fetchIgnoringCacheData'

Check failure on line 72 in apollo-ios/Sources/Apollo/GraphQLQueryWatcher.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

type 'CachePolicy' has no member 'fetchIgnoringCacheData'
fetch(cachePolicy: cachePolicy)
}

Expand Down Expand Up @@ -120,13 +120,13 @@
_ store: ApolloStore,
didChangeKeys changedKeys: Set<CacheKey>
) {
if
let incomingIdentifier = QueryWatcherContext.identifier,
incomingIdentifier == self.contextIdentifier {
// This is from changes to the keys made from the `fetch` method above,
// changes will be returned through that and do not need to be returned
// here as well.
return
if let incomingIdentifier = QueryWatcherContext.identifier,
incomingIdentifier == self.contextIdentifier
{
// This is from changes to the keys made from the `fetch` method above,
// changes will be returned through that and do not need to be returned
// here as well.
return
}

Task { [store] in
Expand All @@ -147,7 +147,7 @@
return
}

#warning("TODO: temp Task encapsulation to move forward; this probably is not right?")
#warning("TODO: temp Task encapsulation to move forward; this probably is not right?")
Task {
await self.fetching.mutate {
$0.dependentKeys = graphQLResult.dependentKeys
Expand All @@ -157,7 +157,7 @@
}

case .failure:
#warning("TODO: temp Task encapsulation to move forward; this probably is not right?")
#warning("TODO: temp Task encapsulation to move forward; this probably is not right?")
Task {
let cachePolicy = await fetching.cachePolicy
if self.refetchOnFailedUpdates && cachePolicy != .returnCacheDataDontFetch {
Expand All @@ -175,6 +175,26 @@
// MARK: - Task Local Values

private enum QueryWatcherContext {
@TaskLocal
static var identifier: UUID?
@TaskLocal static var identifier: UUID?
}

// MARK: - Deprecation

extension GraphQLQueryWatcher {
@available(*, deprecated)
public convenience init(
client: any ApolloClientProtocol,
query: Query,
refetchOnFailedUpdates: Bool = true,
context: (any RequestContext)? = nil,
callbackQueue: DispatchQueue = .main,
resultHandler: @escaping GraphQLResultHandler<Query.Data>
) {
self.init(
client: client,
query: query,
refetchOnFailedUpdates: refetchOnFailedUpdates,
resultHandler: resultHandler
)
}
}
3 changes: 0 additions & 3 deletions apollo-ios/Sources/Apollo/GraphQLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ public protocol GraphQLRequest<Operation>: Sendable {
/// The `FetchBehavior` to use for this request. Determines if fetching will include cache/network.
var fetchBehavior: FetchBehavior { get set }

/// [optional] A context that is being passed through the request chain.
var context: (any RequestContext)? { get set }

/// The telemetry metadata about the client. This is used by GraphOS Studio's
/// [client awareness](https://www.apollographql.com/docs/graphos/platform/insights/client-segmentation)
/// feature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@
typealias NextHTTPInterceptorFunction = @Sendable (URLRequest) async throws -> HTTPResponse

func intercept(
request: URLRequest,
context: (any RequestContext)?,
request: URLRequest,
next: NextHTTPInterceptorFunction
) async throws -> HTTPResponse

Expand All @@ -75,7 +74,7 @@
self.stream = stream
}

init<S: AsyncSequence & Sendable>(stream wrapped: sending S) where S.Element == T {

Check failure on line 77 in apollo-ios/Sources/Apollo/Interceptors/ApolloInterceptor.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

cannot find type 'sending' in scope

Check failure on line 77 in apollo-ios/Sources/Apollo/Interceptors/ApolloInterceptor.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

unnamed parameters must be written with the empty name '_'

Check failure on line 77 in apollo-ios/Sources/Apollo/Interceptors/ApolloInterceptor.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

expected ',' separator
self.stream = AsyncThrowingStream { continuation in
let task = Task { [wrapped] in
do {
Expand Down Expand Up @@ -192,7 +191,7 @@
extension TaskLocal {

@_disfavoredOverload
final public func withValue<R: ~Copyable>(

Check failure on line 194 in apollo-ios/Sources/Apollo/Interceptors/ApolloInterceptor.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

cannot find type 'Copyable' in scope

Check failure on line 194 in apollo-ios/Sources/Apollo/Interceptors/ApolloInterceptor.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

cannot suppress conformances here
_ valueDuringOperation: Value,
operation: () async throws -> R
) async rethrows -> R {
Expand All @@ -206,7 +205,7 @@
}

@_disfavoredOverload
final public func withValue<R: ~Copyable>(

Check failure on line 208 in apollo-ios/Sources/Apollo/Interceptors/ApolloInterceptor.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

cannot find type 'Copyable' in scope

Check failure on line 208 in apollo-ios/Sources/Apollo/Interceptors/ApolloInterceptor.swift

View workflow job for this annotation

GitHub Actions / Apollo Unit Tests - macOS

cannot suppress conformances here
_ valueDuringOperation: Value,
operation: () throws -> R
) rethrows -> R {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public struct ResponseCodeInterceptor: HTTPInterceptor {

public func intercept(
request: URLRequest,
context: (any RequestContext)?,
next: NextHTTPInterceptorFunction
) async throws -> HTTPResponse {
return try await next(request).mapChunks { (response, chunk) in
Expand Down
5 changes: 0 additions & 5 deletions apollo-ios/Sources/Apollo/JSONRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public struct JSONRequest<Operation: GraphQLOperation>: GraphQLRequest, AutoPers

/// The `FetchBehavior` to use for this request. Determines if fetching will include cache/network.
public var fetchBehavior: FetchBehavior

/// [optional] A context that is being passed through the request chain.
public var context: (any RequestContext)?

public let requestBodyCreator: any JSONRequestBodyCreator

Expand Down Expand Up @@ -48,7 +45,6 @@ public struct JSONRequest<Operation: GraphQLOperation>: GraphQLRequest, AutoPers
/// - clientName: The name of the client to send with the `"apollographql-client-name"` header
/// - clientVersion: The version of the client to send with the `"apollographql-client-version"` header
/// - cachePolicy: The `CachePolicy` to use for this request.
/// - context: [optional] A context that is being passed through the request chain. Defaults to `nil`.
/// - apqConfig: A configuration struct used by a `GraphQLRequest` to configure the usage of
/// [Automatic Persisted Queries (APQs).](https://www.apollographql.com/docs/apollo-server/performance/apq) By default, APQs
/// are disabled.
Expand All @@ -58,7 +54,6 @@ public struct JSONRequest<Operation: GraphQLOperation>: GraphQLRequest, AutoPers
operation: Operation,
graphQLEndpoint: URL,
fetchBehavior: FetchBehavior,
context: (any RequestContext)? = nil,
apqConfig: AutoPersistedQueryConfiguration = .init(),
useGETForQueries: Bool = false,
requestBodyCreator: any JSONRequestBodyCreator = DefaultRequestBodyCreator(),
Expand Down
12 changes: 4 additions & 8 deletions apollo-ios/Sources/Apollo/NetworkTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ public protocol NetworkTransport: AnyObject, Sendable {
/// - Returns: A stream of `GraphQLResult`s for each response.
func send<Query: GraphQLQuery>(
query: Query,
cachePolicy: CachePolicy,
context: (any RequestContext)?
cachePolicy: CachePolicy
) throws -> AsyncThrowingStream<GraphQLResult<Query.Data>, any Error>

func send<Mutation: GraphQLMutation>(
mutation: Mutation,
cachePolicy: CachePolicy,
context: (any RequestContext)?
cachePolicy: CachePolicy
) throws -> AsyncThrowingStream<GraphQLResult<Mutation.Data>, any Error>

}
Expand All @@ -33,8 +31,7 @@ public protocol SubscriptionNetworkTransport: NetworkTransport {

func send<Subscription: GraphQLSubscription>(
subscription: Subscription,
cachePolicy: CachePolicy,
context: (any RequestContext)?
cachePolicy: CachePolicy
) throws -> AsyncThrowingStream<GraphQLResult<Subscription.Data>, any Error>

}
Expand All @@ -54,7 +51,6 @@ public protocol UploadingNetworkTransport: NetworkTransport {
#warning("TODO: should support query and mutation as seperate functions")
func upload<Operation: GraphQLOperation>(
operation: Operation,
files: [GraphQLFile],
context: (any RequestContext)?
files: [GraphQLFile]
) throws -> AsyncThrowingStream<GraphQLResult<Operation.Data>, any Error>
}
Loading
Loading