- we can divided application into feature modules
- Do not put multiple business logic in a single app module
- The best practice is to break the application into multiple modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "gql-api", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "start": "nodemon ./index.js --exec babel-node -e js", | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "keywords": [], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = (req, res, next) => { | |
| try { | |
| //check if user is an admin or not | |
| // you can check it from user property from request object | |
| // I am assuming user property in request object | |
| // here you can find a user record on the based on id or you can save role property in jwt token | |
| if(req.userData.role !== 'admin'){ // or you create a number value 0= admin, 1 = user etc. | |
| next({err: 'Sorry You are not admin, You are not allowed to perform this action'}) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default { | |
| Query: { | |
| async allProducts(_, args, ctx) { | |
| return await ctx.models.product.find(); | |
| }, | |
| async getProduct(_, { _id }, ctx) { | |
| return await ctx.models.product.findById(_id); | |
| } | |
| }, | |
| Mutation: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| awaitPromises(req, res) { | |
| const makeRequest = async () => { | |
| //Run both promises in parallel | |
| const [product, order] = await Promise.all([ | |
| Product.create({ | |
| name: 'MacbookPro', | |
| qty: 1232213, | |
| price: 10000. | |
| }), | |
| Order.findOne({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| asyncAwait(req, res) { | |
| const orderRequest = async () => { | |
| try { | |
| let resp = {}; | |
| const product = await Product.create({ | |
| name: 'MacbookPro', | |
| qty: 1232213, | |
| price: 10000. | |
| }); | |
| resp.product = product; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| promiseVersion(req, res) { | |
| let data = {}; | |
| Product.create({ | |
| name: 'MacbookPro', | |
| qty: 123, | |
| price: 10000.9 | |
| }) | |
| .then(_product => { | |
| data.product = _product; | |
| return Order.findOne({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| asyncLib(req, res) { | |
| const _async = require('async'); | |
| const createProduct = cb => { | |
| Product.create({ | |
| name: 'MacbookPro', | |
| qty: 123, | |
| price: 10000.9 | |
| }, (err, _product) => { | |
| if (err) return cb(err); | |
| cb(null, _product); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| callbackHell(req, res) { | |
| //Create new Product | |
| Product.create({ | |
| name: 'MacbookPro', | |
| qty: 123, | |
| price: 10000.9 | |
| }, (err, _product) => { | |
| if (err) return res.serverError(err); | |
| //Find Order by orderNumber = 123 | |
| Order.findOne({ |
NewerOlder