node.js GraphQL 预期可迭代,但没有为字段 xxx.yyy 找到一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46513476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GraphQL Expected Iterable, but did not find one for field xxx.yyy
提问by VetterHyman
I'm currently trying GraphQL with NodeJS and I don't know, why this error occurs with the following query:
我目前正在使用 NodeJS 尝试 GraphQL,但我不知道为什么会在以下查询中出现此错误:
{
library{
name,
user {
name
email
}
}
}
I am not sure if the typeof my resolveLibraryis right, because at any example I had a look at they used new GraphQL.GraphQLList(), but in my case I really want to return a single user object, not an array of users.
我不确定type我的resolveLibrary是否正确,因为在任何示例中我都看过他们使用的new GraphQL.GraphQLList(),但在我的情况下,我真的想返回一个用户对象,而不是用户数组。
My code:
我的代码:
const GraphQL = require('graphql');
const DB = require('../database/db');
const user = require('./user').type;
const library = new GraphQL.GraphQLObjectType({
name: 'library',
description: `This represents a user's library`,
fields: () => {
return {
name: {
type: GraphQL.GraphQLString,
resolve(library) {
return library.name;
}
},
user: {
type: user,
resolve(library) {
console.log(library.user);
return library.user
}
}
}
}
});
const resolveLibrary = {
type: library,
resolve(root) {
return {
name: 'My fancy library',
user: {
name: 'User name',
email: {
email: '[email protected]'
}
}
}
}
}
module.exports = resolveLibrary;
Error:
错误:
Error: Expected Iterable, but did not find one for field library.user.
So my libraryschema provides a userfield which returns the right data (the console.log is called).
所以我的library模式提供了一个user返回正确数据的字段(调用了 console.log)。
回答by Vineeth Pradhan
I ran into this problem as well but it seems quite obvious why this happens. It appears that what you're returning from your resolver doesn't match the return type in your schema.
我也遇到了这个问题,但为什么会发生这种情况似乎很明显。您从解析器返回的内容似乎与架构中的返回类型不匹配。
Specifically for the error message Expected Iterable, but did not find one for field library.user., your schema expects an array(Iterable) but you aren't returning an array in your resolver
特别是对于错误消息Expected Iterable, but did not find one for field library.user.,您的架构需要一个数组(Iterable),但您没有在解析器中返回一个数组
I had this in my schema.js:
我在我的 schema.js 中有这个:
login(email: String, password: String): [SuccessfulLogin]
login(email: String, password: String): [SuccessfulLogin]
I changed that to:
我把它改成:
login(email: String, password: String): SuccessfulLogin
login(email: String, password: String): SuccessfulLogin
Notice the square brackets around "SuccessfulLogin". It's upto you whether you want to update the resolver return type or update the schema's expectations
注意“SuccessfulLogin”周围的方括号。是否要更新解析器返回类型或更新架构的期望取决于您
回答by Uziel Valdez
I guess your useris an instance of GraphQLListthat is why the field user is expecting to resolve to an iterable object.
我猜你user是一个实例,GraphQLList这就是为什么字段用户期望解析为可迭代对象的原因。
回答by Alfrex92
I had the same problem. I was using find instead filter.
我有同样的问题。我使用的是 find 而不是过滤器。
回答by jmunsch
In my case it was related to django-grapheneI didn't have a resolve method defined.
就我而言,这与django-graphene我没有定义解析方法有关。
class SomeNode(DjangoObjectType):
things = graphene.List(ThingNode)
def resolve_things(self, info, **kwargs):
return self.things.all()

