Mutation error handling

Say I'm trying to make a bike like a mutation

var createBike = (wheelSize) => { if (!factoryHasEnoughMetal(wheelSize)) { return supplierError('Not enough metal'); } return factoryBuild(wheelSize); } 

What happens when they lack the steel for them with shiny wheels? We will probably need a mistake for the client side. How do I get them from my graphQL server using the following mutation:

 // Mutations mutation: new graphql.GraphQLObjectType({ name: 'BikeMutation', fields: () => ({ createBike: { type: bikeType, args: { wheelSize: { description: 'Wheel size', type: new graphql.GraphQLNonNull(graphql.Int) }, }, resolve: (_, args) => createBike(args.wheelSize) } }) }) 

This is as simple as returning some type of error that I defined for server /?

+5
source share
1 answer

Not quite sure what it is that you after ...

Just enter a new error, it should return something like

 { "data": { "createBike": null }, "errors": [ { "message": "Not enough metal", "originalError": {} } ] } 

your client side should just handle the response

 if (res.errors) {res.errors[0].message} 

What I am doing is passing an object with an error code and a message, at this stage the best way to do this is to execute it.

 throw new Errors(JSON.stringify({ code:409, message:"Duplicate request......" })) 

NOTE: you may also be interested in this library https://github.com/kadirahq/graphql-errors

you can mask all errors (include the message "Internal error") or define userError ("message to the client"), which they will not be replaced.

+6
source

Source: https://habr.com/ru/post/1244167/


All Articles