forked from fastify/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathbenzene-http.js
40 lines (33 loc) · 904 Bytes
/
benzene-http.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use strict";
const { createServer } = require("http");
const {
Benzene,
makeHandler,
parseGraphQLBody,
makeCompileQuery,
} = require("@benzene/http");
const { createApolloSchema } = require("../lib/schemas/createApolloSchema");
const rawBody = (req, done) => {
let body = "";
req.on("data", (chunk) => (body += chunk));
req.on("end", () => done(body));
};
const schema = createApolloSchema();
const GQL = new Benzene({
schema,
compileQuery: makeCompileQuery(),
});
const graphqlHTTP = makeHandler(GQL);
const server = createServer((req, res) => {
rawBody(req, (rawBody) =>
graphqlHTTP({
method: req.method,
headers: req.headers,
body: parseGraphQLBody(rawBody, req.headers["content-type"]),
}).then((result) => {
res.writeHead(result.status, result.headers);
res.end(JSON.stringify(result.payload));
}),
);
});
server.listen(4001);