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:
@ -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
|
||||
|
Reference in New Issue
Block a user