在GraphQL中将数组作为parameter passing

我已经开始研究GraphQL。我的模式也包含一个列表项目。

以下是我的模式的代码:

var userType = new graphql.GraphQLObjectType({ name: 'user', fields: function () { return { _id: { type: graphql.GraphQLID }, name: { type: graphql.GraphQLString }, age: { type: graphql.GraphQLString }, degrees:[ {type:graphql.GraphQLList} ] } } }); 

并且查询如下:

  var QueryType = new graphql.GraphQLObjectType({ name: 'Query', fields: () => ({ userArr: { type: new graphql.GraphQLList(userType), args:{ degrees:{type:new graphql.GraphQLList(userType)} }, resolve: function(source, args) { console.log(args); resolve(args); } } }) }) 

我得到这个错误。 在这里输入图像描述

基本上我需要从客户端graphql查询发布数组,并必须相应地定义查询,我无法实现。 任何build议,因为我找不到任何帮助,在这个问题..

我最近做了类似的事情。 您的input参数不需要保存到格式化输出数据以发送回相同的types系统。 所以你的arg只需要接受一个string或对象的列表,或者你想要发送的任何标准types。

在这种情况下,我更新它接受string列表(数组)。

  var QueryType = new graphql.GraphQLObjectType({ name: 'Query', fields: () => ({ userArr: { type: new graphql.GraphQLList(userType), args:{ degrees:{type:new graphql.GraphQLList(graphql.GraphQLString)} }, resolve: function(source, args) { console.log(args); resolve(args); } } }) }) 

另外,我注意到你的用户types,你有包围arrays括号度。 类似于你的学位input参数,你将输出一个string数组。 尝试这样的事情:

  degrees:{ type:new graphql.GraphQLList(graphql.GraphQLString) } 

快乐的编码!

GraphQLObjectType不是有效的inputtypes。

请参阅突变和inputtypes

“inputtypes不能包含其他对象的字段,只能是基本的标量types,列表types和其他inputtypes。”

您可以使用上面的build议,因为GraphQLString是一个标量

 degrees:{ type:new graphql.GraphQLList(graphql.GraphQLString) } 

否则,你需要定义一个GraphQLInputObjectType

 const userInputType = new GraphQLInputObjectType({ name: 'userInput', fields: { /* put your fields here */ } }); /* some code in between */ degrees:{ type:new graphql.GraphQLList(userInputType) }