GraphQL 预期可迭代,但没有为字段 xxx.yyy 找到一个 [英] GraphQL Expected Iterable, but did not find one for field xxx.yyy

查看:10
本文介绍了GraphQL 预期可迭代,但没有为字段 xxx.yyy 找到一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
    }
  }
}

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

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.

我的代码:

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 被调用).

So my library schema provides a user field which returns the right data (the console.log is called).

推荐答案

我也遇到了这个问题.您从解析器返回的内容似乎与架构中的返回类型不匹配.

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.

特别是对于错误消息 Expected Iterable, but not find an for field library.user.,您的架构需要一个数组(Iterable),但您没有在解析器中返回一个数组

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

我的 schema.js 中有这个:

I had this in my schema.js:

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

我将其更改为:

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

注意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

这篇关于GraphQL 预期可迭代,但没有为字段 xxx.yyy 找到一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆