1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-06-28 23:09:18 +00:00

feat(server): Add better logging to detect potential errors

Signed-off-by: Avior <f.bouillon@aptatio.com>
This commit is contained in:
2022-12-09 14:42:22 +01:00
parent 33814d7df2
commit 8192542c1d
2 changed files with 41 additions and 10 deletions

View File

@ -1,26 +1,44 @@
import express from 'express'
import { graphqlHTTP } from 'express-graphql'
import { buildSchema, formatError } from 'graphql'
import resolver from './resolver'
import fs from 'fs'
import { buildSchema, formatError, GraphQLError } from 'graphql'
import resolver from './resolver'
// Init Express Router
const router = express.Router()
/**
* Drawbacks
* Attack.damage is a string instead of possibly being a number or a string
*/
const schema = buildSchema(fs.readFileSync('./public/v2/graphql.gql').toString())
// Error Logging for debugging
function graphQLErrorHandle(error: GraphQLError) {
if (process.env.NODE_ENV !== 'production') {
console.error(error)
}
if (error.source) {
console.error('GraphQL Error')
console.error(error.message)
console.error(error.source?.body.replace(/\n/g, '\\n'))
}
return formatError(error)
}
// Add graphql to the route
router.use(graphqlHTTP({
router.get('/', graphqlHTTP({
schema,
rootValue: resolver,
graphiql: true,
customFormatErrorFn(error) {
console.error(error)
return formatError(error)
}
customFormatErrorFn: graphQLErrorHandle
}))
router.post('/', graphqlHTTP({
schema,
rootValue: resolver,
graphiql: true,
customFormatErrorFn: graphQLErrorHandle
}))
export default router