Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom encoders support for router #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
205 changes: 0 additions & 205 deletions Package.resolved

This file was deleted.

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ In your `configure.swift` file call the `register(graphQLSchema: Schema<YourReso

```Swift
// Register the schema and it's resolver.
app.register(graphQLSchema: todoSchema, withResolver: TodoAPI())
app.register(
graphQLSchema: todoSchema,
withResolver: TodoAPI(),
customEncoder: GraphQLJSONEncoder()
)
```

## License
Expand Down
83 changes: 73 additions & 10 deletions Sources/GraphQLKit/Graphiti+Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,36 @@ import Graphiti
import GraphQL

extension RoutesBuilder {
public func register<RootType>(graphQLSchema schema: Schema<RootType, Request>, withResolver rootAPI: RootType, at path: PathComponent="graphql", postBodyStreamStrategy: HTTPBodyStreamStrategy = .collect) {
public func register<RootType>(
graphQLSchema schema: Schema<RootType, Request>,
withResolver rootAPI: RootType,
at path: PathComponent="graphql",
postBodyStreamStrategy: HTTPBodyStreamStrategy = .collect,
customEncoder: ContentEncoder? = nil
) {
self.on(.POST, path, body: postBodyStreamStrategy) { (request) -> EventLoopFuture<Response> in
try request.resolveByBody(graphQLSchema: schema, with: rootAPI)
.flatMap({
$0.encodeResponse(status: .ok, for: request)
})
try request.resolveByBody(
graphQLSchema: schema,
with: rootAPI
).flatMap { result in
result.encodeResponse(
status: .ok,
for: request,
using: customEncoder
)
}
}
self.get(path) { (request) -> EventLoopFuture<Response> in
try request.resolveByQueryParameters(graphQLSchema: schema, with: rootAPI)
.flatMap({
$0.encodeResponse(status: .ok, for: request)
})
try request.resolveByQueryParameters(
graphQLSchema: schema,
with: rootAPI
).flatMap { result in
result.encodeResponse(
status: .ok,
for: request,
using: customEncoder
)
}
}
}
}
Expand All @@ -23,4 +41,49 @@ enum GraphQLResolveError: Swift.Error {
case noQueryFound
}

extension GraphQLResult: Content { }
extension GraphQLResult: Content {
func encodeResponse(
status: HTTPStatus,
headers: HTTPHeaders = [:],
for request: Request,
using encoder: ContentEncoder?
) -> EventLoopFuture<Response> {
if let encoder = encoder {
let response = Response()
do {
var body = ByteBufferAllocator().buffer(capacity: 0)
try encoder.encode(self, to: &body, headers: &response.headers)
response.body = Response.Body(buffer: body)

for (name, value) in headers {
response.headers.replaceOrAdd(
name: name,
value: value
)
}

response.status = status
} catch {
return request.eventLoop.makeFailedFuture(error)
}
return request.eventLoop.makeSucceededFuture(response)
} else {
return self.encodeResponse(
status: status,
headers: headers,
for: request
)
}
}
}

extension GraphQLJSONEncoder: ContentEncoder {
public func encode<E>(
_ encodable: E,
to body: inout ByteBuffer,
headers: inout HTTPHeaders
) throws where E: Encodable {
headers.contentType = .json
try body.writeBytes(self.encode(encodable))
}
}