我目前正在使用 NodeJS 尝试 GraphQL,我不知道为什么这个错误会出现在下面的查询中:
{
library{
name,
user {
name
email
}
}
}
我不确定我的 resolveLibrary
的 type
是否正确,因为在任何示例中我都看到他们使用了 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)。