我目前正在尝试使用带有NodeJS的GraphQL,但不知道为什么以下查询会发生此错误:

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

我不确定自己的typeresolveLibrary是否正确,因为在任何示例中我都看过它们使用了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)。

最佳答案

我也遇到了这个问题。看来您从解析器返回的内容与架构中的返回类型不匹配。
专门针对错误消息Expected Iterable, but did not find one for field library.user.,您的架构需要一个数组(可迭代),但您没有在解析器中返回数组
我在schema.js中有这个:login(email: String, password: String): [SuccessfulLogin]我将其更改为:login(email: String, password: String): SuccessfulLogin请注意“SuccessfulLogin”周围的方括号。是否要更新解析程序返回类型或更新架构的期望完全取决于您

关于node.js - GraphQL预期可迭代,但未为字段xxx.yyy找到一个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46513476/

10-12 00:07
查看更多