@as-integrations/h3 1.1.6 → 1.2.1
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 +61 -8
- package/dist/index.cjs +78 -22
- package/dist/index.d.cts +30 -2
- package/dist/index.d.mts +30 -2
- package/dist/index.d.ts +30 -2
- package/dist/index.mjs +78 -24
- package/package.json +40 -21
package/README.md
CHANGED
|
@@ -31,12 +31,14 @@ import { ApolloServer } from '@apollo/server'
|
|
|
31
31
|
import { startServerAndCreateH3Handler } from '@as-integrations/h3'
|
|
32
32
|
|
|
33
33
|
const apollo = new ApolloServer({
|
|
34
|
-
// Specify server options here
|
|
34
|
+
// Specify server options like schema and resolvers here
|
|
35
35
|
})
|
|
36
36
|
|
|
37
37
|
export default startServerAndCreateH3Handler(apollo, {
|
|
38
38
|
// Optional: Specify context
|
|
39
|
-
context: (event) => {
|
|
39
|
+
context: (event) => {
|
|
40
|
+
/*...*/
|
|
41
|
+
},
|
|
40
42
|
})
|
|
41
43
|
```
|
|
42
44
|
|
|
@@ -45,26 +47,77 @@ export default startServerAndCreateH3Handler(apollo, {
|
|
|
45
47
|
Create and configure an instance of Apollo Server as described in the [documentation](https://www.apollographql.com/docs/apollo-server/getting-started#step-6-create-an-instance-of-apolloserver) and then register it as a route handler in your `h3` application.
|
|
46
48
|
|
|
47
49
|
```js
|
|
48
|
-
import { createApp
|
|
50
|
+
import { createApp } from 'h3'
|
|
49
51
|
import { ApolloServer } from '@apollo/server'
|
|
50
52
|
import { startServerAndCreateH3Handler } from '@as-integrations/h3'
|
|
51
53
|
|
|
52
54
|
const apollo = new ApolloServer({
|
|
53
|
-
// Specify server options here
|
|
55
|
+
// Specify server options like schema and resolvers here
|
|
54
56
|
})
|
|
55
57
|
|
|
56
|
-
const app = createApp()
|
|
58
|
+
export const app = createApp()
|
|
57
59
|
app.use(
|
|
58
60
|
'/api',
|
|
59
61
|
startServerAndCreateH3Handler(apollo, {
|
|
60
62
|
// Optional: Specify context
|
|
61
|
-
context: (event) => {
|
|
62
|
-
|
|
63
|
+
context: (event) => {
|
|
64
|
+
/*...*/
|
|
65
|
+
},
|
|
66
|
+
}),
|
|
63
67
|
)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Then run your h3 server as usual, e.g. with `npx --yes listhen -w --open ./app.ts`.
|
|
71
|
+
Visit http://localhost:3000/api in your browser to access the Apollo Sandbox.
|
|
72
|
+
|
|
73
|
+
## Subscriptions with WebSockets
|
|
64
74
|
|
|
65
|
-
|
|
75
|
+
This package also supports subscriptions over WebSockets. To enable this feature, you need to install the `graphql-ws` package:
|
|
76
|
+
|
|
77
|
+
```sh
|
|
78
|
+
# npm
|
|
79
|
+
npm install graphql-ws
|
|
80
|
+
|
|
81
|
+
# yarn
|
|
82
|
+
yarn add graphql-ws
|
|
83
|
+
|
|
84
|
+
# pnpm
|
|
85
|
+
pnpm add graphql-ws
|
|
66
86
|
```
|
|
67
87
|
|
|
88
|
+
Then you can add a WebSocket handler to your `h3` app using the `defineGraphqlWebSocketHandler` or `defineGraphqlWebSocket` functions from this package. Here is an example that combines the HTTP and WebSocket handlers in a single app.
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
import { createApp } from 'h3'
|
|
92
|
+
import { ApolloServer } from '@apollo/server'
|
|
93
|
+
import {
|
|
94
|
+
startServerAndCreateH3Handler,
|
|
95
|
+
defineGraphqlWebSocketHandler,
|
|
96
|
+
} from '@as-integrations/h3'
|
|
97
|
+
import { makeExecutableSchema } from '@graphql-tools/schema'
|
|
98
|
+
|
|
99
|
+
// Define your schema and resolvers
|
|
100
|
+
const typeDefs = `...`
|
|
101
|
+
const resolvers = {
|
|
102
|
+
/*...*/
|
|
103
|
+
}
|
|
104
|
+
const schema = makeExecutableSchema({ typeDefs, resolvers })
|
|
105
|
+
|
|
106
|
+
const apollo = new ApolloServer({ schema })
|
|
107
|
+
|
|
108
|
+
export const app = createApp()
|
|
109
|
+
app.use(
|
|
110
|
+
'/api',
|
|
111
|
+
startServerAndCreateH3Handler(apollo, {
|
|
112
|
+
websocket: defineGraphqlWebSocketHandler({ schema }),
|
|
113
|
+
}),
|
|
114
|
+
)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Then you can connect to the WebSocket endpoint using the Apollo Sandbox or any other client that supports the `graphql-ws` protocol.
|
|
118
|
+
|
|
119
|
+
See the [WebSocket example](./examples/websocket.ts) for a complete example.
|
|
120
|
+
|
|
68
121
|
## 💻 Development
|
|
69
122
|
|
|
70
123
|
- Clone this repository
|
package/dist/index.cjs
CHANGED
|
@@ -2,35 +2,89 @@
|
|
|
2
2
|
|
|
3
3
|
const server = require('@apollo/server');
|
|
4
4
|
const h3 = require('h3');
|
|
5
|
+
const graphqlWs = require('graphql-ws');
|
|
6
|
+
|
|
7
|
+
function defineGraphqlWebSocket(options) {
|
|
8
|
+
const server = graphqlWs.makeServer(options);
|
|
9
|
+
const peers = /* @__PURE__ */ new WeakMap();
|
|
10
|
+
return h3.defineWebSocket({
|
|
11
|
+
open(peer) {
|
|
12
|
+
const client = {
|
|
13
|
+
handleMessage: () => {
|
|
14
|
+
throw new Error("Message received before handler was registered");
|
|
15
|
+
},
|
|
16
|
+
closed: () => {
|
|
17
|
+
throw new Error("Closed before handler was registered");
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
client.closed = server.opened(
|
|
21
|
+
{
|
|
22
|
+
// TODO: use protocol on socket once h3 exposes it
|
|
23
|
+
// https://github.com/unjs/crossws/issues/31
|
|
24
|
+
protocol: graphqlWs.GRAPHQL_TRANSPORT_WS_PROTOCOL,
|
|
25
|
+
send: (message) => {
|
|
26
|
+
if (peers.has(peer)) {
|
|
27
|
+
peer.send(message);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
close: (_code, _reason) => {
|
|
31
|
+
if (peers.has(peer)) ;
|
|
32
|
+
},
|
|
33
|
+
onMessage: (cb) => client.handleMessage = cb
|
|
34
|
+
},
|
|
35
|
+
{ peer }
|
|
36
|
+
);
|
|
37
|
+
peers.set(peer, client);
|
|
38
|
+
},
|
|
39
|
+
message(peer, message) {
|
|
40
|
+
const client = peers.get(peer);
|
|
41
|
+
if (!client)
|
|
42
|
+
throw new Error("Message received for a missing client");
|
|
43
|
+
return client.handleMessage(message.text());
|
|
44
|
+
},
|
|
45
|
+
close(peer, details) {
|
|
46
|
+
const client = peers.get(peer);
|
|
47
|
+
if (!client)
|
|
48
|
+
throw new Error("Closing a missing client");
|
|
49
|
+
return client.closed(details.code ?? 1e3, details.reason ?? "");
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function defineGraphqlWebSocketHandler(options) {
|
|
54
|
+
return h3.defineWebSocketHandler(defineGraphqlWebSocket(options));
|
|
55
|
+
}
|
|
5
56
|
|
|
6
57
|
function startServerAndCreateH3Handler(server, options) {
|
|
7
58
|
server.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
|
|
8
59
|
const defaultContext = () => Promise.resolve({});
|
|
9
60
|
const contextFunction = options?.context ?? defaultContext;
|
|
10
|
-
return h3.eventHandler(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
try {
|
|
15
|
-
const graphqlRequest = await toGraphqlRequest(event);
|
|
16
|
-
const { body, headers, status } = await server.executeHTTPGraphQLRequest({
|
|
17
|
-
httpGraphQLRequest: graphqlRequest,
|
|
18
|
-
context: () => contextFunction({ event })
|
|
19
|
-
});
|
|
20
|
-
if (body.kind === "chunked") {
|
|
21
|
-
throw new Error("Incremental delivery not implemented");
|
|
61
|
+
return h3.eventHandler({
|
|
62
|
+
async handler(event) {
|
|
63
|
+
if (h3.isMethod(event, "OPTIONS")) {
|
|
64
|
+
return null;
|
|
22
65
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
66
|
+
try {
|
|
67
|
+
const graphqlRequest = await toGraphqlRequest(event);
|
|
68
|
+
const { body, headers, status } = await server.executeHTTPGraphQLRequest({
|
|
69
|
+
httpGraphQLRequest: graphqlRequest,
|
|
70
|
+
context: () => contextFunction({ event })
|
|
71
|
+
});
|
|
72
|
+
if (body.kind === "chunked") {
|
|
73
|
+
throw new Error("Incremental delivery not implemented");
|
|
74
|
+
}
|
|
75
|
+
h3.setHeaders(event, Object.fromEntries(headers));
|
|
76
|
+
event.res.statusCode = status || 200;
|
|
77
|
+
return body.string;
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (error instanceof SyntaxError) {
|
|
80
|
+
event.res.statusCode = 400;
|
|
81
|
+
return error.message;
|
|
82
|
+
} else {
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
32
85
|
}
|
|
33
|
-
}
|
|
86
|
+
},
|
|
87
|
+
websocket: options?.websocket
|
|
34
88
|
});
|
|
35
89
|
}
|
|
36
90
|
async function toGraphqlRequest(event) {
|
|
@@ -65,4 +119,6 @@ async function normalizeBody(event) {
|
|
|
65
119
|
}
|
|
66
120
|
}
|
|
67
121
|
|
|
122
|
+
exports.defineGraphqlWebSocket = defineGraphqlWebSocket;
|
|
123
|
+
exports.defineGraphqlWebSocketHandler = defineGraphqlWebSocketHandler;
|
|
68
124
|
exports.startServerAndCreateH3Handler = startServerAndCreateH3Handler;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
import { BaseContext, ContextFunction, ApolloServer } from '@apollo/server';
|
|
2
|
-
import {
|
|
2
|
+
import { Hooks, Peer } from 'crossws';
|
|
3
|
+
import { EventHandler, EventHandlerRequest, H3Event } from 'h3';
|
|
4
|
+
import { ConnectionInitMessage, ServerOptions } from 'graphql-ws';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The extra that will be put in the `Context`.
|
|
8
|
+
*
|
|
9
|
+
* @category Server/h3
|
|
10
|
+
*/
|
|
11
|
+
interface Extra {
|
|
12
|
+
/**
|
|
13
|
+
* The underlying WebSocket peer.
|
|
14
|
+
*/
|
|
15
|
+
readonly peer: Peer;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create the WebSocket hooks to be used with [h3](https://h3.unjs.io/).
|
|
19
|
+
*
|
|
20
|
+
* Use this over {@link defineGraphqlWebSocketHandler} if you need more control over the WebSocket server or
|
|
21
|
+
* if you want to add custom hooks (e.g. for authentication or logging).
|
|
22
|
+
*/
|
|
23
|
+
declare function defineGraphqlWebSocket<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Partial<Hooks>;
|
|
24
|
+
/**
|
|
25
|
+
* Create a event handler to be used with [h3](https://h3.unjs.io/).
|
|
26
|
+
*
|
|
27
|
+
* @category Server/h3
|
|
28
|
+
*/
|
|
29
|
+
declare function defineGraphqlWebSocketHandler<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): EventHandler<EventHandlerRequest, never>;
|
|
3
30
|
|
|
4
31
|
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
5
32
|
interface H3ContextFunctionArgument {
|
|
@@ -7,8 +34,9 @@ interface H3ContextFunctionArgument {
|
|
|
7
34
|
}
|
|
8
35
|
interface H3HandlerOptions<TContext extends BaseContext> {
|
|
9
36
|
context?: ContextFunction<[H3ContextFunctionArgument], TContext>;
|
|
37
|
+
websocket?: Partial<Hooks>;
|
|
10
38
|
}
|
|
11
39
|
declare function startServerAndCreateH3Handler(server: ApolloServer<BaseContext>, options?: H3HandlerOptions<BaseContext>): EventHandler;
|
|
12
40
|
declare function startServerAndCreateH3Handler<TContext extends BaseContext>(server: ApolloServer<TContext>, options: WithRequired<H3HandlerOptions<TContext>, 'context'>): EventHandler;
|
|
13
41
|
|
|
14
|
-
export { type H3ContextFunctionArgument, type H3HandlerOptions, startServerAndCreateH3Handler };
|
|
42
|
+
export { type H3ContextFunctionArgument, type H3HandlerOptions, defineGraphqlWebSocket, defineGraphqlWebSocketHandler, startServerAndCreateH3Handler };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
import { BaseContext, ContextFunction, ApolloServer } from '@apollo/server';
|
|
2
|
-
import {
|
|
2
|
+
import { Hooks, Peer } from 'crossws';
|
|
3
|
+
import { EventHandler, EventHandlerRequest, H3Event } from 'h3';
|
|
4
|
+
import { ConnectionInitMessage, ServerOptions } from 'graphql-ws';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The extra that will be put in the `Context`.
|
|
8
|
+
*
|
|
9
|
+
* @category Server/h3
|
|
10
|
+
*/
|
|
11
|
+
interface Extra {
|
|
12
|
+
/**
|
|
13
|
+
* The underlying WebSocket peer.
|
|
14
|
+
*/
|
|
15
|
+
readonly peer: Peer;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create the WebSocket hooks to be used with [h3](https://h3.unjs.io/).
|
|
19
|
+
*
|
|
20
|
+
* Use this over {@link defineGraphqlWebSocketHandler} if you need more control over the WebSocket server or
|
|
21
|
+
* if you want to add custom hooks (e.g. for authentication or logging).
|
|
22
|
+
*/
|
|
23
|
+
declare function defineGraphqlWebSocket<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Partial<Hooks>;
|
|
24
|
+
/**
|
|
25
|
+
* Create a event handler to be used with [h3](https://h3.unjs.io/).
|
|
26
|
+
*
|
|
27
|
+
* @category Server/h3
|
|
28
|
+
*/
|
|
29
|
+
declare function defineGraphqlWebSocketHandler<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): EventHandler<EventHandlerRequest, never>;
|
|
3
30
|
|
|
4
31
|
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
5
32
|
interface H3ContextFunctionArgument {
|
|
@@ -7,8 +34,9 @@ interface H3ContextFunctionArgument {
|
|
|
7
34
|
}
|
|
8
35
|
interface H3HandlerOptions<TContext extends BaseContext> {
|
|
9
36
|
context?: ContextFunction<[H3ContextFunctionArgument], TContext>;
|
|
37
|
+
websocket?: Partial<Hooks>;
|
|
10
38
|
}
|
|
11
39
|
declare function startServerAndCreateH3Handler(server: ApolloServer<BaseContext>, options?: H3HandlerOptions<BaseContext>): EventHandler;
|
|
12
40
|
declare function startServerAndCreateH3Handler<TContext extends BaseContext>(server: ApolloServer<TContext>, options: WithRequired<H3HandlerOptions<TContext>, 'context'>): EventHandler;
|
|
13
41
|
|
|
14
|
-
export { type H3ContextFunctionArgument, type H3HandlerOptions, startServerAndCreateH3Handler };
|
|
42
|
+
export { type H3ContextFunctionArgument, type H3HandlerOptions, defineGraphqlWebSocket, defineGraphqlWebSocketHandler, startServerAndCreateH3Handler };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
import { BaseContext, ContextFunction, ApolloServer } from '@apollo/server';
|
|
2
|
-
import {
|
|
2
|
+
import { Hooks, Peer } from 'crossws';
|
|
3
|
+
import { EventHandler, EventHandlerRequest, H3Event } from 'h3';
|
|
4
|
+
import { ConnectionInitMessage, ServerOptions } from 'graphql-ws';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The extra that will be put in the `Context`.
|
|
8
|
+
*
|
|
9
|
+
* @category Server/h3
|
|
10
|
+
*/
|
|
11
|
+
interface Extra {
|
|
12
|
+
/**
|
|
13
|
+
* The underlying WebSocket peer.
|
|
14
|
+
*/
|
|
15
|
+
readonly peer: Peer;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create the WebSocket hooks to be used with [h3](https://h3.unjs.io/).
|
|
19
|
+
*
|
|
20
|
+
* Use this over {@link defineGraphqlWebSocketHandler} if you need more control over the WebSocket server or
|
|
21
|
+
* if you want to add custom hooks (e.g. for authentication or logging).
|
|
22
|
+
*/
|
|
23
|
+
declare function defineGraphqlWebSocket<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Partial<Hooks>;
|
|
24
|
+
/**
|
|
25
|
+
* Create a event handler to be used with [h3](https://h3.unjs.io/).
|
|
26
|
+
*
|
|
27
|
+
* @category Server/h3
|
|
28
|
+
*/
|
|
29
|
+
declare function defineGraphqlWebSocketHandler<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): EventHandler<EventHandlerRequest, never>;
|
|
3
30
|
|
|
4
31
|
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
5
32
|
interface H3ContextFunctionArgument {
|
|
@@ -7,8 +34,9 @@ interface H3ContextFunctionArgument {
|
|
|
7
34
|
}
|
|
8
35
|
interface H3HandlerOptions<TContext extends BaseContext> {
|
|
9
36
|
context?: ContextFunction<[H3ContextFunctionArgument], TContext>;
|
|
37
|
+
websocket?: Partial<Hooks>;
|
|
10
38
|
}
|
|
11
39
|
declare function startServerAndCreateH3Handler(server: ApolloServer<BaseContext>, options?: H3HandlerOptions<BaseContext>): EventHandler;
|
|
12
40
|
declare function startServerAndCreateH3Handler<TContext extends BaseContext>(server: ApolloServer<TContext>, options: WithRequired<H3HandlerOptions<TContext>, 'context'>): EventHandler;
|
|
13
41
|
|
|
14
|
-
export { type H3ContextFunctionArgument, type H3HandlerOptions, startServerAndCreateH3Handler };
|
|
42
|
+
export { type H3ContextFunctionArgument, type H3HandlerOptions, defineGraphqlWebSocket, defineGraphqlWebSocketHandler, startServerAndCreateH3Handler };
|
package/dist/index.mjs
CHANGED
|
@@ -1,34 +1,88 @@
|
|
|
1
1
|
import { HeaderMap } from '@apollo/server';
|
|
2
|
-
import { eventHandler, isMethod, setHeaders, getHeaders, readBody } from 'h3';
|
|
2
|
+
import { defineWebSocket, defineWebSocketHandler, eventHandler, isMethod, setHeaders, getHeaders, readBody } from 'h3';
|
|
3
|
+
import { makeServer, GRAPHQL_TRANSPORT_WS_PROTOCOL } from 'graphql-ws';
|
|
4
|
+
|
|
5
|
+
function defineGraphqlWebSocket(options) {
|
|
6
|
+
const server = makeServer(options);
|
|
7
|
+
const peers = /* @__PURE__ */ new WeakMap();
|
|
8
|
+
return defineWebSocket({
|
|
9
|
+
open(peer) {
|
|
10
|
+
const client = {
|
|
11
|
+
handleMessage: () => {
|
|
12
|
+
throw new Error("Message received before handler was registered");
|
|
13
|
+
},
|
|
14
|
+
closed: () => {
|
|
15
|
+
throw new Error("Closed before handler was registered");
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
client.closed = server.opened(
|
|
19
|
+
{
|
|
20
|
+
// TODO: use protocol on socket once h3 exposes it
|
|
21
|
+
// https://github.com/unjs/crossws/issues/31
|
|
22
|
+
protocol: GRAPHQL_TRANSPORT_WS_PROTOCOL,
|
|
23
|
+
send: (message) => {
|
|
24
|
+
if (peers.has(peer)) {
|
|
25
|
+
peer.send(message);
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
close: (_code, _reason) => {
|
|
29
|
+
if (peers.has(peer)) ;
|
|
30
|
+
},
|
|
31
|
+
onMessage: (cb) => client.handleMessage = cb
|
|
32
|
+
},
|
|
33
|
+
{ peer }
|
|
34
|
+
);
|
|
35
|
+
peers.set(peer, client);
|
|
36
|
+
},
|
|
37
|
+
message(peer, message) {
|
|
38
|
+
const client = peers.get(peer);
|
|
39
|
+
if (!client)
|
|
40
|
+
throw new Error("Message received for a missing client");
|
|
41
|
+
return client.handleMessage(message.text());
|
|
42
|
+
},
|
|
43
|
+
close(peer, details) {
|
|
44
|
+
const client = peers.get(peer);
|
|
45
|
+
if (!client)
|
|
46
|
+
throw new Error("Closing a missing client");
|
|
47
|
+
return client.closed(details.code ?? 1e3, details.reason ?? "");
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function defineGraphqlWebSocketHandler(options) {
|
|
52
|
+
return defineWebSocketHandler(defineGraphqlWebSocket(options));
|
|
53
|
+
}
|
|
3
54
|
|
|
4
55
|
function startServerAndCreateH3Handler(server, options) {
|
|
5
56
|
server.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
|
|
6
57
|
const defaultContext = () => Promise.resolve({});
|
|
7
58
|
const contextFunction = options?.context ?? defaultContext;
|
|
8
|
-
return eventHandler(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
try {
|
|
13
|
-
const graphqlRequest = await toGraphqlRequest(event);
|
|
14
|
-
const { body, headers, status } = await server.executeHTTPGraphQLRequest({
|
|
15
|
-
httpGraphQLRequest: graphqlRequest,
|
|
16
|
-
context: () => contextFunction({ event })
|
|
17
|
-
});
|
|
18
|
-
if (body.kind === "chunked") {
|
|
19
|
-
throw new Error("Incremental delivery not implemented");
|
|
59
|
+
return eventHandler({
|
|
60
|
+
async handler(event) {
|
|
61
|
+
if (isMethod(event, "OPTIONS")) {
|
|
62
|
+
return null;
|
|
20
63
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
64
|
+
try {
|
|
65
|
+
const graphqlRequest = await toGraphqlRequest(event);
|
|
66
|
+
const { body, headers, status } = await server.executeHTTPGraphQLRequest({
|
|
67
|
+
httpGraphQLRequest: graphqlRequest,
|
|
68
|
+
context: () => contextFunction({ event })
|
|
69
|
+
});
|
|
70
|
+
if (body.kind === "chunked") {
|
|
71
|
+
throw new Error("Incremental delivery not implemented");
|
|
72
|
+
}
|
|
73
|
+
setHeaders(event, Object.fromEntries(headers));
|
|
74
|
+
event.res.statusCode = status || 200;
|
|
75
|
+
return body.string;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
if (error instanceof SyntaxError) {
|
|
78
|
+
event.res.statusCode = 400;
|
|
79
|
+
return error.message;
|
|
80
|
+
} else {
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
30
83
|
}
|
|
31
|
-
}
|
|
84
|
+
},
|
|
85
|
+
websocket: options?.websocket
|
|
32
86
|
});
|
|
33
87
|
}
|
|
34
88
|
async function toGraphqlRequest(event) {
|
|
@@ -63,4 +117,4 @@ async function normalizeBody(event) {
|
|
|
63
117
|
}
|
|
64
118
|
}
|
|
65
119
|
|
|
66
|
-
export { startServerAndCreateH3Handler };
|
|
120
|
+
export { defineGraphqlWebSocket, defineGraphqlWebSocketHandler, startServerAndCreateH3Handler };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@as-integrations/h3",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "An Apollo Server integration for use with h3 or Nuxt",
|
|
5
5
|
"repository": "github:apollo-server-integrations/apollo-server-integration-h3",
|
|
6
6
|
"license": "MIT",
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"type": "module",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
11
12
|
"import": "./dist/index.mjs",
|
|
12
13
|
"require": "./dist/index.cjs"
|
|
13
14
|
}
|
|
@@ -20,38 +21,56 @@
|
|
|
20
21
|
],
|
|
21
22
|
"peerDependencies": {
|
|
22
23
|
"@apollo/server": "^4.1.1",
|
|
23
|
-
"h3": "^1.
|
|
24
|
+
"h3": "^1.11.0",
|
|
25
|
+
"graphql": "^16.0.0",
|
|
26
|
+
"graphql-ws": "^5.0.0",
|
|
27
|
+
"crossws": "^0.2.4"
|
|
28
|
+
},
|
|
29
|
+
"peerDependenciesMeta": {
|
|
30
|
+
"graphql-ws": {
|
|
31
|
+
"optional": true
|
|
32
|
+
}
|
|
24
33
|
},
|
|
25
34
|
"devDependencies": {
|
|
26
|
-
"@apollo/server": "^4.
|
|
27
|
-
"@apollo/server-integration-testsuite": "^4.
|
|
35
|
+
"@apollo/server": "^4.10.4",
|
|
36
|
+
"@apollo/server-integration-testsuite": "^4.10.4",
|
|
28
37
|
"@apollo/utils.withrequired": "^3.0.0",
|
|
29
|
-
"@
|
|
30
|
-
"@
|
|
31
|
-
"@typescript-eslint/
|
|
32
|
-
"
|
|
33
|
-
"@vitest/coverage-
|
|
34
|
-
"eslint": "^
|
|
35
|
-
"eslint-config-prettier": "^9.
|
|
36
|
-
"eslint-
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
38
|
+
"@graphql-tools/schema": "^10.0.3",
|
|
39
|
+
"@jest/globals": "^29.7.0",
|
|
40
|
+
"@typescript-eslint/parser": "^7.16.0",
|
|
41
|
+
"crossws": "^0.2.4",
|
|
42
|
+
"@vitest/coverage-v8": "^2.0.1",
|
|
43
|
+
"eslint": "^9.6.0",
|
|
44
|
+
"eslint-config-prettier": "^9.1.0",
|
|
45
|
+
"eslint-config-unjs": "^0.3.2",
|
|
46
|
+
"eslint-plugin-unused-imports": "^4.0.0",
|
|
47
|
+
"graphql": "^16.9.0",
|
|
48
|
+
"graphql-subscriptions": "^2.0.0",
|
|
49
|
+
"graphql-ws": "^5.15.0",
|
|
50
|
+
"h3": "^1.12.0",
|
|
51
|
+
"jest": "^29.7.0",
|
|
52
|
+
"listhen": "^1.7.2",
|
|
53
|
+
"prettier": "^3.3.2",
|
|
41
54
|
"standard-version": "^9.5.0",
|
|
42
|
-
"ts-jest": "^29.
|
|
43
|
-
"typescript": "^5.
|
|
55
|
+
"ts-jest": "^29.2.0",
|
|
56
|
+
"typescript": "^5.5.3",
|
|
44
57
|
"unbuild": "^2.0.0",
|
|
45
|
-
"vitest": "^0.
|
|
58
|
+
"vitest": "^2.0.1"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": "^16.10.0 || >=18.0.0"
|
|
46
62
|
},
|
|
47
|
-
"packageManager": "pnpm@8.6.12",
|
|
48
63
|
"scripts": {
|
|
64
|
+
"dev:prepare": "unbuild --stub",
|
|
49
65
|
"build": "unbuild",
|
|
50
66
|
"test": "vitest dev",
|
|
51
67
|
"test:integration": "jest",
|
|
68
|
+
"test:types": "tsc --noEmit --skipLibCheck",
|
|
52
69
|
"lint": "pnpm lint:eslint && pnpm lint:prettier",
|
|
53
|
-
"lint:eslint": "eslint --
|
|
70
|
+
"lint:eslint": "eslint --report-unused-disable-directives .",
|
|
54
71
|
"lint:prettier": "prettier --check --ignore-path .gitignore . '!pnpm-lock.yaml'",
|
|
72
|
+
"example:simple": "listhen -w --open ./examples/simple.ts",
|
|
73
|
+
"example:websocket": "listhen -w --ws --open ./examples/websocket.ts",
|
|
55
74
|
"release": "pnpm test && standard-version && git push --follow-tags && pnpm publish"
|
|
56
75
|
}
|
|
57
76
|
}
|