我是 GrpahQL 的新手,我正在尝试模拟用户和组之间的多对多关系。我的架构中定义了以下类型:
// UserType.js
const {
GraphQLObjectType,
GraphQLString,
GraphQLList,
GraphQLID } = require('graphql');
const {
GraphQLEmail } = require('graphql-custom-types');
const GroupType = require('./GroupType'); const AuthService = require('../../services/AuthService');
let authService = new AuthService();
const UserType = new GraphQLObjectType({
name: "UserType",
fields: () => ({
id: { type: GraphQLID },
user: { type: GraphQLString },
password: { type: GraphQLString },
name: { type: GraphQLString },
lastname: { type: GraphQLString },
email: { type: GraphQLEmail },
groups: {
type: new GraphQLList(GroupType),
resolve(parentValue) {
return authService.userGroups(userId);
}
}
}) });
module.exports = UserType;
这是另一个文件:
// GroupType.js
const {
GraphQLObjectType,
GraphQLString,
GraphQLID,
GraphQLList
} = require('graphql');
const UserType = require('./UserType');
const AuthService = require('../../services/AuthService');
let authService = new AuthService();
const GroupType = new GraphQLObjectType({
name: "GroupType",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
description: { type: GraphQLString },
users: {
type: new GraphQLList(UserType),
resolve(parentArgs) {
return authService.userGroups(parentArgs.id);
}
}
})
});
module.exports = GroupType;
这个例子对我不起作用,因为由于某种原因我得到了这个错误:
错误:只能创建 GraphQLType 的列表,但得到:[object Object]。
此错误仅发生在 GroupType 而不是 UserType 当两者相似时。这里发生了什么?我究竟做错了什么?