|
1 |
| -import { |
2 |
| - makeServer, |
3 |
| - ServerOptions, |
4 |
| - ConnectionInitMessage, |
5 |
| - GRAPHQL_TRANSPORT_WS_PROTOCOL, |
6 |
| -} from 'graphql-ws' |
| 1 | +import type { ServerOptions, ConnectionInitMessage } from 'graphql-ws' |
7 | 2 | import {
|
8 | 3 | defineWebSocket,
|
9 | 4 | defineWebSocketHandler,
|
@@ -43,10 +38,14 @@ interface Client {
|
43 | 38 | * Use this over {@link defineGraphqlWebSocketHandler} if you need more control over the WebSocket server or
|
44 | 39 | * if you want to add custom hooks (e.g. for authentication or logging).
|
45 | 40 | */
|
46 |
| -export function defineGraphqlWebSocket< |
| 41 | +export async function defineGraphqlWebSocket< |
47 | 42 | P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'],
|
48 | 43 | E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>,
|
49 |
| ->(options: ServerOptions<P, Extra & Partial<E>>): Partial<Hooks> { |
| 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, GRAPHQL_TRANSPORT_WS_PROTOCOL } = await import( |
| 47 | + 'graphql-ws' |
| 48 | + ) |
50 | 49 | const server = makeServer(options)
|
51 | 50 | const peers = new WeakMap<Peer, Client>()
|
52 | 51 | return defineWebSocket({
|
@@ -103,11 +102,11 @@ export function defineGraphqlWebSocket<
|
103 | 102 | *
|
104 | 103 | * @category Server/h3
|
105 | 104 | */
|
106 |
| -export function defineGraphqlWebSocketHandler< |
| 105 | +export async function defineGraphqlWebSocketHandler< |
107 | 106 | P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'],
|
108 | 107 | E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>,
|
109 | 108 | >(
|
110 | 109 | options: ServerOptions<P, Extra & Partial<E>>,
|
111 |
| -): EventHandler<EventHandlerRequest, never> { |
112 |
| - return defineWebSocketHandler(defineGraphqlWebSocket(options)) |
| 110 | +): Promise<EventHandler<EventHandlerRequest, never>> { |
| 111 | + return defineWebSocketHandler(await defineGraphqlWebSocket(options)) |
113 | 112 | }
|
0 commit comments