Skip to content
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
61 changes: 31 additions & 30 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 Sources/GraphQLTransportWS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class Client<InitPayload: Equatable & Codable> {
return
}
try await self.onComplete(completeResponse, self)
case .unknown:
default:
try await self.error(.invalidType())
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/GraphQLTransportWS/GraphqlTransportWSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ struct GraphQLTransportWSError: Error {

static func invalidRequestFormat(messageType: RequestMessageType) -> Self {
return self.init(
"Request message doesn't match '\(messageType.rawValue)' JSON format",
"Request message doesn't match '\(messageType.type.rawValue)' JSON format",
code: .invalidRequestFormat
)
}

static func invalidResponseFormat(messageType: ResponseMessageType) -> Self {
return self.init(
"Response message doesn't match '\(messageType.rawValue)' JSON format",
"Response message doesn't match '\(messageType.type.rawValue)' JSON format",
code: .invalidResponseFormat
)
}
Expand Down
122 changes: 87 additions & 35 deletions Sources/GraphQLTransportWS/Requests.swift
Original file line number Diff line number Diff line change
@@ -1,53 +1,105 @@
import Foundation
import GraphQL

/// We also require that an 'authToken' field is provided in the 'payload' during the connection
/// init message. For example:
/// ```
/// {
/// "type": 'connection_init',
/// "payload": {
/// "authToken": "eyJhbGciOiJIUz..."
/// }
/// }
/// ```

/// A general request. This object's type is used to triage to other, more specific request objects.
struct Request: Equatable, JsonEncodable {
let type: RequestMessageType
public struct Request: Equatable, JsonEncodable {
public let type: RequestMessageType
}

/// A websocket `connection_init` request from the client to the server
struct ConnectionInitRequest<InitPayload: Codable & Equatable>: Equatable, JsonEncodable {
var type = RequestMessageType.connectionInit
let payload: InitPayload
public struct ConnectionInitRequest<InitPayload: Codable & Equatable>: Equatable, JsonEncodable {
public let type: RequestMessageType = .connectionInit
public let payload: InitPayload

public init(payload: InitPayload) {
self.payload = payload
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
if try container.decode(RequestMessageType.self, forKey: .type) != .connectionInit {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "type must be `\(RequestMessageType.connectionInit.type)`"
))
}
payload = try container.decode(InitPayload.self, forKey: .payload)
}
}

/// A websocket `subscribe` request from the client to the server
struct SubscribeRequest: Equatable, JsonEncodable {
var type = RequestMessageType.subscribe
let payload: GraphQLRequest
let id: String
public struct SubscribeRequest: Equatable, JsonEncodable {
public let type = RequestMessageType.subscribe
public let payload: GraphQLRequest
public let id: String

public init(payload: GraphQLRequest, id: String) {
self.payload = payload
self.id = id
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
if try container.decode(RequestMessageType.self, forKey: .type) != .subscribe {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "type must be `\(RequestMessageType.subscribe.type)`"
))
}
payload = try container.decode(GraphQLRequest.self, forKey: .payload)
id = try container.decode(String.self, forKey: .id)
}
}

/// A websocket `complete` request from the client to the server
struct CompleteRequest: Equatable, JsonEncodable {
var type = RequestMessageType.complete
let id: String
public struct CompleteRequest: Equatable, JsonEncodable {
public let type = RequestMessageType.complete
public let id: String

public init(id: String) {
self.id = id
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
if try container.decode(RequestMessageType.self, forKey: .type) != .complete {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "type must be `\(RequestMessageType.complete.type)`"
))
}
id = try container.decode(String.self, forKey: .id)
}
}

/// The supported websocket request message types from the client to the server
enum RequestMessageType: String, Codable {
case connectionInit = "connection_init"
case subscribe
case complete
case unknown

init(from decoder: Decoder) throws {
guard let value = try? decoder.singleValueContainer().decode(String.self) else {
self = .unknown
return
}
self = RequestMessageType(rawValue: value) ?? .unknown
public struct RequestMessageType: Equatable, Codable, Sendable {
// This is implemented as a struct with only public static properties, backed by an internal enum
// in order to grow the list of accepted request types in a non-breaking way.

let type: RequestType

init(type: RequestType) {
self.type = type
}

public init(from decoder: any Decoder) throws {
let container = try decoder.singleValueContainer()
type = try container.decode(RequestType.self)
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(type)
}

public static let connectionInit: Self = .init(type: .connectionInit)
public static let subscribe: Self = .init(type: .subscribe)
public static let complete: Self = .init(type: .complete)

enum RequestType: String, Codable {
case connectionInit = "connection_init"
case subscribe
case complete
}
}
111 changes: 85 additions & 26 deletions Sources/GraphQLTransportWS/Responses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,79 @@ import Foundation
import GraphQL

/// A general response. This object's type is used to triage to other, more specific response objects.
struct Response: Equatable, JsonEncodable {
let type: ResponseMessageType
public struct Response: Equatable, JsonEncodable {
public let type: ResponseMessageType
}

/// A websocket `connection_ack` response from the server to the client
public struct ConnectionAckResponse: Equatable, JsonEncodable {
let type: ResponseMessageType
public let type: ResponseMessageType = .connectionAck
public let payload: [String: Map]?

init(_ payload: [String: Map]? = nil) {
type = .connectionAck
public init(payload: [String: Map]? = nil) {
self.payload = payload
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
if try container.decode(ResponseMessageType.self, forKey: .type) != .connectionAck {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "type must be `\(ResponseMessageType.connectionAck.type)`"
))
}
payload = try container.decodeIfPresent([String: Map].self, forKey: .payload)
}
}

/// A websocket `next` response from the server to the client
public struct NextResponse: Equatable, JsonEncodable {
let type: ResponseMessageType
public let type: ResponseMessageType = .next
public let payload: GraphQLResult?
public let id: String

init(_ payload: GraphQLResult? = nil, id: String) {
type = .next
public init(payload: GraphQLResult?, id: String) {
self.payload = payload
self.id = id
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
if try container.decode(ResponseMessageType.self, forKey: .type) != .next {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "type must be `\(ResponseMessageType.next.type)`"
))
}
payload = try container.decodeIfPresent(GraphQLResult.self, forKey: .payload)
id = try container.decode(String.self, forKey: .id)
}
}

/// A websocket `complete` response from the server to the client
public struct CompleteResponse: Equatable, JsonEncodable {
let type: ResponseMessageType
public let type: ResponseMessageType = .complete
public let id: String

init(id: String) {
type = .complete
public init(id: String) {
self.id = id
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
if try container.decode(ResponseMessageType.self, forKey: .type) != .complete {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "type must be `\(ResponseMessageType.complete.type)`"
))
}
id = try container.decode(String.self, forKey: .id)
}
}

/// A websocket `error` response from the server to the client
public struct ErrorResponse: Equatable, JsonEncodable {
let type: ResponseMessageType
public let type: ResponseMessageType = .error
public let payload: [GraphQLError]
public let id: String

Expand All @@ -56,26 +87,54 @@ public struct ErrorResponse: Equatable, JsonEncodable {
return GraphQLError(error)
}
}
type = .error
payload = graphQLErrors
self.id = id
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
if try container.decode(ResponseMessageType.self, forKey: .type) != .error {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "type must be `\(ResponseMessageType.error.type)`"
))
}
payload = try container.decode([GraphQLError].self, forKey: .payload)
id = try container.decode(String.self, forKey: .id)
}
}

/// The supported websocket response message types from the server to the client
enum ResponseMessageType: String, Codable {
case connectionAck = "connection_ack"
case next
case error
case complete
case unknown

init(from decoder: Decoder) throws {
guard let value = try? decoder.singleValueContainer().decode(String.self) else {
self = .unknown
return
}
self = ResponseMessageType(rawValue: value) ?? .unknown
public struct ResponseMessageType: Equatable, Codable, Sendable {
// This is implemented as a struct with only public static properties, backed by an internal enum
// in order to grow the list of accepted response types in a non-breaking way.

let type: ResponseType

init(type: ResponseType) {
self.type = type
}

public init(from decoder: any Decoder) throws {
let container = try decoder.singleValueContainer()
type = try container.decode(ResponseType.self)
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(type)
}

public static let connectionAck: Self = .init(type: .connectionAck)
public static let next: Self = .init(type: .next)
public static let complete: Self = .init(type: .complete)
public static let error: Self = .init(type: .error)

enum ResponseType: String, Codable {
case connectionAck = "connection_ack"
case next
case complete
case error
}
}

Expand Down
Loading