forked from contra/graphql-helix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
119 lines (105 loc) · 3.02 KB
/
server.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import express from "express";
import { parse, validate } from "graphql";
import {
getGraphQLParameters,
processRequest,
renderGraphiQL,
shouldRenderGraphiQL,
} from "graphql-helix";
import { compileQuery, isCompiledQuery } from "graphql-jit";
import lru from "tiny-lru";
import { schema } from "./schema";
const cache = lru(1000, 3600000);
const app = express();
app.use(express.json());
app.use("/graphql", async (req, res) => {
const request = {
body: req.body,
headers: req.headers,
method: req.method,
query: req.query,
};
if (shouldRenderGraphiQL(request)) {
res.send(renderGraphiQL());
} else {
const { operationName, query, variables } = getGraphQLParameters(request);
const cacheKey = query || "";
const cached = cache.get(cacheKey);
let compiledQuery = cached?.compiledQuery;
let document = cached?.document;
let validationErrors = cached?.validationErrors;
console.log({ cached, compiledQuery, document, validationErrors });
const result = await processRequest({
operationName,
query,
variables,
request,
schema,
parse: (source, options) => {
if (!document) {
document = parse(source, options);
cache.set(cacheKey, { document });
}
return document;
},
validate: (schema, documentAST, rules, typeInfo, options) => {
if (!validationErrors) {
validationErrors = validate(
schema,
documentAST,
rules,
typeInfo,
options
);
cache.set(cacheKey, { document, validationErrors });
}
return validationErrors;
},
execute: (
schema,
documentAst,
rootValue,
contextValue,
variableValues,
operationName
) => {
if (!compiledQuery) {
compiledQuery = compileQuery(schema, documentAst, operationName);
cache.set(cacheKey, { compiledQuery, document, validationErrors });
}
if (isCompiledQuery(compiledQuery)) {
return compiledQuery.query(
rootValue,
contextValue,
variableValues || {}
);
} else {
return compiledQuery;
}
},
});
if (result.type === "RESPONSE") {
result.headers.forEach(({ name, value }) => res.setHeader(name, value));
res.status(result.status);
res.json(result.payload);
} else if (result.type === "PUSH") {
res.writeHead(200, {
"Content-Type": "text/event-stream",
Connection: "keep-alive",
"Cache-Control": "no-cache",
});
req.on("close", () => {
result.unsubscribe();
});
await result.subscribe((result) => {
res.write(`data: ${JSON.stringify(result)}\n\n`);
});
} else {
// GraphQL JIT does not currently support @defer and @stream
}
}
});
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`GraphQL server is running on port ${port}.`);
});