@@ -43,9 +43,8 @@ export async function defineGraphqlWebSocket<
43
43
E extends Record < PropertyKey , unknown > = Record < PropertyKey , never > ,
44
44
> ( options : ServerOptions < P , Extra & Partial < E > > ) : Promise < Partial < Hooks > > {
45
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
- )
46
+ const { makeServer, DEPRECATED_GRAPHQL_WS_PROTOCOL , CloseCode } =
47
+ await import ( 'graphql-ws' )
49
48
const server = makeServer ( options )
50
49
const peers = new WeakMap < Peer , Client > ( )
51
50
return defineWebSocket ( {
@@ -61,20 +60,16 @@ export async function defineGraphqlWebSocket<
61
60
62
61
client . closed = server . opened (
63
62
{
64
- // TODO: use protocol on socket once h3 exposes it
65
- // https://github.com/unjs/crossws/issues/31
66
- protocol : GRAPHQL_TRANSPORT_WS_PROTOCOL ,
63
+ protocol : peer . request . headers ?. get ( 'Sec-WebSocket-Protocol' ) ?? '' ,
67
64
send : ( message ) => {
68
65
// The peer might have been destroyed in the meantime, send only if exists
69
66
if ( peers . has ( peer ) ) {
70
67
peer . send ( message )
71
68
}
72
69
} ,
73
- close : ( _code , _reason ) => {
70
+ close : ( code , reason ) => {
74
71
if ( peers . has ( peer ) ) {
75
- // TODO: No way to close a connection in crossws
76
- // https://github.com/unjs/crossws/issues/23
77
- // peer.close(code, reason);
72
+ peer . close ( code , reason )
78
73
}
79
74
} ,
80
75
onMessage : ( cb ) => ( client . handleMessage = cb ) ,
@@ -91,8 +86,17 @@ export async function defineGraphqlWebSocket<
91
86
close ( peer , details ) {
92
87
const client = peers . get ( peer )
93
88
if ( ! client ) throw new Error ( 'Closing a missing client' )
94
- // TODO: Once h3 exposes the protocol, add a check here for deprecated protocols
95
- // similar to https://github.com/enisdenjo/graphql-ws/blob/6013eb54829b27bd7c598f0985ec80a0e1acf09c/src/use/deno.ts#L109-L116
89
+ const upgradeProtocol = peer . request . headers ?. get (
90
+ 'Sec-WebSocket-Protocol' ,
91
+ )
92
+ if (
93
+ details . code === CloseCode . SubprotocolNotAcceptable &&
94
+ upgradeProtocol === DEPRECATED_GRAPHQL_WS_PROTOCOL
95
+ )
96
+ console . warn (
97
+ `Client provided the unsupported and deprecated subprotocol "${ upgradeProtocol } " used by subscriptions-transport-ws.` +
98
+ 'Please see https://www.apollographql.com/docs/apollo-server/data/subscriptions/#switching-from-subscriptions-transport-ws.' ,
99
+ )
96
100
return client . closed ( details . code , details . reason )
97
101
} ,
98
102
} )
0 commit comments