Skip to content
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

Add an option to gzip compress gql http requests #10

Merged
merged 3 commits into from
Jan 13, 2023
Merged
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
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ let package = Package(
.package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.41.2"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
.package(url: "https://github.com/jpsim/Yams.git", from: "4.0.4"),
.package(url: "https://github.com/unstoppablefi/GzipSwift", .upToNextMajor(from: "5.0.0")),
],
targets: [
/* SwiftGraphQL */
.target(
name: "SwiftGraphQL",
dependencies: [],
dependencies: [.product(name: "Gzip", package: "GzipSwift")],
path: "Sources/SwiftGraphQL"
),
.target(
Expand Down
16 changes: 14 additions & 2 deletions Sources/SwiftGraphQL/HTTP.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//import Combine
import Foundation
import Gzip

/*
SwiftGraphQL has no client as it needs no state. Developers
Expand All @@ -25,6 +26,7 @@ public func send<Type, TypeLock>(
headers: HttpHeaders = [:],
method: HttpMethod = .post,
session: URLSession = .shared,
compressRequest: Bool = false,
onComplete completionHandler: @escaping (Response<Type, TypeLock>) -> Void
) -> URLSessionDataTask? where TypeLock: GraphQLHttpOperation & Decodable {
send(
Expand All @@ -34,6 +36,7 @@ public func send<Type, TypeLock>(
headers: headers,
method: method,
session: session,
compressRequest: compressRequest,
completionHandler: completionHandler
)
}
Expand All @@ -59,6 +62,7 @@ public func send<Type, TypeLock>(
headers: HttpHeaders = [:],
method: HttpMethod = .post,
session: URLSession = .shared,
compressRequest: Bool = false,
onComplete completionHandler: @escaping (Response<Type, TypeLock>) -> Void
) -> URLSessionDataTask? where TypeLock: GraphQLHttpOperation & Decodable {
send(
Expand All @@ -68,6 +72,7 @@ public func send<Type, TypeLock>(
headers: headers,
method: method,
session: session,
compressRequest: compressRequest,
completionHandler: completionHandler
)
}
Expand All @@ -81,6 +86,7 @@ private func send<Type, TypeLock>(
headers: HttpHeaders,
method: HttpMethod,
session: URLSession,
compressRequest: Bool = false,
completionHandler: @escaping (Response<Type, TypeLock>) -> Void
) -> URLSessionDataTask? where TypeLock: GraphQLOperation & Decodable {
// Validate that we got a valid url.
Expand All @@ -98,6 +104,7 @@ private func send<Type, TypeLock>(
url: url,
headers: headers,
method: method,
compressRequest: compressRequest,
debugTime: debugTime
)

Expand Down Expand Up @@ -220,6 +227,7 @@ private func createGraphQLRequest<Type, TypeLock>(
url: URL,
headers: HttpHeaders,
method: HttpMethod,
compressRequest: Bool,
debugTime: UInt64
) -> URLRequest where TypeLock: GraphQLOperation & Decodable {
// Construct a request.
Expand All @@ -246,8 +254,12 @@ private func createGraphQLRequest<Type, TypeLock>(
}
#endif
#endif
let encoded = try! encoder.encode(payload)
request.httpBody = encoded
request.httpBody = try? encoder.encode(payload)

if compressRequest, let gzipped = try? encoder.encode(payload) {
request.httpBody = gzipped
request.setValue("gzip", forHTTPHeaderField: "Content-Encoding")
}

return request
}
Expand Down