Skip to content

Commit

Permalink
Merge pull request #109 from d-exclaimation/fix/#108/public-structs-p…
Browse files Browse the repository at this point in the history
…ublic-init

Public structs, public init
  • Loading branch information
d-exclaimation authored Nov 26, 2022
2 parents b63739a + 6d2b215 commit 800edf7
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 14 deletions.
12 changes: 0 additions & 12 deletions Sources/Pioneer/Extensions/Closure.swift

This file was deleted.

18 changes: 18 additions & 0 deletions Sources/Pioneer/Extensions/Expression.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Expression.swift
// Pioneer
//
// Created by d-exclaimation.
//

/// Define an expression from a closure
/// - Returns: The returned value of this closure
public func expression<ReturnType>(_ fn: () throws -> ReturnType) rethrows -> ReturnType {
try fn()
}

/// Define an expression from a closure
/// - Returns: The returned value of this closure
public func expression<ReturnType>(_ fn: () async throws -> ReturnType) async rethrows -> ReturnType {
try await fn()
}
5 changes: 5 additions & 0 deletions Sources/Pioneer/GraphQL/BuiltinTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public struct ID : Codable, ExpressibleByStringLiteral, CustomStringConvertible,
/// The detailed reasoning why conversion failed
public var reason: String

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

public var localizedDescription: String {
"ID.ConversionError ('\(id)'): \"\(reason)\""
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/Pioneer/GraphQL/GraphQLMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public struct GraphQLMessage: Codable {
public var id: String?
public var type: String
public var payload: Map?

init(id: String? = nil, type: String, payload: Map? = nil) {
self.id = id
self.type = type
self.payload = payload
}
}

static func errors(id: String? = nil, type: String, _ error: [GraphQLError]) -> Variance {
Expand Down
30 changes: 30 additions & 0 deletions Sources/Pioneer/Http/HTTPGraphQL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import enum NIOHTTP1.HTTPResponseStatus
import enum NIOHTTP1.HTTPMethod
import enum GraphQL.Map
import struct NIOHTTP1.HTTPHeaders
import struct GraphQL.GraphQLResult
import struct GraphQL.GraphQLError

extension Pioneer {
/// HTTP-based GraphQL Response
Expand All @@ -18,6 +20,16 @@ extension Pioneer {

/// HTTP status code for this response
public var status: HTTPResponseStatus

public init(result: GraphQLResult, status: HTTPResponseStatus) {
self.result = result
self.status = status
}

public init(data: Map? = nil, errors: [GraphQLError] = [], status: HTTPResponseStatus) {
self.result = .init(data: data, errors: errors)
self.status = status
}
}

/// HTTP-based GraphQL request
Expand All @@ -30,5 +42,23 @@ extension Pioneer {

/// HTTP method for this request
public var method: HTTPMethod

public init(request: GraphQLRequest, headers: HTTPHeaders, method: HTTPMethod) {
self.request = request
self.headers = headers
self.method = method
}

public init(
query: String,
operationName: String? = nil,
variables: [String: Map]? = nil,
headers: HTTPHeaders,
method: HTTPMethod
) {
self.request = .init(query: query, operationName: operationName, variables: variables)
self.headers = headers
self.method = method
}
}
}
2 changes: 1 addition & 1 deletion Sources/Pioneer/Http/IDE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ extension Pioneer {

/// GraphiQL HTML
public var graphiqlHtml: String {
let fetcher: String = def {
let fetcher: String = expression {
switch websocketProtocol {
case .subscriptionsTransportWs:
return """
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pioneer/Pioneer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public struct Pioneer<Resolver, Context> {
self.timeout = timeout


let proto: SubProtocol.Type = def {
let proto: SubProtocol.Type = expression {
switch websocketProtocol {
case .graphqlWs:
return GraphQLWs.self
Expand Down

0 comments on commit 800edf7

Please sign in to comment.