在GraphQL中将数组作为参数传递

N.A*_*N.A 5 facebook node.js graphql

我已经开始研究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}
  ]
}
  }
   });
Run Code Online (Sandbox Code Playgroud)

AND查询如下:

  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);
      }
    }
})
})
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误. 在此输入图像描述

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

Jus*_*tin 2

我最近做了一些非常类似的事情。您的输入参数不需要与格式化要发回的输出数据时保持相同的类型系统。因此,您的 arg 只需接受字符串或对象的列表,或者您想要发送的任何标准类型。

在本例中,我将其更新为接受字符串列表(数组)。

 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);
      }
    }
})
})
Run Code Online (Sandbox Code Playgroud)

另外,我注意到您的用户类型,您的度数被数组括号包围。与度数输入参数类似,您将输出一个字符串数组。尝试这样的事情:

  degrees:{
    type:new graphql.GraphQLList(graphql.GraphQLString)
  }
Run Code Online (Sandbox Code Playgroud)

快乐编码!