[英]Many to Many graphql schema error
I'm new with GrpahQL and I'm trying to simulate Many to Many relationship between Users and Groups. 我是GrpahQL的新手,正在尝试模拟“用户和组”之间的多对多关系。 I have the followinf types defined in my schema:
我在架构中定义了followinf类型:
// 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;
and this is the other file: 这是另一个文件:
// 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;
This example doesn't work for me because for some reason i got this error: 这个例子对我不起作用,因为由于某种原因我得到了这个错误:
Error: Can only create List of a GraphQLType but got: [object Object].
错误:只能创建GraphQLType的列表,但得到:[对象对象]。
This error happens only for the GroupType and not for the UserType when both are similars. 当两者相似时,只会对GroupType而不是UserType发生此错误。 What's going on here?
这里发生了什么? What am I doing wrong?
我究竟做错了什么?
The problem is that UserType
requires GroupType
, and GroupType
requires UserType
: this is known as a circular dependency. 问题在于
UserType
需要GroupType
,而GroupType
需要UserType
:这称为循环依赖关系。
What happens is that UserType.js
gets required, exports an {}
while finishing to run (this is standard Node.js module execution), requires GroupType
, which requires back UserType
and gets an empty object back, and exports the correct GraphQL GroupType
to UserType
. 结果是需要
UserType.js
,在运行时导出{}
(这是标准的Node.js模块执行),需要GroupType
,这需要返回UserType
并返回一个空对象,然后将正确的GraphQL GroupType
到UserType
。 So UserType
works because it is a list of GroupType
, but GroupType
doesn't it got an empty object for its require of UserType. 所以
UserType
可以工作,因为它是GroupType
的列表,但是GroupType
并没有为UserType提供空对象。
To circumvent this, you can use a runtime require in GroupType.js
: 为了避免这种情况,您可以在
GroupType.js
使用运行时require:
// GroupType.js
...
// Remove the line which requires UserType at the top
// const UserType = require('./UserType');
const AuthService = require('../../services/AuthService');
...
const GroupType = new GraphQLObjectType({
...
fields: () => ({
...
users: {
type: new GraphQLList(require('./UserType')), // Require UserType at runtime
...
}
})
});
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.