我目前正在尝试使用 NodeJS 进行 GraphQL,但我不知道为什么以下查询会出现此错误:
{
library{
name,
user {
name
email
}
}
}
我不确定是否type
of my resolveLibrary
是对的,因为在任何例子中我都看过他们使用的new GraphQL.GraphQLList()
,但就我而言,我真的想返回单个用户对象,而不是用户数组。
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] /cdn-cgi/l/email-protection'
}
}
}
}
}
module.exports = resolveLibrary;
Error:
Error: Expected Iterable, but did not find one for field library.user.
So my library
模式提供了一个user
返回正确数据的字段(调用 console.log)。