Tuesday, 6 August 2019

How to handle error and send response in GraphQL

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

I went through couple of articles on web but almost all of them use Apollo and the code-structure looks very different than how i work.

Consider this piece of code, here where I am making a mutation, now how can send response message with error and change headers status message in case of error?

  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(error)
    } else {
      try {
      const addLinkedinUser = await insertIntheTable('personal', payload)
      return true
      } catch (err) {
        console.error(err)
        throw new Error(err)
      }
    }
  }
}



from How to handle error and send response in GraphQL

No comments:

Post a Comment