How to handle error and send response in GraphQL

前端 未结 2 826
无人及你
无人及你 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:42

    graphql should be an application level layer that shouldn't (see last paragraph why shouldn't and not doesn't) require http to work. Although in 99% of cases it runs on top of http, because of how convenient it is to do so, graphql is itself a layer 7 protocol.

    What does that mean in your case? Well, it means you should not mix concepts from HTTP/REST with concepts from graphql and focus on the latter. The headers error code is a HTTP/REST concept, graphql sends errors in the errors field of the response and the nodejs implementation already catches all your errors and adds them to the list. The HTTP status will be always 200, and your clients shouldn't care and consume your graphql api and not a mix of REST with graphql.

    Now, that being said, there are couple of things that REST over HTTP does better. So people, including the developers of Apollo, kinda mixed concepts too, mainly because the graphql standard is not complete (aka, it doesn't have a standard/rule for solving all the problems you might encounter while building an API), so people improvised. Honestly, I wouldn't recommend graphql yet for any serious project. Been there, done that, not worth it. Just stick with REST over HTTP.

    Reference

    0 讨论(0)
  • 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!

    0 讨论(0)
提交回复
热议问题