简体   繁体   English

无法编写用于在数据库中创建新用户的GraphQL突变

[英]Not able to write GraphQL Mutation for creating a new user in database

What I want to do basically insert new user in to database and return new user data using GraphQL Mutation. 我要做的基本上是将新用户插入数据库,然后使用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 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 resolver.JS

import { employeesRepo } from "repos";

const mutationResolvers = {
    createEmployee: async ({ firstName, lastName, email, phone, businessName, domainName }) =>
    await employeesRepo.createEmployee(arguments[0])
};

employeesRepo.Js employeesRepo.Js

async createEmployee(employee) {
let newEmployee = await this.employeeStore.insert(employee);
return newEmployee;

} }

MongoStore.JS 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 . 但是,您实际上要处理传递给该字段的input参数,该参数位于传递给resolve的第二个参数中。 Try instead: 请尝试:

createEmployee: async (source, args) => await employeesRepo.createEmployee(args.input)

See the GraphQLFieldResolveFn definition here: 请在此处查看GraphQLFieldResolveFn定义:

http://graphql.org/graphql-js/type/#graphqlobjecttype 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM