@as-integrations/h3 1.1.6 → 1.2.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 +61 -8
- package/dist/index.cjs +6 -67
- package/dist/index.d.ts +1 -14
- package/dist/index.mjs +11 -63
- package/package.json +39 -21
- package/dist/index.d.cts +0 -14
- package/dist/index.d.mts +0 -14
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
|
@@ -1,68 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function startServerAndCreateH3Handler(server, options) {
|
|
7
|
-
server.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
|
|
8
|
-
const defaultContext = () => Promise.resolve({});
|
|
9
|
-
const contextFunction = options?.context ?? defaultContext;
|
|
10
|
-
return h3.eventHandler(async (event) => {
|
|
11
|
-
if (h3.isMethod(event, "OPTIONS")) {
|
|
12
|
-
return null;
|
|
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");
|
|
22
|
-
}
|
|
23
|
-
h3.setHeaders(event, Object.fromEntries(headers));
|
|
24
|
-
event.res.statusCode = status || 200;
|
|
25
|
-
return body.string;
|
|
26
|
-
} catch (error) {
|
|
27
|
-
if (error instanceof SyntaxError) {
|
|
28
|
-
event.res.statusCode = 400;
|
|
29
|
-
return error.message;
|
|
30
|
-
} else {
|
|
31
|
-
throw error;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
async function toGraphqlRequest(event) {
|
|
37
|
-
return {
|
|
38
|
-
method: event.req.method || "POST",
|
|
39
|
-
headers: normalizeHeaders(h3.getHeaders(event)),
|
|
40
|
-
search: normalizeQueryString(event.req.url),
|
|
41
|
-
body: await normalizeBody(event)
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
function normalizeHeaders(headers) {
|
|
45
|
-
const headerMap = new server.HeaderMap();
|
|
46
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
47
|
-
if (Array.isArray(value)) {
|
|
48
|
-
headerMap.set(key, value.join(","));
|
|
49
|
-
} else if (value) {
|
|
50
|
-
headerMap.set(key, value);
|
|
51
|
-
}
|
|
1
|
+
module.exports = require("/home/runner/work/apollo-server-integration-h3/apollo-server-integration-h3/node_modules/.pnpm/jiti@1.21.0/node_modules/jiti/lib/index.js")(null, {
|
|
2
|
+
"esmResolve": true,
|
|
3
|
+
"interopDefault": true,
|
|
4
|
+
"alias": {
|
|
5
|
+
"@as-integrations/h3": "/home/runner/work/apollo-server-integration-h3/apollo-server-integration-h3"
|
|
52
6
|
}
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
function normalizeQueryString(url) {
|
|
56
|
-
if (!url) {
|
|
57
|
-
return "";
|
|
58
|
-
}
|
|
59
|
-
return url.split("?")[1] || "";
|
|
60
|
-
}
|
|
61
|
-
async function normalizeBody(event) {
|
|
62
|
-
const PayloadMethods = ["PATCH", "POST", "PUT", "DELETE"];
|
|
63
|
-
if (h3.isMethod(event, PayloadMethods)) {
|
|
64
|
-
return await h3.readBody(event);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
exports.startServerAndCreateH3Handler = startServerAndCreateH3Handler;
|
|
7
|
+
})("/home/runner/work/apollo-server-integration-h3/apollo-server-integration-h3/src/index.ts")
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { H3Event, EventHandler } from 'h3';
|
|
3
|
-
|
|
4
|
-
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
5
|
-
interface H3ContextFunctionArgument {
|
|
6
|
-
event: H3Event;
|
|
7
|
-
}
|
|
8
|
-
interface H3HandlerOptions<TContext extends BaseContext> {
|
|
9
|
-
context?: ContextFunction<[H3ContextFunctionArgument], TContext>;
|
|
10
|
-
}
|
|
11
|
-
declare function startServerAndCreateH3Handler(server: ApolloServer<BaseContext>, options?: H3HandlerOptions<BaseContext>): EventHandler;
|
|
12
|
-
declare function startServerAndCreateH3Handler<TContext extends BaseContext>(server: ApolloServer<TContext>, options: WithRequired<H3HandlerOptions<TContext>, 'context'>): EventHandler;
|
|
13
|
-
|
|
14
|
-
export { type H3ContextFunctionArgument, type H3HandlerOptions, startServerAndCreateH3Handler };
|
|
1
|
+
export * from "/home/runner/work/apollo-server-integration-h3/apollo-server-integration-h3/src/index";
|
package/dist/index.mjs
CHANGED
|
@@ -1,66 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { eventHandler, isMethod, setHeaders, getHeaders, readBody } from 'h3';
|
|
1
|
+
import jiti from "file:///home/runner/work/apollo-server-integration-h3/apollo-server-integration-h3/node_modules/.pnpm/jiti@1.21.0/node_modules/jiti/lib/index.js";
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return null;
|
|
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");
|
|
20
|
-
}
|
|
21
|
-
setHeaders(event, Object.fromEntries(headers));
|
|
22
|
-
event.res.statusCode = status || 200;
|
|
23
|
-
return body.string;
|
|
24
|
-
} catch (error) {
|
|
25
|
-
if (error instanceof SyntaxError) {
|
|
26
|
-
event.res.statusCode = 400;
|
|
27
|
-
return error.message;
|
|
28
|
-
} else {
|
|
29
|
-
throw error;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
async function toGraphqlRequest(event) {
|
|
35
|
-
return {
|
|
36
|
-
method: event.req.method || "POST",
|
|
37
|
-
headers: normalizeHeaders(getHeaders(event)),
|
|
38
|
-
search: normalizeQueryString(event.req.url),
|
|
39
|
-
body: await normalizeBody(event)
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
function normalizeHeaders(headers) {
|
|
43
|
-
const headerMap = new HeaderMap();
|
|
44
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
45
|
-
if (Array.isArray(value)) {
|
|
46
|
-
headerMap.set(key, value.join(","));
|
|
47
|
-
} else if (value) {
|
|
48
|
-
headerMap.set(key, value);
|
|
49
|
-
}
|
|
3
|
+
/** @type {import("/home/runner/work/apollo-server-integration-h3/apollo-server-integration-h3/src/index")} */
|
|
4
|
+
const _module = jiti(null, {
|
|
5
|
+
"esmResolve": true,
|
|
6
|
+
"interopDefault": true,
|
|
7
|
+
"alias": {
|
|
8
|
+
"@as-integrations/h3": "/home/runner/work/apollo-server-integration-h3/apollo-server-integration-h3"
|
|
50
9
|
}
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
function normalizeQueryString(url) {
|
|
54
|
-
if (!url) {
|
|
55
|
-
return "";
|
|
56
|
-
}
|
|
57
|
-
return url.split("?")[1] || "";
|
|
58
|
-
}
|
|
59
|
-
async function normalizeBody(event) {
|
|
60
|
-
const PayloadMethods = ["PATCH", "POST", "PUT", "DELETE"];
|
|
61
|
-
if (isMethod(event, PayloadMethods)) {
|
|
62
|
-
return await readBody(event);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
10
|
+
})("/home/runner/work/apollo-server-integration-h3/apollo-server-integration-h3/src/index.ts");
|
|
65
11
|
|
|
66
|
-
export
|
|
12
|
+
export const startServerAndCreateH3Handler = _module.startServerAndCreateH3Handler;
|
|
13
|
+
export const defineGraphqlWebSocket = _module.defineGraphqlWebSocket;
|
|
14
|
+
export const defineGraphqlWebSocketHandler = _module.defineGraphqlWebSocketHandler;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@as-integrations/h3",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
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,55 @@
|
|
|
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": {
|
|
49
64
|
"build": "unbuild",
|
|
50
65
|
"test": "vitest dev",
|
|
51
66
|
"test:integration": "jest",
|
|
67
|
+
"test:types": "tsc --noEmit --skipLibCheck",
|
|
52
68
|
"lint": "pnpm lint:eslint && pnpm lint:prettier",
|
|
53
|
-
"lint:eslint": "eslint --
|
|
69
|
+
"lint:eslint": "eslint --report-unused-disable-directives .",
|
|
54
70
|
"lint:prettier": "prettier --check --ignore-path .gitignore . '!pnpm-lock.yaml'",
|
|
71
|
+
"example:simple": "listhen -w --open ./examples/simple.ts",
|
|
72
|
+
"example:websocket": "listhen -w --ws --open ./examples/websocket.ts",
|
|
55
73
|
"release": "pnpm test && standard-version && git push --follow-tags && pnpm publish"
|
|
56
74
|
}
|
|
57
75
|
}
|
package/dist/index.d.cts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { BaseContext, ContextFunction, ApolloServer } from '@apollo/server';
|
|
2
|
-
import { H3Event, EventHandler } from 'h3';
|
|
3
|
-
|
|
4
|
-
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
5
|
-
interface H3ContextFunctionArgument {
|
|
6
|
-
event: H3Event;
|
|
7
|
-
}
|
|
8
|
-
interface H3HandlerOptions<TContext extends BaseContext> {
|
|
9
|
-
context?: ContextFunction<[H3ContextFunctionArgument], TContext>;
|
|
10
|
-
}
|
|
11
|
-
declare function startServerAndCreateH3Handler(server: ApolloServer<BaseContext>, options?: H3HandlerOptions<BaseContext>): EventHandler;
|
|
12
|
-
declare function startServerAndCreateH3Handler<TContext extends BaseContext>(server: ApolloServer<TContext>, options: WithRequired<H3HandlerOptions<TContext>, 'context'>): EventHandler;
|
|
13
|
-
|
|
14
|
-
export { type H3ContextFunctionArgument, type H3HandlerOptions, startServerAndCreateH3Handler };
|
package/dist/index.d.mts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { BaseContext, ContextFunction, ApolloServer } from '@apollo/server';
|
|
2
|
-
import { H3Event, EventHandler } from 'h3';
|
|
3
|
-
|
|
4
|
-
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
5
|
-
interface H3ContextFunctionArgument {
|
|
6
|
-
event: H3Event;
|
|
7
|
-
}
|
|
8
|
-
interface H3HandlerOptions<TContext extends BaseContext> {
|
|
9
|
-
context?: ContextFunction<[H3ContextFunctionArgument], TContext>;
|
|
10
|
-
}
|
|
11
|
-
declare function startServerAndCreateH3Handler(server: ApolloServer<BaseContext>, options?: H3HandlerOptions<BaseContext>): EventHandler;
|
|
12
|
-
declare function startServerAndCreateH3Handler<TContext extends BaseContext>(server: ApolloServer<TContext>, options: WithRequired<H3HandlerOptions<TContext>, 'context'>): EventHandler;
|
|
13
|
-
|
|
14
|
-
export { type H3ContextFunctionArgument, type H3HandlerOptions, startServerAndCreateH3Handler };
|