generated from kriasoft/graphql-starter-kit
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.ts
77 lines (68 loc) · 2.24 KB
/
graphql.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
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */
/* SPDX-License-Identifier: MIT */
import type { NextFunction, Request, Response } from "express";
import { GraphQLError, printSchema } from "graphql";
import {
getGraphQLParameters,
processRequest,
renderGraphiQL,
sendResult,
shouldRenderGraphiQL,
} from "graphql-helix";
import { HttpError } from "http-errors";
import fs from "node:fs/promises";
import { ValidationError } from "validator-fluent";
import { Context, log } from "./core";
import schema from "./schema";
// Customize GraphQL error serialization
GraphQLError.prototype.toJSON = ((serialize) =>
function toJSON(this: GraphQLError) {
// The original serialized GraphQL error output
const output = serialize.call(this as GraphQLError);
// Append the `HttpError` code
if (this.originalError instanceof HttpError) {
output.status = this.originalError.statusCode;
}
// Append the list of user input validation errors
if (this.originalError instanceof ValidationError) {
output.status = 400;
output.errors = this.originalError.errors;
}
return output;
})(GraphQLError.prototype.toJSON);
/**
* GraphQL middleware for Express.js
*/
async function handleGraphQL(req: Request, res: Response, next: NextFunction) {
try {
if (shouldRenderGraphiQL(req)) {
res.send(renderGraphiQL({ endpoint: "/api" }));
} else {
const params = getGraphQLParameters(req);
const result = await processRequest<Context>({
operationName: params.operationName,
query: params.query,
variables: params.variables,
request: req,
schema,
contextFactory: () => new Context(req, params),
});
sendResult(result, res, (result) => ({
data: result.data,
errors: result.errors?.map((err) => {
if (!(err.originalError instanceof ValidationError)) {
log(req, "ERROR", err.originalError ?? err, params);
}
return err;
}),
}));
}
} catch (err) {
next(err);
}
}
function updateSchema(): Promise<void> {
const output = printSchema(schema);
return fs.writeFile("./schema.graphql", output, { encoding: "utf-8" });
}
export { handleGraphQL, updateSchema };