Skip to content

Instantly share code, notes, and snippets.

{
"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": [],

Module 3 - Routing with Feature Module

Video - 1 What is feature module?

  • 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

Video 2 - Creating Feature Module

@HaiderMalik12
HaiderMalik12 / section-01-routing-foundations-script.md
Created January 27, 2019 07:48
Section 01 Routing Foundations script or action steps

Video 1 - Define Routes

  1. Adding Bootstrap
  2. Create Navbar component
  3. Add Navbar html
  4. Display Navbar

Video 2 Configure Routes

  1. Create Component Posts, and About
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'})
}
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: {
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({
asyncAwait(req, res) {
const orderRequest = async () => {
try {
let resp = {};
const product = await Product.create({
name: 'MacbookPro',
qty: 1232213,
price: 10000.
});
resp.product = product;
promiseVersion(req, res) {
let data = {};
Product.create({
name: 'MacbookPro',
qty: 123,
price: 10000.9
})
.then(_product => {
data.product = _product;
return Order.findOne({
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);
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({