How to handle error and send response in GraphQL

前端 未结 2 815
无人及你
无人及你 2021-02-19 03:03

I was starting with GraphQL and I was unable to comprehend how we can throw errors in GraphQL

I went through a couple of articles on the web but almost all of them use

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-19 03:45

    What I have faced in one of my projects, it is hard to set the status code of the response. So, I made some custom error response to identify correct statusCode using express-graphql

    Below is the example (What I have used in one of my projects):

    --------app.js file--------

    const graphqlHTTP = require('express-graphql')
    
    app.use('/graphql', (req, res) => {
      graphqlHTTP({
        schema: GraphQLSchema, //A GraphQLSchema instance from GraphQL.js. A schema must be provided.
        graphiql: true,
        context: { req },
        formatError: (err) => {
          const error = getErrorCode(err.message)
          return ({ message: error.message, statusCode: error.statusCode })
        }
      })(req, res)
    })
    

    --------getErrorCode function implementation--------

    const { errorType } = require('../constants')
    
    const getErrorCode = errorName => {
      return errorType[errorName]
    }
    
    module.exports = getErrorCode
    

    --------Constant.js file--------

    exports.errorName = {
      USER_ALREADY_EXISTS: 'USER_ALREADY_EXISTS',
      SERVER_ERROR: 'SERVER_ERROR'
    }
    
    exports.errorType = {
      USER_ALREADY_EXISTS: {
        message: 'User is already exists.',
        statusCode: 403
      },
      SERVER_ERROR: {
        message: 'Server error.',
        statusCode: 500
      }
    }
    

    Now, we are ready to use our setup.

    From your query or mutation, you need to require constant file and return custom error:

    const { errorName } = require('../constant')
    
    AddNewPersonalInfo: {
      type: userDashboardType,
      args: { 
        parameter: {
          type: userCreationlInputType
        }
      }, 
      resolve: async (parent, args, context) => {
        args.parameter.userId = context.req.headers.userId
        //Check if user info already exsist
        const checkIfUserInformationExsist = await getSelectedThingFromTable('CatsWork_personal', 'userId', `${userId}`)
        if (checkIfUserInformationExsist[0]) {
          const error = {
            code: 403, 
            message: 'User info Already exsist'
          }
          throw new Error(errorName.USER_ALREADY_EXISTS) // Here you can use error from constatnt file
        } else {
          try {
          const addLinkedinUser = await insertIntheTable('personal', payload)
          return true
          } catch (err) {
            console.error(err)
            throw new Error(errorName.SERVER_ERROR) // Here you can use error from constatnt file
          }
        }
      }
    }
    

    --------Error response--------

    {
      error: [{
        "statusCode": 403,
        "message": "User is already exists."
      }],
      data: null
    }
    

    We just need to write custom error handling from FS side too.

    Note:- formatError: is deprecated and replaced by customFormatErrorFn. It will be removed in version 1.0.0. You can refer customFormatErrorFn.

    Hope this help!

提交回复
热议问题