Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用嵌套模式进行GrahpQL变异_Javascript_Api_Graphql - Fatal编程技术网

Javascript 如何使用嵌套模式进行GrahpQL变异

Javascript 如何使用嵌套模式进行GrahpQL变异,javascript,api,graphql,Javascript,Api,Graphql,我一直在编写一个使用GraphQL的API。我对它还是相当陌生的,并且遇到了一些关于突变的问题。我的API的一种过于简单的形式有两种记录类型。有一个联系人记录和一个标签记录。联系人记录可以有多个与之关联的标记记录 我为每种记录类型编写的模式如下: const Tag = new graphQL.GraphQLObjectType({ name: 'Tag', description: 'Categorizes records into meaningful groups', fields: ()

我一直在编写一个使用GraphQL的API。我对它还是相当陌生的,并且遇到了一些关于突变的问题。我的API的一种过于简单的形式有两种记录类型。有一个联系人记录和一个标签记录。联系人记录可以有多个与之关联的标记记录

我为每种记录类型编写的模式如下:

const Tag = new graphQL.GraphQLObjectType({
name: 'Tag',
description: 'Categorizes records into meaningful groups',
fields: () => ({
  _id: {
    type: graphQL.GraphQLID
  },
  name: {
    type: graphQL.GraphQLString
  }
 })
});

const Contact = new graphQL.GraphQLObjectType({
name: 'Contact',
description: 'Contact record',
fields: () => ({
  _id: {
    type: graphQL.GraphQLID
  },
  name: {
    type: graphQL.GraphQLString
  },
  tags: {
    type: new graphQL.GraphQLList(Tag),
    resolve: function(src, args, context) {
      return TagModel.findByContactId(src._id)
        .then(tags => {
          return Promise.map(tags, (tag) => {
            return TagModel.findById(tag.tag_id);
          });
        });
      }
    }
   })
  });
我可以很容易地对标签之类的记录进行变异,因为它们不包含自己的嵌套记录,但我不确定如何对联系人之类的记录进行变异,因为它也可以包含标签。我放置的变异代码如下所示:

const Mutation = new graphQL.GraphQLObjectType({
name: 'Mutation',
fields: {
  createContact: {
  type: Contact,
  description: "Create Contact",
  args: {
    name: {type: new graphQL.GraphQLNonNull(graphQL.GraphQLString)},
    tags: {type: new graphQL.GraphQLList(Tag)}
  },
  resolve: function(source, args) {
    return ContactModel.save(args.name);
  }
  } 
 }
});
{"query": "mutation createNewContact { 
contact: createContact (name: "John Smith", tags { name: "family" } ) 
{_id, text, tags { name } } }" }
我不知道如何在变异中完成解析器,以便能够同时保存联系人和标签记录。例如,如果我进行了一次变异查询,用一个新标记保存一个新的联系人记录,如下所示:

const Mutation = new graphQL.GraphQLObjectType({
name: 'Mutation',
fields: {
  createContact: {
  type: Contact,
  description: "Create Contact",
  args: {
    name: {type: new graphQL.GraphQLNonNull(graphQL.GraphQLString)},
    tags: {type: new graphQL.GraphQLList(Tag)}
  },
  resolve: function(source, args) {
    return ContactModel.save(args.name);
  }
  } 
 }
});
{"query": "mutation createNewContact { 
contact: createContact (name: "John Smith", tags { name: "family" } ) 
{_id, text, tags { name } } }" }

为了允许这种类型的突变发生,我需要在我的突变模式中做一些特殊的事情吗?

您不能将
标记
用作输入对象类型,您必须创建一个类似
标记输入
的类型

const TagInput = new GraphQLInputObjectType({
    name: 'TagInput',
    fields: {
        _id: { type: GraphQLID },
        name: { type: GraphQLString } 
    }
});
建议始终创建正常类型的
Input
版本。通过创建
ContactInput
,您可以对
联系人执行相同的操作。然后你可以用和你做的非常相似的方式创造一个突变

const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        createContact: {
            type: Contact,
            args: {
                contact: { type: new GraphQLNonNull(ContactInput) },
                tags: { type: new GraphQLList(TagInput) }
            },
            resolve: (root, args, context) => {
                console.log(args);
                // this would console something like
                // { contact: { name: 'contact name' },
                //   tags: [ { name: 'tag#1' }, { name: 'tag#2' } ] }
                // here create contact with tags
            }
    }
});
您要运行的
查询将如下所示

{
    "operationName": "createContact",
    "query": "mutation createContact($contact: ContactInput!, $tags: [TagInput])
              {
                  createContact(contact: $contact, tags: $tags) {
                      _id
                      text
                      tags {
                          name
                      }
                  }
              }",
    "variables": {
        contact: { name: "contact name" },
        tags: [ { name: "tag#1" }, { name: "tag#2" } ]
    }
}

这很有效。谢谢不过,我确实有一个问题,那就是如何在响应中返回标记数据。即使我只是在解析器中返回参数,而不做任何其他操作,所有属性都会返回,但标记只是一个空数组。类似于{contact:_id:1,name:“John”,标记:[]}。我是否缺少返回标记数据所必需的内容?