@ozylog/graphql-errors
v0.1.1
Published
Graphql errors handler
Downloads
3
Readme
@ozylog/graphql-errors
Graphql errors handler
Installation
npm install @ozylog/graphql-errors --save
Usage
formatError
/*
* formatError()
* on development environment, it will return
* {
* message: string,
* data: any,
* createdAt: Date,
* locations: Array<Object>,
* path: Array<string>,
* stack: string
* }
*
* on production environment, it will return
* {
* message: string,
* data: any,
* createdAt: Date
* }
*/
formatError();
/*
* formatError(fields)
* it will return object error based on fields
* e.g. formartError(['message', 'data', 'locations']) will return
* {
* message: string,
* data: any,
* locations: Array<Object>
* }
*/
formatError(fields: Array<string>)
CreateError
CreateError(message: string, data: any = null)
Usage Examples
formatError
'use strict';
import express from 'express';
import expressGraphql from 'express-graphql';
import {formatError} from '@ozylog/graphql-errors';
const app = express();
app.use('/graphql', expressGraphql({
schema,
formatError: formatError()
}));
CreateError
// errors.js
'use strict';
import {CreateError} from '@ozylog/graphql-errors';
export const BadRequestError = CreateError.bind(null, 'Bad Request');
export const ForbiddenError = CreateError.bind(null, 'Forbidden');
// userMutation.js
'use strict';
import {GraphQLString, GraphQLNonNull} from 'graphql';
import {Rules} from '@ozylog/validator';
import userType from './userType';
import {BadRequestError} from './errors';
export default {
user: {
type: userType,
args: {
name: {
name: 'name',
type: new GraphQLNonNull(GraphQLString)
},
email: {
name: 'email',
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: async(parentValue, {name, email}) => {
const rules = new Rules();
rules.add('name', name, 'Name is required').isRequired();
rules.add('name', name, 'Min 3 characters').isLength({min: 3});
rules.add('name', name, 'Max 50 characters').isLength({max: 50});
rules.add('email', email, 'Email is required').isRequired();
rules.add('email', email, 'Valid email is required').isEmail();
rules.add('email', email, 'Max 50 characters').isLength({max: 50});
const errors = rules.validate();
if (errors) {
throw new BadRequestError(errors);
}
...
}
}
};
License
MIT