forked from fastify/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathgraphql-api-koa+graphql-jit.js
41 lines (36 loc) · 1.09 KB
/
graphql-api-koa+graphql-jit.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
41
"use strict";
const Koa = require("koa");
const bodyParser = require("koa-bodyparser");
const graphqlUploadKoa = require("graphql-upload/graphqlUploadKoa.js");
const { parse } = require("graphql");
const { compileQuery } = require("graphql-jit");
const { createApolloSchema } = require("../lib/schemas/createApolloSchema");
const schema = createApolloSchema();
const cache = {};
Promise.all([
import("graphql-api-koa/execute.mjs"),
import("graphql-api-koa/errorHandler.mjs"),
]).then(([{ default: execute }, { default: errorHandler }]) => {
const app = new Koa()
.use(errorHandler())
.use(graphqlUploadKoa())
.use(bodyParser())
.use(
execute({
schema,
override: ({
request: {
body: { query },
},
}) => ({
execute({ rootValue, contextValue, variableValues }) {
if (!(query in cache)) {
cache[query] = compileQuery(schema, parse(query));
}
return cache[query].query(rootValue, contextValue, variableValues);
},
}),
}),
);
app.listen(4001);
});