[英]Not able to write GraphQL Mutation for creating a new user in database
我要做的基本上是將新用戶插入數據庫,然后使用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
}
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;
}
您已將解析器定義為:
createEmployee: async (source) => await employeesRepo.createEmployee(source)
但是,您實際上要處理傳遞給該字段的input
參數,該參數位於傳遞給resolve
的第二個參數中。 請嘗試:
createEmployee: async (source, args) => await employeesRepo.createEmployee(args.input)
請在此處查看GraphQLFieldResolveFn
定義:
http://graphql.org/graphql-js/type/#graphqlobjecttype
type GraphQLFieldResolveFn = (
source?: any,
args?: {[argName: string]: any},
context?: any,
info?: GraphQLResolveInfo
) => any
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.