@kiyasov/platform-hono 1.0.2 → 1.0.4
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/dist/cjs/src/drivers/graphql.driver.d.ts +3 -3
- package/dist/cjs/src/drivers/graphql.driver.js +11 -9
- package/dist/cjs/src/drivers/graphql.driver.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/drivers/graphql.driver.d.ts +3 -3
- package/dist/esm/src/drivers/graphql.driver.js +15 -13
- package/dist/esm/src/drivers/graphql.driver.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/drivers/graphql.driver.ts +22 -18
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { ApolloServer, BaseContext, HeaderMap } from
|
|
2
|
-
import { AbstractGraphQLDriver } from
|
|
3
|
-
import { ApolloServerPluginDrainHttpServer } from
|
|
4
|
-
import { ApolloDriverConfig } from
|
|
5
|
-
import { Context, HonoRequest } from
|
|
6
|
-
import { StatusCode } from
|
|
7
|
-
import { Logger } from
|
|
1
|
+
import { ApolloServer, BaseContext, HeaderMap } from "@apollo/server";
|
|
2
|
+
import { AbstractGraphQLDriver } from "@nestjs/graphql";
|
|
3
|
+
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
|
|
4
|
+
import { ApolloDriverConfig } from "@nestjs/apollo";
|
|
5
|
+
import { Context, HonoRequest } from "hono";
|
|
6
|
+
import { StatusCode } from "hono/utils/http-status";
|
|
7
|
+
import { Logger } from "@nestjs/common";
|
|
8
8
|
|
|
9
9
|
export class HonoGraphQLDriver<
|
|
10
|
-
T extends Record<string, any> = ApolloDriverConfig
|
|
10
|
+
T extends Record<string, any> = ApolloDriverConfig
|
|
11
11
|
> extends AbstractGraphQLDriver {
|
|
12
12
|
protected apolloServer: ApolloServer<BaseContext>;
|
|
13
13
|
|
|
@@ -19,8 +19,8 @@ export class HonoGraphQLDriver<
|
|
|
19
19
|
const { httpAdapter } = this.httpAdapterHost;
|
|
20
20
|
const platformName = httpAdapter.getType();
|
|
21
21
|
|
|
22
|
-
if (platformName !==
|
|
23
|
-
throw new Error(
|
|
22
|
+
if (platformName !== "hono") {
|
|
23
|
+
throw new Error("This driver is only compatible with the Hono platform");
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
return this.registerHono(options);
|
|
@@ -28,7 +28,7 @@ export class HonoGraphQLDriver<
|
|
|
28
28
|
|
|
29
29
|
protected async registerHono(
|
|
30
30
|
options: T,
|
|
31
|
-
{ preStartHook }: { preStartHook?: () => void } = {}
|
|
31
|
+
{ preStartHook }: { preStartHook?: () => void } = {}
|
|
32
32
|
) {
|
|
33
33
|
const { path, typeDefs, resolvers, schema } = options;
|
|
34
34
|
const { httpAdapter } = this.httpAdapterHost;
|
|
@@ -52,6 +52,10 @@ export class HonoGraphQLDriver<
|
|
|
52
52
|
app.use(path, async (ctx: Context) => {
|
|
53
53
|
const bodyData = await this.parseBody(ctx.req);
|
|
54
54
|
|
|
55
|
+
const defaultContext = () => Promise.resolve({} as BaseContext);
|
|
56
|
+
|
|
57
|
+
const contextFunction = options?.context ?? defaultContext;
|
|
58
|
+
|
|
55
59
|
const httpGraphQLResponse = await server.executeHTTPGraphQLRequest({
|
|
56
60
|
httpGraphQLRequest: {
|
|
57
61
|
body: bodyData,
|
|
@@ -59,7 +63,7 @@ export class HonoGraphQLDriver<
|
|
|
59
63
|
headers: this.httpHeadersToMap(ctx.req.raw.headers),
|
|
60
64
|
search: new URL(ctx.req.url).search,
|
|
61
65
|
},
|
|
62
|
-
context:
|
|
66
|
+
context: () => contextFunction(ctx.req, ctx),
|
|
63
67
|
});
|
|
64
68
|
|
|
65
69
|
const { headers, body, status } = httpGraphQLResponse;
|
|
@@ -70,7 +74,7 @@ export class HonoGraphQLDriver<
|
|
|
70
74
|
|
|
71
75
|
ctx.status(status === undefined ? 200 : (status as StatusCode));
|
|
72
76
|
|
|
73
|
-
if (body.kind ===
|
|
77
|
+
if (body.kind === "complete") {
|
|
74
78
|
return ctx.body(body.string);
|
|
75
79
|
}
|
|
76
80
|
|
|
@@ -84,7 +88,7 @@ export class HonoGraphQLDriver<
|
|
|
84
88
|
});
|
|
85
89
|
|
|
86
90
|
return new Response(readableStream, {
|
|
87
|
-
headers: {
|
|
91
|
+
headers: { "Content-Type": "application/octet-stream" },
|
|
88
92
|
});
|
|
89
93
|
});
|
|
90
94
|
|
|
@@ -102,12 +106,12 @@ export class HonoGraphQLDriver<
|
|
|
102
106
|
}
|
|
103
107
|
|
|
104
108
|
private async parseBody(req: HonoRequest): Promise<Record<string, unknown>> {
|
|
105
|
-
const contentType = req.header(
|
|
106
|
-
if (contentType ===
|
|
109
|
+
const contentType = req.header("content-type");
|
|
110
|
+
if (contentType === "application/graphql")
|
|
107
111
|
return { query: await req.text() };
|
|
108
|
-
if (contentType ===
|
|
112
|
+
if (contentType === "application/json")
|
|
109
113
|
return req.json().catch(this.logError);
|
|
110
|
-
if (contentType ===
|
|
114
|
+
if (contentType === "application/x-www-form-urlencoded")
|
|
111
115
|
return this.parseFormURL(req);
|
|
112
116
|
return {};
|
|
113
117
|
}
|