What I want to do basically insert new user in to database and return new user data using GraphQL Mutation. But I'm not able to insert data in to database. In the below image getting null values instead of new user data. Can anyone tell me where exactly my code is wrong.
schema.JS
type Mutation {
createEmployee(input: EmployeeInput): Employee
}
input EmployeeInput {
firstName: String
lastName: String
phone: String
email: String
name: String
domainName: String
smsID: String
}
type Employee {
id: ID
adminFirstName: String
adminLastName: String
adminPhone: String
adminEmail: String
smsID: String
domainName: String
}
resolver.JS
import { employeesRepo } from "repos";
const mutationResolvers = {
createEmployee: async ({ firstName, lastName, email, phone, businessName, domainName }) =>
await employeesRepo.createEmployee(arguments[0])
};
employeesRepo.Js
async createEmployee(employee) {
let newEmployee = await this.employeeStore.insert(employee);
return newEmployee;
}
MongoStore.JS
async insert(document) {
let db, collection, result, now;
now = new Date();
document.createdOn = now;
document.lastUpdated = now;
document._id = new ObjectId();
try {
db = await MongoClient.connect(url, options);
collection = db.collection(this.collectionName);
result = await collection.insertOne(document);
} catch (err) {
console.log(err);
} finally {
db.close();
}
return document;
}
You have defined your resolver as:
createEmployee: async (source) => await employeesRepo.createEmployee(source)
However you actually want to process the input
argument passed to the field, which is in the second argument passed to resolve
. Try instead:
createEmployee: async (source, args) => await employeesRepo.createEmployee(args.input)
See the GraphQLFieldResolveFn
definition here:
http://graphql.org/graphql-js/type/#graphqlobjecttype
type GraphQLFieldResolveFn = (
source?: any,
args?: {[argName: string]: any},
context?: any,
info?: GraphQLResolveInfo
) => any
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.