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
2 changes: 1 addition & 1 deletion Sources/GraphQLWS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,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/GraphQLWS/GraphQLWSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ struct GraphQLWSError: 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
139 changes: 102 additions & 37 deletions Sources/GraphQLWS/Requests.swift
Original file line number Diff line number Diff line change
@@ -1,59 +1,124 @@
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
public struct ConnectionInitRequest<InitPayload: Codable & Equatable>: Equatable, JsonEncodable {
var type = RequestMessageType.GQL_CONNECTION_INIT
let payload: InitPayload
public let type: RequestMessageType = .GQL_CONNECTION_INIT
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) != .GQL_CONNECTION_INIT {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "type must be `\(RequestMessageType.GQL_CONNECTION_INIT.type)`"
))
}
payload = try container.decode(InitPayload.self, forKey: .payload)
}
}

/// A websocket `start` request from the client to the server
struct StartRequest: Equatable, JsonEncodable {
var type = RequestMessageType.GQL_START
let payload: GraphQLRequest
let id: String
public struct StartRequest: Equatable, JsonEncodable {
public let type: RequestMessageType = .GQL_START
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) != .GQL_START {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "type must be `\(RequestMessageType.GQL_START.type)`"
))
}
payload = try container.decode(GraphQLRequest.self, forKey: .payload)
id = try container.decode(String.self, forKey: .id)
}
}

/// A websocket `stop` request from the client to the server
struct StopRequest: Equatable, JsonEncodable {
var type = RequestMessageType.GQL_STOP
let id: String
public struct StopRequest: Equatable, JsonEncodable {
public let type: RequestMessageType = .GQL_STOP
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) != .GQL_CONNECTION_TERMINATE {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "type must be `\(RequestMessageType.GQL_STOP.type)`"
))
}
id = try container.decode(String.self, forKey: .id)
}
}

/// A websocket `connection_terminate` request from the client to the server
struct ConnectionTerminateRequest: Equatable, JsonEncodable {
var type = RequestMessageType.GQL_CONNECTION_TERMINATE
public struct ConnectionTerminateRequest: Equatable, JsonEncodable {
public let type: RequestMessageType = .GQL_CONNECTION_TERMINATE

public init() {}

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

/// The supported websocket request message types from the client to the server
enum RequestMessageType: String, Codable {
case GQL_CONNECTION_INIT = "connection_init"
case GQL_START = "start"
case GQL_STOP = "stop"
case GQL_CONNECTION_TERMINATE = "connection_terminate"
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 response 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 GQL_CONNECTION_INIT: Self = .init(type: .GQL_CONNECTION_INIT)
public static let GQL_START: Self = .init(type: .GQL_START)
public static let GQL_STOP: Self = .init(type: .GQL_STOP)
public static let GQL_CONNECTION_TERMINATE: Self = .init(type: .GQL_CONNECTION_TERMINATE)

enum RequestType: String, Codable {
case GQL_CONNECTION_INIT = "connection_init"
case GQL_START = "start"
case GQL_STOP = "stop"
case GQL_CONNECTION_TERMINATE = "connection_terminate"
}
}
Loading