GraphQL参数错误:参数types必须是inputtypes,但得到:function GraphQLObjectType(config){

在服务器启动( node index.js )我得到以下错误与我的GraphQL NodeJS服务器:

错误:Query.payment(data :)参数types必须是inputtypes,但得到:function GraphQLObjectType(config){_classCallCheck(this,GraphQLObjectType);

这个错误发生时,我改变了我的原始参数从string

  args: { data: { type: graphQL.GraphQLString } }, 

对于一个对象types:

  args: { data: { type: graphQL.GraphQLObjectType } }, 

我需要一个对象types,因为我需要发送几个字段作为参数。

GraphQL服务器:

 var Query = new graphQL.GraphQLObjectType({ name: 'Query', fields: { payment: { type: graphQL.GraphQLString, args: { data: { type: graphQL.GraphQLObjectType } }, resolve: function (_, args) { // There will be more data here, // but ultimately I want to return a string return 'success!'; } } } }); 

我怎样才能让它接受一个对象?


前端 (如果需要的话,但我甚至发送这个错误之前):

 var userQuery = encodeURIComponent('{ payment ( data: { user : "test" } )}'); $.get('http://localhost:4000/graphql?query=' + userQuery, function (res) { //stuff }); 

如果你想使用Object作为参数,你应该使用GraphQLInputObjectType而不是GraphQLObjectType 。 请记住,GraphQL是基于强types的,因此不允许使用通用GraphQLObjectType作为argtypes,然后dynamic查询args。 你必须明确地定义在这个input对象中的所有可能的字段(并且select哪个是强制的,哪个不是)

尝试使用这种方法:

 // your arg input object var inputType = new GraphQLInputObjectType({ name: 'paymentInput', fields: { user: { type: new GraphQLNonNull(GraphQLString) }, order: { type: GraphQLString }, ...another fields } }); var Query = new graphQL.GraphQLObjectType({ name: 'Query', fields: { payment: { type: graphQL.GraphQLString, args: { data: { type: new GraphQLNonNull(inputType) } }, resolve: function (_, args) { // There will be more data here, // but ultimately I want to return a string return 'success!'; } } } });