Skip to content

Commit b2e24e7

Browse files
Merge pull request #7 from GraphQLSwift/fix/websocket-collection-size
v1.0 Cleanup
2 parents 3aa03a1 + 1445b5b commit b2e24e7

5 files changed

Lines changed: 16 additions & 19 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FGraphQLSwift%2Fgraphql-vapor%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/GraphQLSwift/graphql-vapor)
44
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FGraphQLSwift%2Fgraphql-vapor%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/GraphQLSwift/graphql-vapor)
55

6-
> ***WARNING***: This package is in v0.x beta. It's API is still evolving and is subject to breaking changes in minor version bumps.
7-
86
A Swift library for integrating [GraphQL](https://github.com/GraphQLSwift/GraphQL) with [Vapor](https://github.com/vapor/vapor), enabling you to easily expose GraphQL APIs in your Vapor applications.
97

108
## Features

Sources/GraphQLVapor/GraphQLConfig.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public struct GraphQLConfig<
88
>: Sendable {
99
let allowGet: Bool
1010
let allowMissingAcceptHeader: Bool
11+
let maxBodySize: ByteCount?
1112
let ide: IDE
1213
let subscriptionProtocols: Set<SubscriptionProtocol>
1314
let websocket: WebSocket
@@ -17,13 +18,15 @@ public struct GraphQLConfig<
1718
/// - Parameters:
1819
/// - allowGet: Whether to allow GraphQL queries via `GET` requests.
1920
/// - allowMissingAcceptHeader: Whether to allow clients to omit "Accept" headers and default to `application/graphql-response+json` encoded responses.
21+
/// - maxBodySize: The maximum size of GraphQL requests in bytes. If not provided, this uses the default [`app.routes.defaultMaxBodySize`](https://docs.vapor.codes/basics/routing/#body-streaming)
2022
/// - ide: The IDE to expose
2123
/// - subscriptionProtocols: Protocols used to support GraphQL subscription requests
2224
/// - websocket: WebSocket-specific configuration
2325
/// - additionalValidationRules: Additional validation rules to apply to requests. The default GraphQL validation rules are always applied.
2426
public init(
2527
allowGet: Bool = true,
2628
allowMissingAcceptHeader: Bool = false,
29+
maxBodySize: ByteCount? = nil,
2730
ide: IDE = .graphiql,
2831
subscriptionProtocols: Set<SubscriptionProtocol> = [],
2932
websocket: WebSocket = .init(
@@ -34,12 +37,14 @@ public struct GraphQLConfig<
3437
) {
3538
self.allowGet = allowGet
3639
self.allowMissingAcceptHeader = allowMissingAcceptHeader
40+
self.maxBodySize = maxBodySize
3741
self.additionalValidationRules = additionalValidationRules
3842
self.ide = ide
3943
self.subscriptionProtocols = subscriptionProtocols
4044
self.websocket = websocket
4145
}
4246

47+
/// An embeddable GraphQL IDE
4348
public struct IDE: Sendable, Equatable {
4449
/// GraphiQL: https://github.com/graphql/graphiql
4550
public static var graphiql: Self {
@@ -58,6 +63,7 @@ public struct GraphQLConfig<
5863
}
5964
}
6065

66+
/// A GraphQL subscription implementation
6167
public struct SubscriptionProtocol: Sendable, Hashable {
6268
/// Expose GraphQL subscriptions over WebSockets
6369
public static var websocket: Self {
@@ -70,6 +76,7 @@ public struct GraphQLConfig<
7076
}
7177
}
7278

79+
/// WebSocket configuration
7380
public struct WebSocket: Sendable {
7481
let onWebSocketInit: @Sendable (WebSocketInit, Request) async throws -> WebSocketInitResult
7582

Sources/GraphQLVapor/HTTP/GraphQLHandler+HTTP.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,16 @@ extension GraphQLHandler {
8686

8787
let response = Response()
8888

89-
let configuredMediaTypes: Set<HTTPMediaType> = [.jsonGraphQL, .json]
89+
let configuredMediaTypes: [HTTPMediaType] = [.jsonGraphQL, .json]
9090

9191
// Try to respond with the best matching media type, in order
9292
var selectedMediaType: HTTPMediaType? = nil
93-
for mediaType in headers.accept.mediaTypes {
94-
if configuredMediaTypes.contains(mediaType) {
95-
selectedMediaType = mediaType
96-
break
97-
}
98-
}
99-
100-
// If no exact matches, look for any matching wildcards
101-
if selectedMediaType == nil {
102-
let acceptableMediaSet = HTTPMediaTypeSet(mediaTypes: headers.accept.mediaTypes)
103-
for mediaType in configuredMediaTypes {
104-
if acceptableMediaSet.contains(mediaType) {
105-
selectedMediaType = mediaType
106-
break
93+
headerLoop: for mediaType in headers.accept.mediaTypes {
94+
for configuredMediaType in configuredMediaTypes {
95+
// Note that `==` handles wildcards.
96+
if mediaType == configuredMediaType {
97+
selectedMediaType = configuredMediaType
98+
break headerLoop
10799
}
108100
}
109101
}

Sources/GraphQLVapor/RoutesBuilder+graphql.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public extension RoutesBuilder {
6363
}
6464
return try await handler.handleGet(request: request)
6565
}
66-
post(path) { request in
66+
on(.POST, path, body: .collect(maxSize: config.maxBodySize)) { request in
6767
try await handler.handlePost(request: request)
6868
}
6969
}

Sources/GraphQLVapor/WebSocket/GraphQLHandler+handleWebSocket.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extension GraphQLHandler {
1111
let subProtocol = try negotiateSubProtocol(request: request)
1212
let response = Response(status: .switchingProtocols)
1313
response.upgrader = WebSocketUpgrader(
14-
maxFrameSize: .default,
14+
maxFrameSize: .init(integerLiteral: (config.maxBodySize ?? request.application.routes.defaultMaxBodySize).value),
1515
shouldUpgrade: {
1616
request.eventLoop.makeFutureWithTask {
1717
["Sec-WebSocket-Protocol": subProtocol.rawValue]

0 commit comments

Comments
 (0)