Skip to content

Commit

Permalink
Merge pull request #144 from ZirgVoice/serialize-in-id-scalar
Browse files Browse the repository at this point in the history
added: serialize in asScalar
  • Loading branch information
d-exclaimation authored Dec 19, 2024
2 parents abe3e58 + 49cce54 commit 210d7de
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5.2
// swift-tools-version:5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
1 change: 1 addition & 0 deletions Sources/Pioneer/Extensions/Int/UInt64+Nanoseconds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Created by d-exclaimation on 14:19.
//
import Vapor

public extension Optional where WrappedType == UInt64 {
/// Convert the given value in seconds into nanoseconds
Expand Down
20 changes: 18 additions & 2 deletions Sources/Pioneer/GraphQL/BuiltinTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,24 @@ public struct ID: Codable, ExpressibleByStringLiteral, CustomStringConvertible,

/// Apply scalar to Graphiti schema to allow the use of ID.
public static func asScalar<Resolver, Context>() -> Scalar<Resolver, Context, Self> {
.init(ID.self, as: "ID")
.description("The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable")
.init(
ID.self,
as: "ID",
serialize: { value, _ in
guard let idValue = value as? ID else {
throw GraphQLError(message: "Failed to cast value \(value) to ID type")
}

return try GraphQLID.serialize(value: idValue.id)
},
parseValue: { inputValue, _ in
try GraphQLID.parseValue(value: inputValue)
},
parseLiteral: { ast, _ in
try GraphQLID.parseLiteral(valueAST: ast)
}
)
.description("The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable")
}

/// Create a new ID from UUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import class GraphQL.GraphQLJSONEncoder
import Vapor

extension GraphQLJSONEncoder: ContentEncoder {
extension GraphQLJSONEncoder: Vapor.ContentEncoder {
public func encode<E>(_ encodable: E, to body: inout NIOCore.ByteBuffer, headers: inout NIOHTTP1.HTTPHeaders) throws where E: Encodable {
headers.contentType = .json
try body.writeBytes(self.encode(encodable))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
import struct GraphQL.GraphQLResult
import protocol Vapor.Content

extension GraphQLResult: Content {}
extension GraphQLResult: Vapor.Content {}
2 changes: 1 addition & 1 deletion Sources/Pioneer/Vapor/Http/Pioneer+IDE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Pioneer {
case .sandbox:
return serve(html: embeddedSandboxHtml)
case let .redirect(to: cloud):
return req.redirect(to: cloud.url, type: .permanent)
return req.redirect(to: cloud.url, redirectType: .permanent)
case .disable:
return Response(status: .notFound)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Pioneer/Vapor/WebSocket/Pioneer+WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public extension Pioneer {
}

/// Should upgrade callback
@Sendable
internal func shouldUpgrade(req: Request) -> EventLoopFuture<HTTPHeaders?> {
req.eventLoop.makeSucceededFuture(.init([("Sec-WebSocket-Protocol", websocketProtocol.name)]))
}
Expand Down
12 changes: 8 additions & 4 deletions Tests/PioneerTests/VaporTests/HTTPQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ final class HTTPQueryTests: XCTestCase {
let gql3 = GraphQLRequest(query: "query { error { id, name } }")
let gql4 = GraphQLRequest(query: "query { invalid }")

let app = Application(.testing)
let app = try await Application.make(.testing)
defer {
app.shutdown()
Task{
try await app.asyncShutdown()
}
}

app.middleware.use(server.vaporMiddleware(), at: .beginning)
Expand Down Expand Up @@ -181,9 +183,11 @@ final class HTTPQueryTests: XCTestCase {
let gql3 = "query=" + "query { error { id, name } }".addingPercentEncoding(withAllowedCharacters: .alphanumerics)!
let gql4 = "query=" + "query { invalid }".addingPercentEncoding(withAllowedCharacters: .alphanumerics)!

let app = Application(.testing)
let app = try await Application.make(.testing)
defer {
app.shutdown()
Task{
try await app.asyncShutdown()
}
}

app.middleware.use(server.vaporMiddleware(), at: .beginning)
Expand Down

0 comments on commit 210d7de

Please sign in to comment.