GraphQL Expected Iterable,但没有找到字段 xxx.yyy 的值

我目前正在使用 NodeJS 尝试 GraphQL,我不知道为什么这个错误会出现在下面的查询中:

{
library{
name,
user {
name
email
}
}
}

我不确定我的 resolveLibrarytype是否正确,因为在任何示例中我都看到他们使用了 new GraphQL.GraphQLList(),但在我的示例中,我真的想返回一个用户对象,而不是一个用户数组。

我的代码:

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: 'test@123.de'
}
}
}
}
}


module.exports = resolveLibrary;

错误:

Error: Expected Iterable, but did not find one for field library.user.

因此,我的 library模式提供了一个返回正确数据的 user字段(调用 console.log)。

89458 次浏览

I guess your user is an instance of GraphQLList that is why the field user is expecting to resolve to an iterable object.

I ran into this problem as well. 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

I had this in my schema.js:

login(email: String, password: String): [SuccessfulLogin]

I changed that to:

login(email: String, password: String): SuccessfulLogin

Notice the square brackets around "SuccessfulLogin". It's up to you whether you want to update the resolver return type or update the schema's expectations

I had the same problem. I was using find instead filter.

In my case it was related to django-graphene I didn't have a resolve method defined.

class SomeNode(DjangoObjectType):
things = graphene.List(ThingNode)


def resolve_things(self, info, **kwargs):
return self.things.all()

I ran into the same issue but i was using GraphQL with Go.

Solution: I mentioned the return type to be a list( or you can say an array), but my resolver function was returning an interface and not a list of interfaces.

Before it was =>

Type: graphql.NewList(graphqll.UniversalType)

Later i changed it to =>

Type: graphqll.UniversalType

graphqll.UniversalType : 'graphqll' is the name of my user-defined package and 'UniversalType' is the GraphQL object i have created.

The previous structure of graphql object was :

var GetAllEmpDet = &graphql.Field{
Type: graphql.NewList(graphqll.UniversalType),
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
...
...
// Your resolver code goes here, how you handle.
...
return models.Universal, nil // models.Universal is struct and not list of struct so it gave that error.
},
}

It worked when i changed this to:

var GetAllEmpDet = &graphql.Field{
Type: graphqll.UniversalType,
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
...
...
// Your resolver code goes here, how you handle.
...
return models.Universal, nil // models.Universal is struct and not list of struct so it gave that error.
},
}

For me, it was a simple fix.

 items: {
type: new GraphQLList(VideoType),<-- error
resolve(parentValue, args) {
const url = 'www'


return axios.get(url)
.then(res => res.data);
}
}

and change it to

 items: {
type: VideoType,
resolve(parentValue, args) {
const url = 'www'


return axios.get(url)
.then(res => res.data);
}
}

i had the same issue i was using findOne and that seems like the issue that didnt worked. i changed to find and it worked

    @Query(()=> [Post])
async getSinglePost(
@Arg('post_id') id: string,
){
/*
const post = await getConnection().getRepository(Post).findOne({uuid:postuid})
console.log(post);
return post
*/


const post = Post.find({uuid:id})
return post
}

I faced the same issue. For me, it was an issue with Mongo DB model.js file.

GraphQL kept throwing that error because my model was saving the field as an object whereas graphQL was returning it as an array. The code that caused the error was this.

tableHeaders: {
            

text: {
type: String,
required: false,
},
align: {
type: String,
required: false,
},
sortable: {
type: Boolean,
required: false,
},
value: {
type: String,
required: false,
},
            

},

It was corrected to the following.

tableHeaders: [
{
text: {
type: String,
required: false,
},
align: {
type: String,
required: false,
},
sortable: {
type: Boolean,
required: false,
},
value: {
type: String,
required: false,
},
},
],

Changing type from object to array fixed it.

It's usually a simple mistake. Caused by declaring in the schema a List instead of a Field. The reverse will happen if you interchange. An example from Django-graphene. Switch from this:

my_query_name = graphene.List(MyModelType, id=graphene.Int())

to this:

my_query_name = graphene.Field(MyModelType, id=graphene.Int())

This simply results due to the import error earlier code

const books =require('./data')
// Resolvers define the technique for fetching the types defined in the
// schema. This resolver retrieves books from the "books" array above.
const resolvers = {
Query: {
books(){
return books;
},
},
}
module.exports = { resolvers };

just replace the import statement with

const {books} =require('./data')

as you had ex