Skip to content

Commit

Permalink
Use order-preserving JSON encoder in graphql-server
Browse files Browse the repository at this point in the history
This uses the GraphQLJSONEncoder when converting a GraphQLResult to a
Response to return the query results in the order that the were
specified in the request.
  • Loading branch information
rgcottrell committed Oct 24, 2024
1 parent a16b97b commit 7063636
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.

This file was deleted.

18 changes: 18 additions & 0 deletions graphql-server/Sources/App/GraphQL/GraphQL+ResponseGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import GraphQL
import HTTPTypes
import Hummingbird

extension GraphQLResult: ResponseGenerator {
public func response(from _: Request, context _: some RequestContext) throws -> Response {
let encoder = GraphQLJSONEncoder()
let data = try encoder.encode(self)
return Response(
status: .ok,
headers: [
.contentType: "application/json; charset=utf-8",
.contentLength: "\(data.count)",
],
body: .init(byteBuffer: ByteBuffer(data: data))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ extension GraphQLError: HTTPResponseError {
}

public func response(from request: Request, context: some RequestContext) -> Response {
.init(status: status, headers: headers, body: .init(byteBuffer: ByteBuffer(string: message)))
.init(status: self.status, headers: self.headers, body: .init(byteBuffer: ByteBuffer(string: message)))
}
}

0 comments on commit 7063636

Please sign in to comment.