I'm making an API server with Express, Graphql (Apollo server) and mongoose.
I'm testing the user creation. When the email is duplicated mongoose throws an error (Validation error. Unique = true) and graphql handles very good. But the console (terminal) shows the error too. How can i prevent that issue?
Resolver:
const MODEL_PATH = '../../models';
const User = require(MODEL_PATH + '/User');
const { register } = require('../../controllers/auth/RegisterController');
module.exports = {
RootQuery: {
users() {
return User.find({});
}
},
Mutation: {
registerUser(_, data) {
return register(data);
}
}
};
RegisterController (register function)
exports.register = function(data) {
const { email, password } = data;
const user = new User({
email,
password
});
return new Promise((resolve, reject) => {
user.save().then((user) => {
resolve(user);
}).catch((err) => {
reject(err);
});
});
};
And the error in the console (I DON'T WANT THAT. I HANDLED THE ERROR IN THE CONTROLLER. I WANT THE ERROR ONLY IN THE GRAPHQL RESPONSE)
MongoError: E11000 duplicate key error collection: y.users index: email_1 dup key: { : "test@example.com" }
at Function.MongoError.create (/Volumes/Datos/x/code/y/server/node_modules/mongodb-core/lib/error.js:31:11)
at toError (/Volumes/Datos/x/code/y/server/node_modules/mongodb/lib/utils.js:114:22)
....
Response in Graphiql (That is ok)
{
"data": {
"registerUser": null
},
"errors": [
{
"message": "E11000 duplicate key error collection: y.users index: email_1 dup key: { : \"test@example.com\" }",
"locations": [
{
"line": 9,
"column": 3
}
],
"path": [
"registerUser"
]
}
]
}
Thank you
check out apollo-errors
:
https://github.com/thebigredgeek/apollo-errors
and apollo-resolvers
:
https://github.com/thebigredgeek/apollo-resolvers
These packages, together, were made to handle issues like the one you describe
According to: http://dev.apollodata.com/tools/graphql-server/setup.html
in server
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myschema, debug: false }));
debug false did the trick.
Now. I'm check the packages for better error handling
Thanks
How we can insert header and footer in google docs with google docs api using PHP code
Writing a new and appending a file in PHP without erasing contents
How to combine 2 arrays with the condition that 2 and 5 values will be from the second array?
How to keep the ?error on url if page extension not included?
I have a question to ask yourself: I have a structuredtxt file:
Actually, I am building a web application that let users to compile their C, C++, Java, Python, C# and VB code for a given problem using Nodejs
I'm first time to use AWS, and I'm trying to install the jquery-chat from https://githubcom/lovelle/jquery-chat to test the demo live chat
I'm struggling to implement Application Layer Paging with the join-monster library following the docs: http://join-monsterreadthedocs