Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

node.js - GraphQL Expected Iterable, but did not find one for field xxx.yyy

I'm currently trying GraphQL with NodeJS and I don't know, why this error occurs with the following query:

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

I am not sure if the type of my resolveLibrary is 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.

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 library schema provides a user field which returns the right data (the console.log is called).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...