Skip to content

Commit

Permalink
Feat: Updated README and made 2 seperate httpHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
d-exclaimation committed Sep 17, 2022
1 parent 2aa9bbb commit d0b60cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pioneer is an open-source Swift GraphQL server for [Vapor](https://github.com/va
## Setup

```swift
.package(url: "https://github.com/d-exclaimation/pioneer", from: "0.9.4")
.package(url: "https://github.com/d-exclaimation/pioneer", from: "0.10.0")
```

## Swift for GraphQL
Expand Down
19 changes: 12 additions & 7 deletions Sources/Pioneer/Http/Pioneer+Http.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,30 @@ extension Pioneer {
at path: PathComponent = "graphql",
bodyStrategy: HTTPBodyStreamStrategy = .collect
) {
router.on(.POST, path, body: bodyStrategy) {
try await self.httpHandler(req: $0)
}
router.on(.POST, path, body: bodyStrategy, use: httpHandler(req:))
}

/// Apply middleware for `GET`
func applyGet(
on router: RoutesBuilder,
at path: PathComponent = "graphql"
) {
router.get(path) {
try await self.httpHandler(req: $0)
}
router.get(path, use: httpHandler(req:))
}

/// Common Handler for GraphQL through HTTP
/// - Parameter req: The HTTP request being made
/// - Returns: A response from the GraphQL operation execution properly formatted
public func httpHandler(req: Request, using encoder: ContentEncoder = GraphQLJSONEncoder()) async throws -> Response {
public func httpHandler(req: Request) async throws -> Response {
try await httpHandler(req: req, using: GraphQLJSONEncoder())
}

/// Common Handler for GraphQL through HTTP
/// - Parameters:
/// - req: The HTTP request being made
/// - using: The custom content encoder
/// - Returns: A response from the GraphQL operation execution properly formatted
public func httpHandler(req: Request, using encoder: ContentEncoder) async throws -> Response {
// Check for CSRF Prevention
guard isCSRFProtected(isActive: httpStrategy == .csrfPrevention, on: req) else {
return try GraphQLError(
Expand Down

0 comments on commit d0b60cb

Please sign in to comment.