Skip to content

Commit f6c8707

Browse files
author
Šimon Šesták
committed
feat(GraphQLAPIAdapter): Add variadic generic initializer for network observers
This introduces a new initializer for `GraphQLAPIAdapter` that leverages Swift 5.9's variadic generics, allowing multiple `GraphQLNetworkObserver` instances to be passed directly without being wrapped in an array. This improves API ergonomics and simplifies usage. The `swift-tools-version` has been updated to 5.9 accordingly.
1 parent 8b63e6a commit f6c8707

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

Sources/GraphQLAPIKit/GraphQLAPIAdapter.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,37 @@ public protocol GraphQLAPIAdapterProtocol: AnyObject {
3838
public final class GraphQLAPIAdapter: GraphQLAPIAdapterProtocol {
3939
private let apollo: ApolloClientProtocol
4040

41+
public init<each Observer: GraphQLNetworkObserver>(
42+
url: URL,
43+
urlSessionConfiguration: URLSessionConfiguration = .default,
44+
defaultHeaders: [String: String] = [:],
45+
networkObservers: repeat each Observer
46+
) {
47+
var observers: [any GraphQLNetworkObserver] = []
48+
repeat observers.append(each networkObservers)
49+
50+
let provider = NetworkInterceptorProvider(
51+
client: URLSessionClient(sessionConfiguration: urlSessionConfiguration),
52+
defaultHeaders: defaultHeaders,
53+
networkObservers: observers
54+
)
55+
56+
let networkTransport = RequestChainNetworkTransport(
57+
interceptorProvider: provider,
58+
endpointURL: url
59+
)
60+
61+
self.apollo = ApolloClient(
62+
networkTransport: networkTransport,
63+
store: ApolloStore()
64+
)
65+
}
66+
4167
public init(
4268
url: URL,
4369
urlSessionConfiguration: URLSessionConfiguration = .default,
4470
defaultHeaders: [String: String] = [:],
45-
networkObservers: [any GraphQLNetworkObserver] = []
71+
networkObservers: [any GraphQLNetworkObserver]
4672
) {
4773
let provider = NetworkInterceptorProvider(
4874
client: URLSessionClient(sessionConfiguration: urlSessionConfiguration),

Tests/GraphQLAPIKitTests/GraphQLAPIAdapterIntegrationTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ final class GraphQLAPIAdapterIntegrationTests: XCTestCase {
175175
url: URL(string: "https://api.example.com/graphql")!,
176176
urlSessionConfiguration: mockSessionConfiguration(),
177177
defaultHeaders: defaultHeaders,
178-
networkObservers: [observer]
178+
networkObservers: observer
179179
)
180180

181181
_ = adapter.fetch(query: MockQuery(), context: nil, queue: .main) { _ in
@@ -210,7 +210,7 @@ final class GraphQLAPIAdapterIntegrationTests: XCTestCase {
210210
url: URL(string: "https://api.example.com/graphql")!,
211211
urlSessionConfiguration: mockSessionConfiguration(),
212212
defaultHeaders: [:],
213-
networkObservers: [observer]
213+
networkObservers: observer
214214
)
215215

216216
_ = adapter.fetch(query: MockQuery(), context: contextHeaders, queue: .main) { _ in
@@ -249,7 +249,7 @@ final class GraphQLAPIAdapterIntegrationTests: XCTestCase {
249249
url: URL(string: "https://api.example.com/graphql")!,
250250
urlSessionConfiguration: mockSessionConfiguration(),
251251
defaultHeaders: defaultHeaders,
252-
networkObservers: [observer]
252+
networkObservers: observer
253253
)
254254

255255
_ = adapter.fetch(query: MockQuery(), context: contextHeaders, queue: .main) { _ in
@@ -285,7 +285,7 @@ final class GraphQLAPIAdapterIntegrationTests: XCTestCase {
285285
url: URL(string: "https://api.example.com/graphql")!,
286286
urlSessionConfiguration: mockSessionConfiguration(),
287287
defaultHeaders: defaultHeaders,
288-
networkObservers: [observer1, observer2, observer3]
288+
networkObservers: observer1, observer2, observer3
289289
)
290290

291291
_ = adapter.fetch(query: MockQuery(), context: nil, queue: .main) { _ in
@@ -322,7 +322,7 @@ final class GraphQLAPIAdapterIntegrationTests: XCTestCase {
322322
url: URL(string: "https://api.example.com/graphql")!,
323323
urlSessionConfiguration: mockSessionConfiguration(),
324324
defaultHeaders: [:],
325-
networkObservers: [observer]
325+
networkObservers: observer
326326
)
327327

328328
_ = adapter.fetch(query: MockQuery(), context: nil, queue: .main) { _ in

0 commit comments

Comments
 (0)