-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from d-exclaimation/chore/graphql-http-violation
Minor improvement to codebase
- Loading branch information
Showing
14 changed files
with
180 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// GraphQLViolation.swift | ||
// pioneer | ||
// | ||
// Created by d-exclaimation on 20:04. | ||
// | ||
|
||
import enum NIOHTTP1.HTTPResponseStatus | ||
|
||
/// Violation to the GraphQL over HTTP spec | ||
public struct GraphQLViolation: Error, Sendable, Equatable { | ||
/// Different HTTP status codes for different media type as per GraphQL over HTTP spec | ||
public struct ResponseStatuses: Sendable, Equatable { | ||
/// Status for application/json | ||
public var json: HTTPResponseStatus | ||
/// Status for application/graphql-response+json | ||
public var graphql: HTTPResponseStatus | ||
|
||
public init(json: HTTPResponseStatus, graphql: HTTPResponseStatus) { | ||
self.json = json | ||
self.graphql = graphql | ||
} | ||
} | ||
|
||
/// Default message for this error | ||
public var message: String | ||
/// Appopriate HTTP status code for this error as per GraphQL over HTTP spec | ||
public var status: ResponseStatuses | ||
|
||
public init(message: String, status: HTTPResponseStatus) { | ||
self.message = message | ||
self.status = .init(json: status, graphql: status) | ||
} | ||
|
||
public init(message: String, status: ResponseStatuses) { | ||
self.message = message | ||
self.status = status | ||
} | ||
|
||
/// Get the appropriate HTTP status code for the media type | ||
/// - Parameter isAcceptingGraphQLResponse: If the accept media type is application/graphql-response+json | ||
/// - Returns: HTTP status code | ||
public func status(_ isAcceptingGraphQLResponse: Bool) -> HTTPResponseStatus { | ||
isAcceptingGraphQLResponse ? status.graphql : status.json | ||
} | ||
|
||
static var missingQuery: Self { | ||
.init( | ||
message: "Missing query in request", | ||
status: .init(json: .ok, graphql: .badRequest) | ||
) | ||
} | ||
|
||
static var invalidForm: Self { | ||
.init( | ||
message: "Invalid GraphQL request form", | ||
status: .init(json: .ok, graphql: .badRequest) | ||
) | ||
} | ||
|
||
static var invalidMethod: Self { | ||
.init(message: "Invalid HTTP method for a GraphQL request", status: .badRequest) | ||
} | ||
|
||
static var invalidContentType: Self { | ||
.init(message: "Invalid or missing content-type", status: .badRequest) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.