@@ -19,7 +19,7 @@ public extension RouterMethods {
1919 _ path: RouterPath = " graphql " ,
2020 schema: GraphQLSchema ,
2121 rootValue: any Sendable = ( ) ,
22- config: GraphQLConfig < EmptyWebSocketInit , Void > = . init( ) ,
22+ config: GraphQLConfig < Context , EmptyWebSocketInit , Void > = . init( ) ,
2323 computeContext: @Sendable @escaping ( GraphQLContextComputationInputs < Context , Void > ) async throws -> GraphQLContext
2424 ) -> Self {
2525 // https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#request
@@ -84,7 +84,7 @@ public extension RouterMethods where Context: WebSocketRequestContext {
8484 _ path: RouterPath = " graphql " ,
8585 schema: GraphQLSchema ,
8686 rootValue: any Sendable = ( ) ,
87- config: GraphQLConfig < WebSocketInit , WebSocketInitResult > = GraphQLConfig < EmptyWebSocketInit , Void > ( ) ,
87+ config: GraphQLConfig < Context , WebSocketInit , WebSocketInitResult > ,
8888 computeContext: @Sendable @escaping ( GraphQLContextComputationInputs < Context , WebSocketInitResult > ) async throws -> GraphQLContext
8989 ) -> Self {
9090 let handler = GraphQLHandler < Context , GraphQLContext , WebSocketInit , WebSocketInitResult > (
@@ -108,4 +108,34 @@ public extension RouterMethods where Context: WebSocketRequestContext {
108108 }
109109 return self
110110 }
111+
112+ /// Registers a graphql websocket route that responds using the provided schema.
113+ ///
114+ /// WebSocket requests support the
115+ /// [`graphql-transport-ws`](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md)
116+ /// and [`graphql-ws`](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md)
117+ /// subprotocols.
118+ ///
119+ /// - Parameters:
120+ /// - path: The route that should respond to GraphQL requests. Both `GET` and `POST` routes are registered.
121+ /// - schema: The GraphQL schema that should be used to respond to requests.
122+ /// - rootValue: The `rootValue` GraphQL execution arg. This is the object passed to the root resolvers.
123+ /// - computeContext: A closure used to compute the GraphQL context from incoming requests. This must be provided.
124+ @discardableResult
125+ func graphqlWebSocket< GraphQLContext: Sendable > (
126+ _ path: RouterPath = " graphql " ,
127+ schema: GraphQLSchema ,
128+ rootValue: any Sendable = ( ) ,
129+ computeContext: @Sendable @escaping ( GraphQLContextComputationInputs < Context , Void > ) async throws -> GraphQLContext
130+ ) -> Self {
131+ // This is just an overload that allows not passing `config`, since we cannot use a default argument that
132+ // uses `Context` without getting `error: generic parameter 'Self' could not be inferred` compilation errors
133+ graphqlWebSocket (
134+ path,
135+ schema: schema,
136+ rootValue: rootValue,
137+ config: GraphQLConfig < Context , EmptyWebSocketInit , Void > ( ) ,
138+ computeContext: computeContext
139+ )
140+ }
111141}
0 commit comments