@effect-gql/node 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -0
- package/index.cjs +315 -0
- package/index.cjs.map +1 -0
- package/index.d.cts +270 -0
- package/index.d.ts +270 -0
- package/index.js +310 -0
- package/index.js.map +1 -0
- package/package.json +14 -31
- package/dist/config.d.ts +0 -43
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -52
- package/dist/config.js.map +0 -1
- package/dist/graphiql.d.ts +0 -5
- package/dist/graphiql.d.ts.map +0 -1
- package/dist/graphiql.js +0 -43
- package/dist/graphiql.js.map +0 -1
- package/dist/http-utils.d.ts +0 -20
- package/dist/http-utils.d.ts.map +0 -1
- package/dist/http-utils.js +0 -36
- package/dist/http-utils.js.map +0 -1
- package/dist/index.d.ts +0 -5
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -18
- package/dist/index.js.map +0 -1
- package/dist/router.d.ts +0 -32
- package/dist/router.d.ts.map +0 -1
- package/dist/router.js +0 -77
- package/dist/router.js.map +0 -1
- package/dist/schema-builder-extensions.d.ts +0 -31
- package/dist/schema-builder-extensions.d.ts.map +0 -1
- package/dist/schema-builder-extensions.js +0 -35
- package/dist/schema-builder-extensions.js.map +0 -1
- package/dist/serve.d.ts +0 -73
- package/dist/serve.d.ts.map +0 -1
- package/dist/serve.js +0 -176
- package/dist/serve.js.map +0 -1
- package/dist/sse.d.ts +0 -86
- package/dist/sse.d.ts.map +0 -1
- package/dist/sse.js +0 -175
- package/dist/sse.js.map +0 -1
- package/dist/ws.d.ts +0 -97
- package/dist/ws.d.ts.map +0 -1
- package/dist/ws.js +0 -137
- package/dist/ws.js.map +0 -1
- package/src/http-utils.ts +0 -32
- package/src/index.ts +0 -15
- package/src/serve.ts +0 -217
- package/src/sse.ts +0 -234
- package/src/ws.ts +0 -183
package/index.d.cts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { Layer } from 'effect';
|
|
2
|
+
import { HttpRouter } from '@effect/platform';
|
|
3
|
+
import { GraphQLSchema } from 'graphql';
|
|
4
|
+
import { GraphQLWSOptions, EffectWebSocket, GraphQLSSEOptions } from '@effect-gql/core';
|
|
5
|
+
import { IncomingHttpHeaders, IncomingMessage, Server, ServerResponse } from 'node:http';
|
|
6
|
+
import { Duplex } from 'node:stream';
|
|
7
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Configuration for WebSocket subscriptions
|
|
11
|
+
*/
|
|
12
|
+
interface SubscriptionsConfig<R> extends GraphQLWSOptions<R> {
|
|
13
|
+
/**
|
|
14
|
+
* The GraphQL schema (required for subscriptions).
|
|
15
|
+
* Must be the same schema used to create the router.
|
|
16
|
+
*/
|
|
17
|
+
readonly schema: GraphQLSchema;
|
|
18
|
+
/**
|
|
19
|
+
* Path for WebSocket connections.
|
|
20
|
+
* @default "/graphql"
|
|
21
|
+
*/
|
|
22
|
+
readonly path?: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for the Node.js GraphQL server
|
|
26
|
+
*/
|
|
27
|
+
interface ServeOptions<R = never> {
|
|
28
|
+
/** Port to listen on (default: 4000) */
|
|
29
|
+
readonly port?: number;
|
|
30
|
+
/** Hostname to bind to (default: "0.0.0.0") */
|
|
31
|
+
readonly host?: string;
|
|
32
|
+
/** Callback when server starts */
|
|
33
|
+
readonly onStart?: (url: string) => void;
|
|
34
|
+
/**
|
|
35
|
+
* Enable WebSocket subscriptions.
|
|
36
|
+
* When provided, the server will handle WebSocket upgrade requests
|
|
37
|
+
* for GraphQL subscriptions using the graphql-ws protocol.
|
|
38
|
+
*/
|
|
39
|
+
readonly subscriptions?: SubscriptionsConfig<R>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Start a Node.js HTTP server with the given router.
|
|
43
|
+
*
|
|
44
|
+
* This is the main entry point for running a GraphQL server on Node.js.
|
|
45
|
+
* It handles all the Effect runtime setup and server lifecycle.
|
|
46
|
+
*
|
|
47
|
+
* @param router - The HttpRouter to serve (typically from makeGraphQLRouter or toRouter)
|
|
48
|
+
* @param layer - Layer providing the router's service dependencies
|
|
49
|
+
* @param options - Server configuration options
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* import { makeGraphQLRouter } from "@effect-gql/core"
|
|
54
|
+
* import { serve } from "@effect-gql/node"
|
|
55
|
+
*
|
|
56
|
+
* const schema = GraphQLSchemaBuilder.empty
|
|
57
|
+
* .query("hello", { type: S.String, resolve: () => Effect.succeed("world") })
|
|
58
|
+
* .buildSchema()
|
|
59
|
+
*
|
|
60
|
+
* const router = makeGraphQLRouter(schema, Layer.empty, { graphiql: true })
|
|
61
|
+
*
|
|
62
|
+
* // Without subscriptions
|
|
63
|
+
* serve(router, serviceLayer, {
|
|
64
|
+
* port: 4000,
|
|
65
|
+
* onStart: (url) => console.log(`Server running at ${url}`)
|
|
66
|
+
* })
|
|
67
|
+
*
|
|
68
|
+
* // With subscriptions
|
|
69
|
+
* serve(router, serviceLayer, {
|
|
70
|
+
* port: 4000,
|
|
71
|
+
* subscriptions: { schema },
|
|
72
|
+
* onStart: (url) => console.log(`Server running at ${url}`)
|
|
73
|
+
* })
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
declare const serve: <E, R, RE>(router: HttpRouter.HttpRouter<E, R>, layer: Layer.Layer<R, RE>, options?: ServeOptions<R>) => void;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Convert Node.js IncomingHttpHeaders to web standard Headers.
|
|
80
|
+
*
|
|
81
|
+
* This handles the difference between Node.js headers (which can be
|
|
82
|
+
* string | string[] | undefined) and web Headers (which are always strings).
|
|
83
|
+
*
|
|
84
|
+
* @param nodeHeaders - Headers from IncomingMessage.headers
|
|
85
|
+
* @returns A web standard Headers object
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* import { toWebHeaders } from "@effect-gql/node"
|
|
90
|
+
*
|
|
91
|
+
* const webHeaders = toWebHeaders(req.headers)
|
|
92
|
+
* const auth = webHeaders.get("authorization")
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare const toWebHeaders: (nodeHeaders: IncomingHttpHeaders) => Headers;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Options for Node.js WebSocket server
|
|
99
|
+
*/
|
|
100
|
+
interface NodeWSOptions<R> extends GraphQLWSOptions<R> {
|
|
101
|
+
/**
|
|
102
|
+
* Path for WebSocket connections.
|
|
103
|
+
* @default "/graphql"
|
|
104
|
+
*/
|
|
105
|
+
readonly path?: string;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Convert a Node.js WebSocket (from 'ws' library) to an EffectWebSocket.
|
|
109
|
+
*
|
|
110
|
+
* This creates an Effect-based wrapper around the ws WebSocket instance,
|
|
111
|
+
* providing a Stream for incoming messages and Effect-based send/close operations.
|
|
112
|
+
*
|
|
113
|
+
* @param ws - The WebSocket instance from the 'ws' library
|
|
114
|
+
* @returns An EffectWebSocket that can be used with makeGraphQLWSHandler
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* wss.on("connection", (ws, req) => {
|
|
119
|
+
* const effectSocket = toEffectWebSocket(ws)
|
|
120
|
+
* Effect.runPromise(handler(effectSocket))
|
|
121
|
+
* })
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
declare const toEffectWebSocket: (ws: WebSocket) => EffectWebSocket;
|
|
125
|
+
/**
|
|
126
|
+
* Create a WebSocket server that handles GraphQL subscriptions.
|
|
127
|
+
*
|
|
128
|
+
* This function creates a WebSocketServer and returns utilities for
|
|
129
|
+
* integrating it with an HTTP server via the upgrade event.
|
|
130
|
+
*
|
|
131
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
132
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
133
|
+
* @param options - Optional configuration and lifecycle hooks
|
|
134
|
+
* @returns Object containing the WebSocketServer and handlers
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const httpServer = createServer(requestHandler)
|
|
139
|
+
* const { wss, handleUpgrade } = createGraphQLWSServer(schema, serviceLayer)
|
|
140
|
+
*
|
|
141
|
+
* httpServer.on("upgrade", (request, socket, head) => {
|
|
142
|
+
* if (request.url === "/graphql") {
|
|
143
|
+
* handleUpgrade(request, socket, head)
|
|
144
|
+
* }
|
|
145
|
+
* })
|
|
146
|
+
*
|
|
147
|
+
* httpServer.listen(4000)
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
declare const createGraphQLWSServer: <R>(schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeWSOptions<R>) => {
|
|
151
|
+
/** The underlying WebSocketServer instance */
|
|
152
|
+
wss: WebSocketServer;
|
|
153
|
+
/** Handle HTTP upgrade requests */
|
|
154
|
+
handleUpgrade: (request: IncomingMessage, socket: Duplex, head: Buffer) => void;
|
|
155
|
+
/** Close the WebSocket server */
|
|
156
|
+
close: () => Promise<void>;
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Attach WebSocket subscription support to an existing HTTP server.
|
|
160
|
+
*
|
|
161
|
+
* This is a convenience function that creates a GraphQL WebSocket server
|
|
162
|
+
* and attaches it to an HTTP server's upgrade event.
|
|
163
|
+
*
|
|
164
|
+
* @param server - The HTTP server to attach to
|
|
165
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
166
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
167
|
+
* @param options - Optional configuration and lifecycle hooks
|
|
168
|
+
* @returns Cleanup function to close the WebSocket server
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* const httpServer = createServer(requestHandler)
|
|
173
|
+
*
|
|
174
|
+
* const cleanup = attachWebSocketToServer(httpServer, schema, serviceLayer, {
|
|
175
|
+
* path: "/graphql",
|
|
176
|
+
* })
|
|
177
|
+
*
|
|
178
|
+
* httpServer.listen(4000)
|
|
179
|
+
*
|
|
180
|
+
* // Later, to cleanup:
|
|
181
|
+
* await cleanup()
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
declare const attachWebSocketToServer: <R>(server: Server, schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeWSOptions<R>) => {
|
|
185
|
+
close: () => Promise<void>;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Options for Node.js SSE handler
|
|
190
|
+
*/
|
|
191
|
+
interface NodeSSEOptions<R> extends GraphQLSSEOptions<R> {
|
|
192
|
+
/**
|
|
193
|
+
* Path for SSE connections.
|
|
194
|
+
* @default "/graphql/stream"
|
|
195
|
+
*/
|
|
196
|
+
readonly path?: string;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Create an SSE handler for Node.js HTTP server.
|
|
200
|
+
*
|
|
201
|
+
* This function creates a handler that can process SSE subscription requests.
|
|
202
|
+
* It handles:
|
|
203
|
+
* - Parsing the GraphQL subscription request from the HTTP body
|
|
204
|
+
* - Setting up the SSE connection with proper headers
|
|
205
|
+
* - Streaming subscription events to the client
|
|
206
|
+
* - Detecting client disconnection and cleaning up
|
|
207
|
+
*
|
|
208
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
209
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
210
|
+
* @param options - Optional lifecycle hooks and configuration
|
|
211
|
+
* @returns A request handler function
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* import { createServer } from "node:http"
|
|
216
|
+
* import { createSSEHandler } from "@effect-gql/node"
|
|
217
|
+
*
|
|
218
|
+
* const sseHandler = createSSEHandler(schema, serviceLayer, {
|
|
219
|
+
* path: "/graphql/stream",
|
|
220
|
+
* onConnect: (request, headers) => Effect.gen(function* () {
|
|
221
|
+
* const user = yield* AuthService.validateToken(headers.get("authorization"))
|
|
222
|
+
* return { user }
|
|
223
|
+
* }),
|
|
224
|
+
* })
|
|
225
|
+
*
|
|
226
|
+
* const server = createServer((req, res) => {
|
|
227
|
+
* const url = new URL(req.url, `http://${req.headers.host}`)
|
|
228
|
+
* if (url.pathname === "/graphql/stream" && req.method === "POST") {
|
|
229
|
+
* sseHandler(req, res)
|
|
230
|
+
* } else {
|
|
231
|
+
* // Handle other requests...
|
|
232
|
+
* }
|
|
233
|
+
* })
|
|
234
|
+
*
|
|
235
|
+
* server.listen(4000)
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
declare const createSSEHandler: <R>(schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeSSEOptions<R>) => ((req: IncomingMessage, res: ServerResponse) => Promise<void>);
|
|
239
|
+
/**
|
|
240
|
+
* Create SSE middleware that can be used with the serve() function.
|
|
241
|
+
*
|
|
242
|
+
* This returns an object that can be used to integrate SSE subscriptions
|
|
243
|
+
* with the HTTP server when using the custom subscription mode.
|
|
244
|
+
*
|
|
245
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
246
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
247
|
+
* @param options - Optional lifecycle hooks and configuration
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* // In serve.ts with custom HTTP server setup
|
|
252
|
+
* const sseServer = createSSEServer(schema, layer, { path: "/graphql/stream" })
|
|
253
|
+
*
|
|
254
|
+
* httpServer.on("request", (req, res) => {
|
|
255
|
+
* if (sseServer.shouldHandle(req)) {
|
|
256
|
+
* sseServer.handle(req, res)
|
|
257
|
+
* }
|
|
258
|
+
* })
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
declare const createSSEServer: <R>(schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeSSEOptions<R>) => {
|
|
262
|
+
/** Path this SSE server handles */
|
|
263
|
+
readonly path: string;
|
|
264
|
+
/** Check if a request should be handled by this SSE server */
|
|
265
|
+
shouldHandle: (req: IncomingMessage) => boolean;
|
|
266
|
+
/** Handle an SSE request */
|
|
267
|
+
handle: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
export { type NodeSSEOptions, type NodeWSOptions, type ServeOptions, attachWebSocketToServer, createGraphQLWSServer, createSSEHandler, createSSEServer, serve, toEffectWebSocket, toWebHeaders };
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { Layer } from 'effect';
|
|
2
|
+
import { HttpRouter } from '@effect/platform';
|
|
3
|
+
import { GraphQLSchema } from 'graphql';
|
|
4
|
+
import { GraphQLWSOptions, EffectWebSocket, GraphQLSSEOptions } from '@effect-gql/core';
|
|
5
|
+
import { IncomingHttpHeaders, IncomingMessage, Server, ServerResponse } from 'node:http';
|
|
6
|
+
import { Duplex } from 'node:stream';
|
|
7
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Configuration for WebSocket subscriptions
|
|
11
|
+
*/
|
|
12
|
+
interface SubscriptionsConfig<R> extends GraphQLWSOptions<R> {
|
|
13
|
+
/**
|
|
14
|
+
* The GraphQL schema (required for subscriptions).
|
|
15
|
+
* Must be the same schema used to create the router.
|
|
16
|
+
*/
|
|
17
|
+
readonly schema: GraphQLSchema;
|
|
18
|
+
/**
|
|
19
|
+
* Path for WebSocket connections.
|
|
20
|
+
* @default "/graphql"
|
|
21
|
+
*/
|
|
22
|
+
readonly path?: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for the Node.js GraphQL server
|
|
26
|
+
*/
|
|
27
|
+
interface ServeOptions<R = never> {
|
|
28
|
+
/** Port to listen on (default: 4000) */
|
|
29
|
+
readonly port?: number;
|
|
30
|
+
/** Hostname to bind to (default: "0.0.0.0") */
|
|
31
|
+
readonly host?: string;
|
|
32
|
+
/** Callback when server starts */
|
|
33
|
+
readonly onStart?: (url: string) => void;
|
|
34
|
+
/**
|
|
35
|
+
* Enable WebSocket subscriptions.
|
|
36
|
+
* When provided, the server will handle WebSocket upgrade requests
|
|
37
|
+
* for GraphQL subscriptions using the graphql-ws protocol.
|
|
38
|
+
*/
|
|
39
|
+
readonly subscriptions?: SubscriptionsConfig<R>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Start a Node.js HTTP server with the given router.
|
|
43
|
+
*
|
|
44
|
+
* This is the main entry point for running a GraphQL server on Node.js.
|
|
45
|
+
* It handles all the Effect runtime setup and server lifecycle.
|
|
46
|
+
*
|
|
47
|
+
* @param router - The HttpRouter to serve (typically from makeGraphQLRouter or toRouter)
|
|
48
|
+
* @param layer - Layer providing the router's service dependencies
|
|
49
|
+
* @param options - Server configuration options
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* import { makeGraphQLRouter } from "@effect-gql/core"
|
|
54
|
+
* import { serve } from "@effect-gql/node"
|
|
55
|
+
*
|
|
56
|
+
* const schema = GraphQLSchemaBuilder.empty
|
|
57
|
+
* .query("hello", { type: S.String, resolve: () => Effect.succeed("world") })
|
|
58
|
+
* .buildSchema()
|
|
59
|
+
*
|
|
60
|
+
* const router = makeGraphQLRouter(schema, Layer.empty, { graphiql: true })
|
|
61
|
+
*
|
|
62
|
+
* // Without subscriptions
|
|
63
|
+
* serve(router, serviceLayer, {
|
|
64
|
+
* port: 4000,
|
|
65
|
+
* onStart: (url) => console.log(`Server running at ${url}`)
|
|
66
|
+
* })
|
|
67
|
+
*
|
|
68
|
+
* // With subscriptions
|
|
69
|
+
* serve(router, serviceLayer, {
|
|
70
|
+
* port: 4000,
|
|
71
|
+
* subscriptions: { schema },
|
|
72
|
+
* onStart: (url) => console.log(`Server running at ${url}`)
|
|
73
|
+
* })
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
declare const serve: <E, R, RE>(router: HttpRouter.HttpRouter<E, R>, layer: Layer.Layer<R, RE>, options?: ServeOptions<R>) => void;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Convert Node.js IncomingHttpHeaders to web standard Headers.
|
|
80
|
+
*
|
|
81
|
+
* This handles the difference between Node.js headers (which can be
|
|
82
|
+
* string | string[] | undefined) and web Headers (which are always strings).
|
|
83
|
+
*
|
|
84
|
+
* @param nodeHeaders - Headers from IncomingMessage.headers
|
|
85
|
+
* @returns A web standard Headers object
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* import { toWebHeaders } from "@effect-gql/node"
|
|
90
|
+
*
|
|
91
|
+
* const webHeaders = toWebHeaders(req.headers)
|
|
92
|
+
* const auth = webHeaders.get("authorization")
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare const toWebHeaders: (nodeHeaders: IncomingHttpHeaders) => Headers;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Options for Node.js WebSocket server
|
|
99
|
+
*/
|
|
100
|
+
interface NodeWSOptions<R> extends GraphQLWSOptions<R> {
|
|
101
|
+
/**
|
|
102
|
+
* Path for WebSocket connections.
|
|
103
|
+
* @default "/graphql"
|
|
104
|
+
*/
|
|
105
|
+
readonly path?: string;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Convert a Node.js WebSocket (from 'ws' library) to an EffectWebSocket.
|
|
109
|
+
*
|
|
110
|
+
* This creates an Effect-based wrapper around the ws WebSocket instance,
|
|
111
|
+
* providing a Stream for incoming messages and Effect-based send/close operations.
|
|
112
|
+
*
|
|
113
|
+
* @param ws - The WebSocket instance from the 'ws' library
|
|
114
|
+
* @returns An EffectWebSocket that can be used with makeGraphQLWSHandler
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* wss.on("connection", (ws, req) => {
|
|
119
|
+
* const effectSocket = toEffectWebSocket(ws)
|
|
120
|
+
* Effect.runPromise(handler(effectSocket))
|
|
121
|
+
* })
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
declare const toEffectWebSocket: (ws: WebSocket) => EffectWebSocket;
|
|
125
|
+
/**
|
|
126
|
+
* Create a WebSocket server that handles GraphQL subscriptions.
|
|
127
|
+
*
|
|
128
|
+
* This function creates a WebSocketServer and returns utilities for
|
|
129
|
+
* integrating it with an HTTP server via the upgrade event.
|
|
130
|
+
*
|
|
131
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
132
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
133
|
+
* @param options - Optional configuration and lifecycle hooks
|
|
134
|
+
* @returns Object containing the WebSocketServer and handlers
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const httpServer = createServer(requestHandler)
|
|
139
|
+
* const { wss, handleUpgrade } = createGraphQLWSServer(schema, serviceLayer)
|
|
140
|
+
*
|
|
141
|
+
* httpServer.on("upgrade", (request, socket, head) => {
|
|
142
|
+
* if (request.url === "/graphql") {
|
|
143
|
+
* handleUpgrade(request, socket, head)
|
|
144
|
+
* }
|
|
145
|
+
* })
|
|
146
|
+
*
|
|
147
|
+
* httpServer.listen(4000)
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
declare const createGraphQLWSServer: <R>(schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeWSOptions<R>) => {
|
|
151
|
+
/** The underlying WebSocketServer instance */
|
|
152
|
+
wss: WebSocketServer;
|
|
153
|
+
/** Handle HTTP upgrade requests */
|
|
154
|
+
handleUpgrade: (request: IncomingMessage, socket: Duplex, head: Buffer) => void;
|
|
155
|
+
/** Close the WebSocket server */
|
|
156
|
+
close: () => Promise<void>;
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Attach WebSocket subscription support to an existing HTTP server.
|
|
160
|
+
*
|
|
161
|
+
* This is a convenience function that creates a GraphQL WebSocket server
|
|
162
|
+
* and attaches it to an HTTP server's upgrade event.
|
|
163
|
+
*
|
|
164
|
+
* @param server - The HTTP server to attach to
|
|
165
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
166
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
167
|
+
* @param options - Optional configuration and lifecycle hooks
|
|
168
|
+
* @returns Cleanup function to close the WebSocket server
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* const httpServer = createServer(requestHandler)
|
|
173
|
+
*
|
|
174
|
+
* const cleanup = attachWebSocketToServer(httpServer, schema, serviceLayer, {
|
|
175
|
+
* path: "/graphql",
|
|
176
|
+
* })
|
|
177
|
+
*
|
|
178
|
+
* httpServer.listen(4000)
|
|
179
|
+
*
|
|
180
|
+
* // Later, to cleanup:
|
|
181
|
+
* await cleanup()
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
declare const attachWebSocketToServer: <R>(server: Server, schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeWSOptions<R>) => {
|
|
185
|
+
close: () => Promise<void>;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Options for Node.js SSE handler
|
|
190
|
+
*/
|
|
191
|
+
interface NodeSSEOptions<R> extends GraphQLSSEOptions<R> {
|
|
192
|
+
/**
|
|
193
|
+
* Path for SSE connections.
|
|
194
|
+
* @default "/graphql/stream"
|
|
195
|
+
*/
|
|
196
|
+
readonly path?: string;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Create an SSE handler for Node.js HTTP server.
|
|
200
|
+
*
|
|
201
|
+
* This function creates a handler that can process SSE subscription requests.
|
|
202
|
+
* It handles:
|
|
203
|
+
* - Parsing the GraphQL subscription request from the HTTP body
|
|
204
|
+
* - Setting up the SSE connection with proper headers
|
|
205
|
+
* - Streaming subscription events to the client
|
|
206
|
+
* - Detecting client disconnection and cleaning up
|
|
207
|
+
*
|
|
208
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
209
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
210
|
+
* @param options - Optional lifecycle hooks and configuration
|
|
211
|
+
* @returns A request handler function
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* import { createServer } from "node:http"
|
|
216
|
+
* import { createSSEHandler } from "@effect-gql/node"
|
|
217
|
+
*
|
|
218
|
+
* const sseHandler = createSSEHandler(schema, serviceLayer, {
|
|
219
|
+
* path: "/graphql/stream",
|
|
220
|
+
* onConnect: (request, headers) => Effect.gen(function* () {
|
|
221
|
+
* const user = yield* AuthService.validateToken(headers.get("authorization"))
|
|
222
|
+
* return { user }
|
|
223
|
+
* }),
|
|
224
|
+
* })
|
|
225
|
+
*
|
|
226
|
+
* const server = createServer((req, res) => {
|
|
227
|
+
* const url = new URL(req.url, `http://${req.headers.host}`)
|
|
228
|
+
* if (url.pathname === "/graphql/stream" && req.method === "POST") {
|
|
229
|
+
* sseHandler(req, res)
|
|
230
|
+
* } else {
|
|
231
|
+
* // Handle other requests...
|
|
232
|
+
* }
|
|
233
|
+
* })
|
|
234
|
+
*
|
|
235
|
+
* server.listen(4000)
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
declare const createSSEHandler: <R>(schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeSSEOptions<R>) => ((req: IncomingMessage, res: ServerResponse) => Promise<void>);
|
|
239
|
+
/**
|
|
240
|
+
* Create SSE middleware that can be used with the serve() function.
|
|
241
|
+
*
|
|
242
|
+
* This returns an object that can be used to integrate SSE subscriptions
|
|
243
|
+
* with the HTTP server when using the custom subscription mode.
|
|
244
|
+
*
|
|
245
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
246
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
247
|
+
* @param options - Optional lifecycle hooks and configuration
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* // In serve.ts with custom HTTP server setup
|
|
252
|
+
* const sseServer = createSSEServer(schema, layer, { path: "/graphql/stream" })
|
|
253
|
+
*
|
|
254
|
+
* httpServer.on("request", (req, res) => {
|
|
255
|
+
* if (sseServer.shouldHandle(req)) {
|
|
256
|
+
* sseServer.handle(req, res)
|
|
257
|
+
* }
|
|
258
|
+
* })
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
declare const createSSEServer: <R>(schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeSSEOptions<R>) => {
|
|
262
|
+
/** Path this SSE server handles */
|
|
263
|
+
readonly path: string;
|
|
264
|
+
/** Check if a request should be handled by this SSE server */
|
|
265
|
+
shouldHandle: (req: IncomingMessage) => boolean;
|
|
266
|
+
/** Handle an SSE request */
|
|
267
|
+
handle: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
export { type NodeSSEOptions, type NodeWSOptions, type ServeOptions, attachWebSocketToServer, createGraphQLWSServer, createSSEHandler, createSSEServer, serve, toEffectWebSocket, toWebHeaders };
|