forked from fastify/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathapollo-as-koa-integrations+graphql-jit+type-graphql.js
43 lines (37 loc) · 1.24 KB
/
apollo-as-koa-integrations+graphql-jit+type-graphql.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
42
43
"use strict";
const { koaMiddleware } = require("@as-integrations/koa");
const Koa = require("koa");
const bodyParser = require("koa-bodyparser");
const cors = require("@koa/cors");
const { ApolloServer } = require("@apollo/server");
const {
ApolloServerPluginDrainHttpServer,
} = require("@apollo/server/plugin/drainHttpServer");
const { parse } = require("graphql");
const { compileQuery } = require("graphql-jit");
const { createServer } = require("http");
const {
createAsyncTypeGraphQLSchema,
} = require("../lib/schemas/createTypeGraphQLSchema");
const app = new Koa();
const cache = {};
const httpServer = createServer(app.callback());
createAsyncTypeGraphQLSchema().then((schema) => {
const server = new ApolloServer({
schema,
executor: ({ source, context }) => {
if (!(source in cache)) {
const document = parse(source);
cache[source] = compileQuery(schema, document);
}
return cache[source].query({}, context, {});
},
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
server.start().then(() => {
app.use(cors());
app.use(bodyParser());
app.use(koaMiddleware(server, {}));
return new Promise((resolve) => httpServer.listen({ port: 4001 }, resolve));
});
});