@as-integrations/h3 1.1.5 → 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 -71
- package/dist/index.d.ts +1 -14
- package/dist/index.mjs +11 -67
- package/package.json +40 -23
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,72 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
server.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
|
|
7
|
-
const defaultContext = () => Promise.resolve({});
|
|
8
|
-
const contextFunction = options?.context ?? defaultContext;
|
|
9
|
-
return h3.eventHandler(async (event) => {
|
|
10
|
-
if (h3.isMethod(event, "OPTIONS")) {
|
|
11
|
-
return null;
|
|
12
|
-
}
|
|
13
|
-
try {
|
|
14
|
-
const graphqlRequest = await toGraphqlRequest(event);
|
|
15
|
-
const { body, headers, status } = await server.executeHTTPGraphQLRequest({
|
|
16
|
-
httpGraphQLRequest: graphqlRequest,
|
|
17
|
-
context: () => contextFunction({ event })
|
|
18
|
-
});
|
|
19
|
-
if (body.kind === "chunked") {
|
|
20
|
-
throw new Error("Incremental delivery not implemented");
|
|
21
|
-
}
|
|
22
|
-
h3.setHeaders(event, Object.fromEntries(headers));
|
|
23
|
-
event.res.statusCode = status || 200;
|
|
24
|
-
return body.string;
|
|
25
|
-
} catch (error) {
|
|
26
|
-
if (error instanceof SyntaxError) {
|
|
27
|
-
event.res.statusCode = 400;
|
|
28
|
-
return error.message;
|
|
29
|
-
} else {
|
|
30
|
-
throw error;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
async function toGraphqlRequest(event) {
|
|
36
|
-
return {
|
|
37
|
-
method: event.req.method || "POST",
|
|
38
|
-
headers: normalizeHeaders(h3.getHeaders(event)),
|
|
39
|
-
search: normalizeQueryString(event.req.url),
|
|
40
|
-
body: await normalizeBody(event)
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
function normalizeHeaders(headers) {
|
|
44
|
-
const headerMap = /* @__PURE__ */ new Map();
|
|
45
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
46
|
-
if (Array.isArray(value)) {
|
|
47
|
-
headerMap.set(key, value.join(","));
|
|
48
|
-
} else if (value) {
|
|
49
|
-
headerMap.set(key, value);
|
|
50
|
-
}
|
|
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"
|
|
51
6
|
}
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
function normalizeQueryString(url) {
|
|
55
|
-
if (!url) {
|
|
56
|
-
return "";
|
|
57
|
-
}
|
|
58
|
-
return url.split("?")[1] || "";
|
|
59
|
-
}
|
|
60
|
-
async function normalizeBody(event) {
|
|
61
|
-
const PayloadMethods = ["PATCH", "POST", "PUT", "DELETE"];
|
|
62
|
-
if (h3.isMethod(event, PayloadMethods)) {
|
|
63
|
-
const body = await h3.readRawBody(event);
|
|
64
|
-
if (h3.getRequestHeader(event, "content-type")?.includes("application/json")) {
|
|
65
|
-
return body ? JSON.parse(body) : {};
|
|
66
|
-
} else {
|
|
67
|
-
return body;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
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 { H3ContextFunctionArgument, H3HandlerOptions, startServerAndCreateH3Handler };
|
|
1
|
+
export * from "/home/runner/work/apollo-server-integration-h3/apollo-server-integration-h3/src/index";
|
package/dist/index.mjs
CHANGED
|
@@ -1,70 +1,14 @@
|
|
|
1
|
-
import
|
|
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";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
try {
|
|
12
|
-
const graphqlRequest = await toGraphqlRequest(event);
|
|
13
|
-
const { body, headers, status } = await server.executeHTTPGraphQLRequest({
|
|
14
|
-
httpGraphQLRequest: graphqlRequest,
|
|
15
|
-
context: () => contextFunction({ event })
|
|
16
|
-
});
|
|
17
|
-
if (body.kind === "chunked") {
|
|
18
|
-
throw new Error("Incremental delivery not implemented");
|
|
19
|
-
}
|
|
20
|
-
setHeaders(event, Object.fromEntries(headers));
|
|
21
|
-
event.res.statusCode = status || 200;
|
|
22
|
-
return body.string;
|
|
23
|
-
} catch (error) {
|
|
24
|
-
if (error instanceof SyntaxError) {
|
|
25
|
-
event.res.statusCode = 400;
|
|
26
|
-
return error.message;
|
|
27
|
-
} else {
|
|
28
|
-
throw error;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
async function toGraphqlRequest(event) {
|
|
34
|
-
return {
|
|
35
|
-
method: event.req.method || "POST",
|
|
36
|
-
headers: normalizeHeaders(getHeaders(event)),
|
|
37
|
-
search: normalizeQueryString(event.req.url),
|
|
38
|
-
body: await normalizeBody(event)
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
function normalizeHeaders(headers) {
|
|
42
|
-
const headerMap = /* @__PURE__ */ new Map();
|
|
43
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
44
|
-
if (Array.isArray(value)) {
|
|
45
|
-
headerMap.set(key, value.join(","));
|
|
46
|
-
} else if (value) {
|
|
47
|
-
headerMap.set(key, value);
|
|
48
|
-
}
|
|
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"
|
|
49
9
|
}
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
function normalizeQueryString(url) {
|
|
53
|
-
if (!url) {
|
|
54
|
-
return "";
|
|
55
|
-
}
|
|
56
|
-
return url.split("?")[1] || "";
|
|
57
|
-
}
|
|
58
|
-
async function normalizeBody(event) {
|
|
59
|
-
const PayloadMethods = ["PATCH", "POST", "PUT", "DELETE"];
|
|
60
|
-
if (isMethod(event, PayloadMethods)) {
|
|
61
|
-
const body = await readRawBody(event);
|
|
62
|
-
if (getRequestHeader(event, "content-type")?.includes("application/json")) {
|
|
63
|
-
return body ? JSON.parse(body) : {};
|
|
64
|
-
} else {
|
|
65
|
-
return body;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
10
|
+
})("/home/runner/work/apollo-server-integration-h3/apollo-server-integration-h3/src/index.ts");
|
|
69
11
|
|
|
70
|
-
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",
|
|
@@ -21,38 +21,55 @@
|
|
|
21
21
|
],
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"@apollo/server": "^4.1.1",
|
|
24
|
-
"h3": "^
|
|
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
|
+
}
|
|
25
33
|
},
|
|
26
34
|
"devDependencies": {
|
|
27
|
-
"@apollo/server": "^4.
|
|
28
|
-
"@apollo/server-integration-testsuite": "^4.
|
|
29
|
-
"@apollo/utils.withrequired": "^
|
|
30
|
-
"@
|
|
31
|
-
"@
|
|
32
|
-
"@typescript-eslint/
|
|
33
|
-
"
|
|
34
|
-
"@vitest/coverage-
|
|
35
|
-
"eslint": "^
|
|
36
|
-
"eslint-config-prettier": "^
|
|
37
|
-
"eslint-
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
35
|
+
"@apollo/server": "^4.10.4",
|
|
36
|
+
"@apollo/server-integration-testsuite": "^4.10.4",
|
|
37
|
+
"@apollo/utils.withrequired": "^3.0.0",
|
|
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",
|
|
42
54
|
"standard-version": "^9.5.0",
|
|
43
|
-
"ts-jest": "^29.0
|
|
44
|
-
"typescript": "^
|
|
45
|
-
"unbuild": "^
|
|
46
|
-
"vitest": "^0.
|
|
55
|
+
"ts-jest": "^29.2.0",
|
|
56
|
+
"typescript": "^5.5.3",
|
|
57
|
+
"unbuild": "^2.0.0",
|
|
58
|
+
"vitest": "^2.0.1"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": "^16.10.0 || >=18.0.0"
|
|
47
62
|
},
|
|
48
|
-
"packageManager": "pnpm@7.25.1",
|
|
49
63
|
"scripts": {
|
|
50
64
|
"build": "unbuild",
|
|
51
65
|
"test": "vitest dev",
|
|
52
66
|
"test:integration": "jest",
|
|
67
|
+
"test:types": "tsc --noEmit --skipLibCheck",
|
|
53
68
|
"lint": "pnpm lint:eslint && pnpm lint:prettier",
|
|
54
|
-
"lint:eslint": "eslint --
|
|
69
|
+
"lint:eslint": "eslint --report-unused-disable-directives .",
|
|
55
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",
|
|
56
73
|
"release": "pnpm test && standard-version && git push --follow-tags && pnpm publish"
|
|
57
74
|
}
|
|
58
75
|
}
|