Skip to content

Instantly share code, notes, and snippets.

View neerajkumar161's full-sized avatar
💭
Looking for Node.js Backend TypeScript Role

Neeraj Kumar neerajkumar161

💭
Looking for Node.js Backend TypeScript Role
View GitHub Profile
--config
--db
--src
--models
--resolvers
--schemas
server.ts
@neerajkumar161
neerajkumar161 / package.json
Last active January 15, 2022 15:56
Package.json after installing dependencies and adding start script
{
"name": "node-graphql-curd",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "pm2 start server.js --watch"
},
"keywords": [],
@neerajkumar161
neerajkumar161 / default.json
Last active January 5, 2022 04:38
config/default.json
{
"port": 8000,
"db_url": "mongodb+srv://<username>:<password>@sandbox.ejy8i.mongodb.net/graphqlDatabase?retryWrites=true&w=majority"
}
@neerajkumar161
neerajkumar161 / mongodb.ts
Last active December 25, 2021 07:57
db/mongodb.ts
import config from 'config'
import {connect} from 'mongoose' // import connect method from mongoose
const MONGO_URI: string = config.get('db_url') // This will get the db_url, that we've declared in config/default.json file
export const connectMongoDB = async () => {
try {
const res = await connect(MONGO_URI)
if (res) console.log('Connected to MongoDB')
} catch (error) {
@neerajkumar161
neerajkumar161 / server.ts
Last active December 29, 2021 14:15
server.ts
import {ApolloServerPluginDrainHttpServer} from 'apollo-server-core'
import {ApolloServer} from 'apollo-server-express'
import config from 'config'
import express from 'express'
import {GraphQLError} from 'graphql'
import http from 'http'
import {connectMongoDB} from './db/mongodb'
import {mainSchema} from './src/schema-loader' // will create in next steps, skip for now
const port = config.get('port') || 5000
@neerajkumar161
neerajkumar161 / user.ts
Created December 27, 2021 16:00
src/models/user.ts
import {model, Schema} from 'mongoose'
const user = new Schema(
{
firstname: {type: String},
lastname: {type: String},
age: {type: Number},
email: {type: String},
password: {type: String}
},
scalar Date
type User {
_id: ID!
firstname: String!
lastname: String!
email: String!
age: String!
createdAt: Date
updatedAt: Date
@neerajkumar161
neerajkumar161 / get-user.graphql
Created December 29, 2021 13:12
/src/schemas/get-user.graphql
type Query {
getUser(id: ID!): User!
}
@neerajkumar161
neerajkumar161 / create-user.ts
Last active December 29, 2021 14:14
/src/resolvers/create-user.ts
import {userModel} from '../models/user'
export const createUserResolver = async (parent: any, args: any, context: any) => {
let user = await userModel.find({email: args.email}).lean()
if (user.length) throw new Error('User already exists!')
user = await userModel.create(args)
if (!user) throw new Error('User creation failed')
return user
}
@neerajkumar161
neerajkumar161 / get-user.ts
Last active January 15, 2022 06:58
/src/resolvers/get-user.ts
import { userModel } from '../models/user';
export const getUserByIdResolver = async (parent: any, args: any, context: any) => {
const user = await userModel.findOne({_id: args.id}).lean()
if (!user) throw new Error('No user exists!')
return user
}