Skip to content
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
13 changes: 2 additions & 11 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/apollographql/apollo-ios.git",
exact: "1.17.0" // Do not forget to download related to this version Apollo CLI and include it with package
exact: "2.0.4"
)
],
targets: [
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Developed to simplify [Futured](https://www.futured.app) in-house development of

- iOS 16.0+ / macOS 13.0+
- Swift 5.9+
- Apollo iOS 1.17.0
- Apollo iOS 2.0.4

## Limitations

Expand Down Expand Up @@ -51,7 +51,7 @@ Copy and paste json configuration to the newly created file:
"schemaTypes" : {
"path" : "./",
"moduleType" : {
"swiftPackageManager": {}
"swiftPackage": {}
}
},
"operations" : {
Expand All @@ -74,7 +74,7 @@ Add `Queries` and `Mutations` folders to `GraphQLGenerated` folder.
#### 5. Define Your first GraphQL Query Or Mutation
Add your first Query or Mutation and save it with `.graphql` extension to `Queries` or `Mutations` folders.

#### 6. Add Xcode Biuld Phase Script
#### 6. Add Xcode Build Phase Script
At your main app's target add a new build phase named `Generate GraphQL Operations`.
Move your newly created build phase above the `Compile Sources` phase.
Add script:
Expand Down Expand Up @@ -120,11 +120,12 @@ let mutation = MyExampleMutation()
import GraphQLAPIKit
import GraphQLGenerated

let apiAdapter = GraphQLAPIAdapter(
let configuration = GraphQLAPIConfiguration(
url: URL(string: "https://api.example.com/graphql")!
)
let queryResult = await apiAdapter.fetch(query: query)
let mutationResult = await apiAdapter.perform(mutation: mutation)
let apiAdapter = GraphQLAPIAdapter(configuration: configuration)
let queryResult = try await apiAdapter.fetch(query: query)
let mutationResult = try await apiAdapter.perform(mutation: mutation)
```

## Contributors
Expand Down
Binary file modified Resources/apollo-ios-cli
Binary file not shown.
38 changes: 38 additions & 0 deletions Sources/GraphQLAPIKit/Configuration/GraphQLAPIConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation

/// Configuration for initializing a GraphQL API adapter.
///
/// Use this struct to configure the GraphQL client with endpoint URL,
/// session configuration, default headers, and network observers.
public struct GraphQLAPIConfiguration: Sendable {
/// The GraphQL endpoint URL.
public let url: URL

/// URL session configuration. Defaults to `.default`.
public let urlSessionConfiguration: URLSessionConfiguration

/// Headers to include in every request.
public let defaultHeaders: [String: String]

/// Network observers for monitoring requests (logging, analytics, etc.).
public let networkObservers: [any GraphQLNetworkObserver]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nakonec ses rozhodl neimplementovat tu zmenu?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V predchádzajúcej verzii to je naimplementované ale skrz to že som pridal teraz "abstrakciu" skrz pomocné štruktúry na konfiguráciu aby sme mohli do budúcna jednoduchšie rozširovať konštruktor alebo pridať support napríklad additional parametrov vrámci volaní. Tak variadické generiká stratili význam nakoľko nemáme ešte možnosť (minimálne o nej neviem) ako uložiť heterogénne pole 😞


/// Creates a new GraphQL API configuration.
///
/// - Parameters:
/// - url: The GraphQL endpoint URL.
/// - urlSessionConfiguration: URL session configuration. Defaults to `.default`.
/// - defaultHeaders: Headers to include in every request. Defaults to empty.
/// - networkObservers: Network observers for monitoring requests. Defaults to empty.
public init(
url: URL,
urlSessionConfiguration: URLSessionConfiguration = .default,
defaultHeaders: [String: String] = [:],
networkObservers: [any GraphQLNetworkObserver] = []
) {
self.url = url
self.urlSessionConfiguration = urlSessionConfiguration
self.defaultHeaders = defaultHeaders
self.networkObservers = networkObservers
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation

/// Base protocol for GraphQL operation configurations.
///
/// Defines common options shared across all GraphQL operations (queries, mutations, subscriptions).
public protocol GraphQLOperationConfiguration: Sendable {
/// Additional headers to add to the request.
var headers: RequestHeaders? { get }
}

/// Configuration for GraphQL queries and mutations.
///
/// Use this struct to customize request-specific options like additional headers.
public struct GraphQLRequestConfiguration: GraphQLOperationConfiguration {
/// Additional headers to add to the request.
public let headers: RequestHeaders?

/// Creates a new request configuration.
///
/// - Parameter headers: Additional headers to add to the request. Defaults to `nil`.
public init(headers: RequestHeaders? = nil) {
self.headers = headers
}
}
2 changes: 1 addition & 1 deletion Sources/GraphQLAPIKit/Errors/ApolloError.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Apollo

struct ApolloError: Error {
struct ApolloError: Error, Sendable {
let errors: [Apollo.GraphQLError]
}
38 changes: 26 additions & 12 deletions Sources/GraphQLAPIKit/Errors/GraphQLAPIAdapterError.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Apollo
import Foundation

public enum GraphQLAPIAdapterError: LocalizedError {
/// Network error received by Apollo from `URLSessionTaskDelegate`
public enum GraphQLAPIAdapterError: LocalizedError, Sendable {
/// Network error with HTTP status code
case network(code: Int, error: Error)

/// The app is offline or doesn't have access to the network.
case connection(Error)

/// Unhandled network error received from `Apollo.URLSessionClient`
/// Unhandled error
case unhandled(Error)

/// Request was cancelled
Expand All @@ -18,22 +18,36 @@ public enum GraphQLAPIAdapterError: LocalizedError {
/// Errors returned by GraphQL API as part of `errors` field
case graphQl([GraphQLError])


init(error: Error) {
if let error = error as? GraphQLAPIAdapterError {
self = error
} else if let error = error as? ApolloError {
self = .graphQl(error.errors.map(GraphQLError.init))
} else if let error = error as? URLSessionClient.URLSessionClientError,
case let URLSessionClient.URLSessionClientError.networkError(_, response, underlyingError) = error
{
if let response = response {
self = .network(code: response.statusCode, error: underlyingError)
} else {
self = .connection(underlyingError)
} else if error is CancellationError {
self = .cancelled
} else if let urlError = error as? URLError {
switch urlError.code {
case .cancelled:
self = .cancelled
case .notConnectedToInternet, .networkConnectionLost, .dataNotAllowed:
self = .connection(urlError)
default:
self = .unhandled(urlError)
}
} else {
self = .unhandled(error)
let nsError = error as NSError
if nsError.domain == NSURLErrorDomain {
if nsError.code == NSURLErrorCancelled {
self = .cancelled
} else if nsError.code == NSURLErrorNotConnectedToInternet ||
nsError.code == NSURLErrorNetworkConnectionLost {
self = .connection(error)
} else {
self = .unhandled(error)
}
} else {
self = .unhandled(error)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/GraphQLAPIKit/Errors/GraphQLAPIError.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Apollo
import Foundation

public struct GraphQLError: LocalizedError {
public struct GraphQLError: LocalizedError, Sendable {
public let message: String
public let code: String?

Expand Down

This file was deleted.

Loading