【发布时间】:2018-02-17 09:39:49
【问题描述】:
我想做的基本上是将新用户插入数据库并使用 GraphQL Mutation 返回新用户数据。但我无法将数据插入到 数据库。在下图中获取空值而不是新的用户数据。谁能告诉我我的代码到底哪里错了。
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
}
解析器.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;
}
【问题讨论】:
标签: javascript node.js mongodb graphql