如何解决嵌套graphql突变?

时间:2019-03-15 10:20:44

标签: mongoose nested graphql mutation resolver

我是graphql的新手,我找不到解决graphql中嵌套变异的好教程。我尝试如下,但是在graphiQL中提交时在子部分中得到未定义的错误。我感谢在这方面的任何建议。 谢谢。

                //Graphql  Schema part
                const Family = require("./models/Family");
                const { buildSchema } = require("graphql");
                const mongoose = require("mongoose");

                app.use(
                  "/api",
                  graphqlHttp({
                    schema: buildSchema(`
                    input ChildInput {
                        field1: String!
                        field2: String!
                    }
                    input ParentInput {
                        field3: String!
                        field4: String!
                        children: [ChildInput!]
                    }
                  type RootMutation{
                    createFamily(childInput: ChildInput, parentInput: ParentInput):Family
                  }
                schema{
                      mutation:RootMutation
                    }`),// resolver
                    rootValue: {
                    createFamily: args => {
                        const newfamily = new Family({
                            field3: args.parentInput.field3,
                            field4: args.parentInput.field4,
                            children:[{
                            field1: args.childInput.field1,
                            field2: args.childInput.field2
                            }]
                        });
                        return newfamily
                          .save()
                          .then(result => {
                            console.log(result);
                            return { ...result._doc };
                          })
                          .catch(err => {
                            throw err;
                          });
                },
                    graphiql: true
                  })

0 个答案:

没有答案