This file contains 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
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')) |
This file contains 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
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!'} | |
} |
This file contains 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
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 |
This file contains 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
type Mutation { | |
deleteUser(email: String!): deleteUser! | |
} | |
type deleteUser { | |
message: String! | |
} |
This file contains 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
type Mutation { | |
updateUser( | |
email: String! | |
firstname: String | |
lastname: String | |
age: Int | |
password: String | |
): User! | |
} |
This file contains 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
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) |
This file contains 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
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 | |
} |
This file contains 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
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 | |
} |
This file contains 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
type Query { | |
getUser(id: ID!): User! | |
} |
This file contains 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
scalar Date | |
type User { | |
_id: ID! | |
firstname: String! | |
lastname: String! | |
email: String! | |
age: String! | |
createdAt: Date | |
updatedAt: Date |
NewerOlder