简体   繁体   English

如何从graphql突变而不是null返回新记录

[英]how to return new record from graphql mutation instead of null

I read through the apollo-server-tutorial and I'm trying to replicate it with only mongodb - no sqllite. 我通读了apollo-server-tutorial,并尝试仅使用mongodb-而不使用sqllite复制它。 I'm coming from meteor so I'm learning async & mongoose while I'm at it. 我来自流星,所以我正在学习异步和猫鼬。

My mutation for addAuthor() works correctly (I see a new record in the DB) but graphiql returns null. 我对addAuthor()的修改可以正常工作(我在数据库中看到一条新记录),但是graphiql返回null。 How do I get it to return the expected fields? 如何获得它返回期望的字段?

mutation {
  addAuthor(firstName: "falieson", lastName:"p") {
    _id
    firstName
    lastName
  }
}

{
  "data": {
    "addAuthor": null
  }
}

Schema: https://github.com/Falieson/apollo-server-tutorial-only-mongodb/blob/master/data/schema.js#L39 架构: https//github.com/Falieson/apollo-server-tutorial-only-mongodb/blob/master/data/schema.js#L39

Resolver: https://github.com/Falieson/apollo-server-tutorial-only-mongodb/blob/master/data/resolvers.js#L38 解析器: https//github.com/Falieson/apollo-server-tutorial-only-mongodb/blob/master/data/resolvers.js#L38

Mongoose: https://github.com/Falieson/apollo-server-tutorial-only-mongodb/blob/master/data/models/mongoose.js#L51 猫鼬: https : //github.com/Falieson/apollo-server-tutorial-only-mongodb/blob/master/data/models/mongoose.js#L51

Model: https://github.com/Falieson/apollo-server-tutorial-only-mongodb/blob/master/data/models/index.js#L7 型号: https : //github.com/Falieson/apollo-server-tutorial-only-mongodb/blob/master/data/models/index.js#L7

I needed to have mongoose return a promise for the mutation. 我必须让猫鼬返回该突变的承诺。 So my MongooseModel.create() changes from 所以我的MongooseModel.create()从

const record = new this.collection(args)
return record.save(cb)

to using Promise 使用Promise

const record = new this.collection(args)
return new Promise((resolve, reject) => {
   record.save((err, res) => {
      err ? reject(err): resolve(res)
   });
});

to using Async/Await 使用异步/等待

 async create(obj = {}) {
   const args = {
     ...this.defaultRecord(),
     ...obj
   }
   const record = new this.Collection(args)

   try {
     const savedRecord = await record.save()
     return savedRecord
   } catch (err) {
     handleError(err)
   }
 }

My mutation doesn't have to change at all, but my fixtures generator also has to be updated to use the Promise.then() chain. 我的突变根本不需要改变,但是我的装置生成器也必须更新以使用Promise.then()链。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的graphql服务器变异返回空值 - my graphql server mutation return null value 变异令牌一路返回 null Graphql - Mutation token all way return a null Graphql 在添加新架构字段后,aws cdk nextjs graphql 突变无法为不可空类型返回 null - aws cdk nextjs graphql mutation cannot return null for non-nullable type after adding in new schema field 从列表中删除记录,Apollo GraphQL乐观UI突变 - Deleting Record from List, Apollo GraphQL Optimistic UI Mutation 从另一个变异中调用GraphQL变异? - Calling GraphQL mutation from another mutation? 为什么 graphql 返回“不能返回 null of non-nullable field mutation.createAccnt” - why is graphql returning “cannot return null of non-nullable field mutation.createAccnt” 在 graphql 中更新/编辑用户数据(不能为不可空字段 Mutation.updateUser 返回 null) - Updating/editing user data in graphql(Cannot return null for non-nullable field Mutation.updateUser) GraphQL 错误:无法为不可为空的字段 Mutation.deleteComment 返回 null - GraphQL Error: Cannot return null for non-nullable field Mutation.deleteComment 如何从单个反应组件调用多个graphql突变? - How to call multiple graphql mutation from a single react component? 如何从GraphQL突变访问variableValues? - How do I access variableValues from GraphQL mutation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM