Cleanup and apply pr suggestions

This commit is contained in:
Chi Vinh Le
2017-09-07 15:38:39 +07:00
parent b1b3db51a6
commit 43b7326db4
6 changed files with 85 additions and 94 deletions
+6 -22
View File
@@ -1,28 +1,8 @@
const {
GraphQLObjectType,
GraphQLInterfaceType
} = require('graphql');
const {forEachField} = require('./utils');
const {maskErrors} = require('graphql-errors');
const errors = require('../errors');
const {Error: {ValidationError}} = require('mongoose');
// This function is pretty much copied verbatim from the graphql-tools repo:
// https://github.com/apollographql/graphql-tools/blob/b12973c86e00be209d04af0184780998056051c4/src/schemaGenerator.ts#L180-L194
const forEachField = (schema, fn) => {
const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach((typeName) => {
const type = typeMap[typeName];
if (type instanceof GraphQLObjectType || type instanceof GraphQLInterfaceType) {
const fields = type.getFields();
Object.keys(fields).forEach((fieldName) => {
const field = fields[fieldName];
fn(field, typeName, fieldName);
});
}
});
};
// If an APIError happens in a mutation, then respond with `{errors: Array}`
// according to the schema.
const decorateWithMutationErrorHandler = (field) => {
@@ -47,7 +27,11 @@ const decorateWithMutationErrorHandler = (field) => {
};
};
// Masks errors during production and handle mutation errors inside the schema.
/**
* Masks errors during production and handle mutation errors inside the schema.
* @param {GraphQLSchema} schema the schema to decorate
* @return {void}
*/
const decorateWithErrorHandler = (schema) => {
forEachField(schema, (field, typeName) => {