forked from fastify/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathgraphql-http+graphql-jit+graphql-compose.js
46 lines (41 loc) · 1.24 KB
/
graphql-http+graphql-jit+graphql-compose.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
44
45
46
"use strict";
const { createHandler } = require("graphql-http/lib/use/express");
const express = require("express");
const { parse } = require("graphql");
const { compileQuery } = require("graphql-jit");
const processRequest = require("graphql-upload/processRequest.js");
const {
createGraphqlComposeSchema,
} = require("../lib/schemas/createGraphqlCompose");
const app = express();
const cache = {};
const schema = createGraphqlComposeSchema();
app.use(
"/graphql",
createHandler({
schema,
async parseRequestParams(req) {
const params = await processRequest(req.raw, req.context.res);
if (Array.isArray(params)) {
throw new Error("Batching is not supported");
}
return {
...params,
// variables must be an object as per the GraphQL over HTTP spec
variables: Object(params.variables),
};
},
execute: (_, __, { query }) => {
if (!(query in cache)) {
const document = parse(query);
cache[query] = compileQuery(schema, document);
}
return {
schema,
customExecuteFn: ({ rootValue, variableValues, contextValue }) =>
cache[query].query(rootValue, contextValue, variableValues),
};
},
}),
);
app.listen(4001);