@@ -9,12 +9,23 @@ import { schemas } from '#nitro-graphql/server-schemas'
99
1010import { mergeResolvers , mergeTypeDefs } from '@graphql-tools/merge'
1111import { makeExecutableSchema } from '@graphql-tools/schema'
12- import { consola } from 'consola'
1312import defu from 'defu'
1413import { parse , subscribe , validate } from 'graphql'
1514import { createYoga } from 'graphql-yoga'
1615import { defineWebSocketHandler } from 'h3'
1716
17+ // Development-only logging
18+ const isDev = process . env . NODE_ENV === 'development'
19+ function devLog ( message : string , ...args : any [ ] ) {
20+ if ( isDev )
21+ console . log ( message , ...args )
22+ }
23+
24+ // Helper for efficient message sending
25+ function sendMessage ( peer : Peer , message : Record < string , any > ) {
26+ peer . send ( JSON . stringify ( message ) )
27+ }
28+
1829// Conditional imports for federation support
1930let buildSubgraphSchema : any = null
2031
@@ -59,7 +70,7 @@ async function createMergedSchema() {
5970 } )
6071 }
6172 else {
62- console . warn ( 'Federation enabled but @apollo/subgraph not available, falling back to regular schema' )
73+ console . warn ( '[GraphQL WS] Federation enabled but @apollo/subgraph not available, falling back to regular schema' )
6374 schema = makeExecutableSchema ( {
6475 typeDefs,
6576 resolvers : mergedResolvers ,
@@ -85,7 +96,7 @@ async function createMergedSchema() {
8596 return schema
8697 }
8798 catch ( error ) {
88- consola . error ( 'Schema merge error:' , error )
99+ console . error ( '[GraphQL WS] Schema merge error:' , error )
89100 throw error
90101 }
91102}
@@ -133,7 +144,7 @@ export default defineWebSocketHandler({
133144 } ,
134145
135146 async open ( peer ) {
136- consola . info ( '[GraphQL WS] Client connected' )
147+ devLog ( '[GraphQL WS] Client connected' )
137148 peerSubscriptions . set ( peer , new Map ( ) )
138149 } ,
139150
@@ -146,34 +157,28 @@ export default defineWebSocketHandler({
146157 const subscriptions = peerSubscriptions . get ( peer )
147158
148159 if ( ! subscriptions ) {
149- consola . error ( '[GraphQL WS] No subscriptions map found for peer' )
160+ console . error ( '[GraphQL WS] No subscriptions map found for peer' )
150161 return
151162 }
152163
153164 switch ( msg . type ) {
154165 case 'connection_init' : {
155- // Acknowledge connection
156- peer . send ( JSON . stringify ( {
157- type : 'connection_ack' ,
158- } ) )
166+ sendMessage ( peer , { type : 'connection_ack' } )
159167 break
160168 }
161169
162170 case 'ping' : {
163- // Respond with pong
164- peer . send ( JSON . stringify ( {
165- type : 'pong' ,
166- } ) )
171+ sendMessage ( peer , { type : 'pong' } )
167172 break
168173 }
169174
170175 case 'subscribe' : {
171176 if ( ! msg . id || ! msg . payload ) {
172- peer . send ( JSON . stringify ( {
177+ sendMessage ( peer , {
173178 id : msg . id ,
174179 type : 'error' ,
175180 payload : [ { message : 'Invalid subscribe message' } ] ,
176- } ) )
181+ } )
177182 break
178183 }
179184
@@ -185,15 +190,15 @@ export default defineWebSocketHandler({
185190 const validationErrors = validate ( schema , document )
186191
187192 if ( validationErrors . length > 0 ) {
188- peer . send ( JSON . stringify ( {
193+ sendMessage ( peer , {
189194 id : msg . id ,
190195 type : 'error' ,
191196 payload : validationErrors . map ( err => ( {
192197 message : err . message ,
193198 locations : err . locations ,
194199 path : err . path ,
195200 } ) ) ,
196- } ) )
201+ } )
197202 break
198203 }
199204
@@ -214,51 +219,51 @@ export default defineWebSocketHandler({
214219 ; ( async ( ) => {
215220 try {
216221 for await ( const value of result ) {
217- peer . send ( JSON . stringify ( {
222+ sendMessage ( peer , {
218223 id : msg . id ,
219224 type : 'next' ,
220225 payload : value ,
221- } ) )
226+ } )
222227 }
223228
224229 // Subscription completed
225- peer . send ( JSON . stringify ( {
230+ sendMessage ( peer , {
226231 id : msg . id ,
227232 type : 'complete' ,
228- } ) )
233+ } )
229234 subscriptions . delete ( msg . id )
230235 }
231236 catch ( error ) {
232- consola . error ( '[GraphQL WS] Subscription error:' , error )
233- peer . send ( JSON . stringify ( {
237+ console . error ( '[GraphQL WS] Subscription error:' , error )
238+ sendMessage ( peer , {
234239 id : msg . id ,
235240 type : 'error' ,
236241 payload : [ { message : error instanceof Error ? error . message : 'Subscription error' } ] ,
237- } ) )
242+ } )
238243 subscriptions . delete ( msg . id )
239244 }
240245 } ) ( )
241246 }
242247 else {
243248 // It's a regular query/mutation result
244- peer . send ( JSON . stringify ( {
249+ sendMessage ( peer , {
245250 id : msg . id ,
246251 type : 'next' ,
247252 payload : result ,
248- } ) )
249- peer . send ( JSON . stringify ( {
253+ } )
254+ sendMessage ( peer , {
250255 id : msg . id ,
251256 type : 'complete' ,
252- } ) )
257+ } )
253258 }
254259 }
255260 catch ( error ) {
256- consola . error ( '[GraphQL WS] Operation error:' , error )
257- peer . send ( JSON . stringify ( {
261+ console . error ( '[GraphQL WS] Operation error:' , error )
262+ sendMessage ( peer , {
258263 id : msg . id ,
259264 type : 'error' ,
260265 payload : [ { message : error instanceof Error ? error . message : 'Operation failed' } ] ,
261- } ) )
266+ } )
262267 }
263268 break
264269 }
@@ -277,21 +282,21 @@ export default defineWebSocketHandler({
277282 }
278283
279284 default : {
280- consola . warn ( '[GraphQL WS] Unknown message type:' , msg . type )
285+ devLog ( '[GraphQL WS] Unknown message type:' , msg . type )
281286 }
282287 }
283288 }
284289 catch ( error ) {
285- consola . error ( '[GraphQL WS] Message handling error:' , error )
286- peer . send ( JSON . stringify ( {
290+ console . error ( '[GraphQL WS] Message handling error:' , error )
291+ sendMessage ( peer , {
287292 type : 'error' ,
288293 payload : [ { message : 'Invalid message format' } ] ,
289- } ) )
294+ } )
290295 }
291296 } ,
292297
293298 async close ( peer , details ) {
294- consola . info ( '[GraphQL WS] Client disconnected:' , details )
299+ devLog ( '[GraphQL WS] Client disconnected:' , details )
295300
296301 // Clean up all subscriptions for this peer
297302 const subscriptions = peerSubscriptions . get ( peer )
@@ -302,7 +307,7 @@ export default defineWebSocketHandler({
302307 await iterator . return ( )
303308 }
304309 catch ( error ) {
305- consola . error ( `[GraphQL WS] Error cleaning up subscription ${ id } :` , error )
310+ console . error ( `[GraphQL WS] Error cleaning up subscription ${ id } :` , error )
306311 }
307312 }
308313 }
@@ -312,6 +317,6 @@ export default defineWebSocketHandler({
312317 } ,
313318
314319 async error ( peer , error ) {
315- consola . error ( '[GraphQL WS] WebSocket error:' , error )
320+ console . error ( '[GraphQL WS] WebSocket error:' , error )
316321 } ,
317322} )
0 commit comments