Skip to content

Instantly share code, notes, and snippets.

@ssaumyaranjan7
Created June 23, 2019 19:35
Show Gist options
  • Save ssaumyaranjan7/4a76ea44b73e303ef2a3a023fb53ad90 to your computer and use it in GitHub Desktop.
Save ssaumyaranjan7/4a76ea44b73e303ef2a3a023fb53ad90 to your computer and use it in GitHub Desktop.
This is the resolver files
// Imported the employee model from the models directory
const models = require(`../models/index`);
const employeeResolvers = {
Query: {
/**
* This method will return an array of employees according to the query
*/
employees: (root, args, context, info) => {
/**
* This will find all the employees from the employees collection
*/
return models.employee.find({})
},
/**
* This method will find an employee details with respect to an ID.
*/
employee: (root, { id }, context, info) => {
return models.employee.findById(id)
},
},
Mutation: {
/**
* This method will create an employee with parameters from the arguments
*/
signUp: (root, args, context, info) => {
return models.employee.create(args)
},
/**
* this method will update an employee data with respect to an id
*/
update: (root, { id, firstName, lastName, email, password }, context, info) => {
let updateInfo = {};
return models.employee.findById(id).then((newEmployee) => {
firstName ? newEmployee.firstName = firstName: "";
lastName ? newEmployee.lastName = firstName: "";
email ? newEmployee.email = email: "";
password ? newEmployee.password = password: "";
return newEmployee.save()
})
},
/**
* this method will delete an employee with respect to an ID
*/
delete: (root, { id }, context, info) => {
return models.employee.findByIdAndRemove(id)
}
}
}
module.exports = employeeResolvers;
// Imported both the resolvers for indexing
const messageResolvers = require(`./message.resolvers`);
const employeeResolvers = require(`./employee.resolvers`);
// Added all the resolvers into an array.
const resolvers = [messageResolvers, employeeResolvers] ;
// exported resolvers as a object.
module.exports = { resolvers };
const messageResolvers = {
Query: {
hello: () => 'Hello world'
}
}
module.exports = messageResolvers ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment