From 164ecb77c64065ea9a496c6a74e9bf4263ea8a6c Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 20 Jan 2020 21:01:59 +0100 Subject: [PATCH] Add new resolver files --- src/app.ts | 10 ++++++++-- src/graphql/MutationResolver.ts | 6 ++++++ src/graphql/QueryResolver.ts | 5 +++++ src/lib/errors/BaseError.ts | 1 + src/lib/models/Post.ts | 1 + 5 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 src/graphql/MutationResolver.ts create mode 100644 src/graphql/QueryResolver.ts diff --git a/src/app.ts b/src/app.ts index 391f6d7..c6b1c54 100644 --- a/src/app.ts +++ b/src/app.ts @@ -8,7 +8,7 @@ import * as graphqlHTTP from "express-graphql"; import * as session from "express-session"; import sharedsession = require("express-socket.io-session"); import * as fsx from "fs-extra"; -import {buildSchema} from "graphql"; +import {buildSchema, GraphQLError} from "graphql"; import {importSchema} from "graphql-import"; import queryComplexity, {directiveEstimator, simpleEstimator} from "graphql-query-complexity"; import * as http from "http"; @@ -192,11 +192,17 @@ class App { }); // @ts-ignore - this.app.use("/graphql", graphqlHTTP(async (request, response, {variables}) => { + this.app.use("/graphql", graphqlHTTP(async (request: any, response: any, {variables}) => { response.setHeader("X-Max-Query-Complexity", config.get("api.maxQueryComplexity")); return { // @ts-ignore all context: {session: request.session}, + formatError: (err: GraphQLError | any) => { + if (err.statusCode) { + response.status(err.statusCode); + } + return err.graphqlError ?? err; + }, graphiql: config.get("api.graphiql"), rootValue: resolver(request, response), schema: buildSchema(importSchema(path.join(__dirname, "./graphql/schema.graphql"))), diff --git a/src/graphql/MutationResolver.ts b/src/graphql/MutationResolver.ts new file mode 100644 index 0000000..017f822 --- /dev/null +++ b/src/graphql/MutationResolver.ts @@ -0,0 +1,6 @@ +/** + * A class that provides methods to resolve mutations + */ +export class MutationResolver { + +} diff --git a/src/graphql/QueryResolver.ts b/src/graphql/QueryResolver.ts new file mode 100644 index 0000000..51a9828 --- /dev/null +++ b/src/graphql/QueryResolver.ts @@ -0,0 +1,5 @@ +/** + * A class that provides functions to resolve queries + */ +export class QueryResolver { +} diff --git a/src/lib/errors/BaseError.ts b/src/lib/errors/BaseError.ts index b4ad153..0b91661 100644 --- a/src/lib/errors/BaseError.ts +++ b/src/lib/errors/BaseError.ts @@ -8,6 +8,7 @@ export class BaseError extends Error { * The graphql error with a frontend error message */ public readonly graphqlError: GraphQLError; + public readonly statusCode: number = 400; constructor(message?: string, friendlyMessage?: string) { super(message); diff --git a/src/lib/models/Post.ts b/src/lib/models/Post.ts index fad5013..b96b88b 100644 --- a/src/lib/models/Post.ts +++ b/src/lib/models/Post.ts @@ -1,5 +1,6 @@ import * as sqz from "sequelize"; import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table} from "sequelize-typescript"; +import globals from "../globals"; import markdown from "../markdown"; import {Activity} from "./Activity"; import {PostVote, VoteType} from "./PostVote";