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
@neerajkumar161
neerajkumar161 / schema-loader.ts
Created January 4, 2022 13:39
/src/schema-loader.ts
import {loadFilesSync} from '@graphql-tools/load-files'
import {mergeTypeDefs} from '@graphql-tools/merge'
import {makeExecutableSchema} from '@graphql-tools/schema'
import * as path from 'path'
import { getUserByIdResolver } from './resolvers/get-user'
import { createUserResolver } from './resolvers/create-user'
import { updateUserResolver } from './resolvers/update-user'
import { deleteUserResolver } from './resolvers/delete-user'
const ROOT_SCHEMA_PATH = loadFilesSync(path.join(__dirname, './schemas'))
@neerajkumar161
neerajkumar161 / delete-user.ts
Last active January 15, 2022 07:13
/src/resolvers/delete-user.ts
import { userModel } from '../models/user'
export const deleteUserResolver = async (parent: any, args: any, context: any) => {
let user = await userModel.findOne({email: args.email})
if (!user) throw new Error('User not exists!')
await userModel.findOneAndRemove({_id: user._id})
return {message: 'User Deleted!'}
}
@neerajkumar161
neerajkumar161 / update-user.ts
Last active January 15, 2022 07:13
/src/resolvers/update-user.ts
import { userModel } from '../models/user'
export const updateUserResolver = async (parent: any, args: any, context: any) => {
let user = await userModel.findOne({email: args.email}).lean()
if (!user) throw new Error('User not exists!')
const updateUser = {email: user.email, ...args}
user = await userModel
.findByIdAndUpdate(user._id, updateUser, {new: true})
.lean()
return user
@neerajkumar161
neerajkumar161 / delete-user.graphql
Last active January 4, 2022 13:19
/src/schema/delete-user.graphql
type Mutation {
deleteUser(email: String!): deleteUser!
}
type deleteUser {
message: String!
}
@neerajkumar161
neerajkumar161 / update-user.graphql
Created January 4, 2022 13:05
/src/schema/update-user.graphql
type Mutation {
updateUser(
email: String!
firstname: String
lastname: String
age: Int
password: String
): User!
}
@neerajkumar161
neerajkumar161 / schema-loader.ts
Created December 29, 2021 14:48
/src/schema-loader.ts
import {loadFilesSync} from '@graphql-tools/load-files'
import {mergeTypeDefs} from '@graphql-tools/merge'
import {makeExecutableSchema} from '@graphql-tools/schema'
import * as path from 'path'
import { getUserByIdResolver } from './resolvers/get-user'
import { createUserResolver } from './resolvers/create-user'
const ROOT_SCHEMA_PATH = loadFilesSync(path.join(__dirname, './schemas'))
const TypeDefinition = mergeTypeDefs(ROOT_SCHEMA_PATH)
@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
}
@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.graphql
Created December 29, 2021 13:12
/src/schemas/get-user.graphql
type Query {
getUser(id: ID!): User!
}
scalar Date
type User {
_id: ID!
firstname: String!
lastname: String!
email: String!
age: String!
createdAt: Date
updatedAt: Date