Skip to content

Commit 62a2088

Browse files
MyshkouskiAlexei Myshkouskitobiasdiez
authored
fix!: exclude websocket features from main entry file (#125)
* fix: exclude websocket features from main entry file * fix: make 'defineGraphqlWebSocket' backward compatible with 1.x.x API since it does not require await import anymore * docs: update readme * fix linter --------- Co-authored-by: Alexei Myshkouski <[email protected]> Co-authored-by: Tobias Diez <[email protected]>
1 parent f79bf78 commit 62a2088

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ Then you can add a WebSocket handler to your `h3` app using the `defineGraphqlWe
9090
```js
9191
import { createApp } from 'h3'
9292
import { ApolloServer } from '@apollo/server'
93-
import {
94-
startServerAndCreateH3Handler,
95-
defineGraphqlWebSocketHandler,
96-
} from '@as-integrations/h3'
93+
import { startServerAndCreateH3Handler } from '@as-integrations/h3'
94+
import { defineGraphqlWebSocketHandler } from '@as-integrations/h3/websocket'
9795
import { makeExecutableSchema } from '@graphql-tools/schema'
9896

9997
// Define your schema and resolvers

examples/websocket.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ app.use(
6464
'/ws',
6565
startServerAndCreateH3Handler(apollo, {
6666
websocket: {
67-
...(await defineGraphqlWebSocket({ schema })),
67+
...defineGraphqlWebSocket({ schema }),
6868
error(peer, error) {
6969
console.error('[ws] error', peer, error)
7070
// In a real app, you would want to properly log this error
@@ -90,7 +90,7 @@ app.use(
9090
app.use(
9191
'/_ws',
9292
defineWebSocketHandler({
93-
...(await defineGraphqlWebSocket({ schema })),
93+
...defineGraphqlWebSocket({ schema }),
9494
error(peer, error) {
9595
console.log('[ws] error', peer, error)
9696
},

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
"types": "./dist/index.d.ts",
1212
"import": "./dist/index.mjs",
1313
"require": "./dist/index.cjs"
14+
},
15+
"./websocket": {
16+
"types": "./dist/websocket.d.ts",
17+
"import": "./dist/websocket.mjs",
18+
"require": "./dist/websocket.cjs"
1419
}
1520
},
1621
"main": "./dist/index.cjs",

src/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ import {
1818
RequestHeaders,
1919
} from 'h3'
2020
import type { WithRequired } from '@apollo/utils.withrequired'
21-
export {
22-
defineGraphqlWebSocket,
23-
defineGraphqlWebSocketHandler,
24-
} from './websocket'
2521

2622
export interface H3ContextFunctionArgument {
2723
event: H3Event

src/websocket.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { ServerOptions, ConnectionInitMessage } from 'graphql-ws'
1+
import {
2+
makeServer,
3+
DEPRECATED_GRAPHQL_WS_PROTOCOL,
4+
CloseCode,
5+
ServerOptions,
6+
ConnectionInitMessage,
7+
} from 'graphql-ws'
28
import {
39
defineWebSocket,
410
defineWebSocketHandler,
@@ -38,13 +44,10 @@ interface Client {
3844
* Use this over {@link defineGraphqlWebSocketHandler} if you need more control over the WebSocket server or
3945
* if you want to add custom hooks (e.g. for authentication or logging).
4046
*/
41-
export async function defineGraphqlWebSocket<
47+
export function defineGraphqlWebSocket<
4248
P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'],
4349
E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>,
44-
>(options: ServerOptions<P, Extra & Partial<E>>): Promise<Partial<Hooks>> {
45-
// Local import since graphql-ws is only an optional peer dependency
46-
const { makeServer, DEPRECATED_GRAPHQL_WS_PROTOCOL, CloseCode } =
47-
await import('graphql-ws')
50+
>(options: ServerOptions<P, Extra & Partial<E>>): Partial<Hooks> {
4851
const server = makeServer(options)
4952
const peers = new WeakMap<Peer, Client>()
5053
return defineWebSocket({
@@ -112,5 +115,5 @@ export async function defineGraphqlWebSocketHandler<
112115
>(
113116
options: ServerOptions<P, Extra & Partial<E>>,
114117
): Promise<EventHandler<EventHandlerRequest, never>> {
115-
return defineWebSocketHandler(await defineGraphqlWebSocket(options))
118+
return defineWebSocketHandler(defineGraphqlWebSocket(options))
116119
}

0 commit comments

Comments
 (0)