当我启动服务器时,我得到这个错误
错误:未定义应为GraphQL类型。
更新:我相信这与javascript的相互要求有关。解决这个问题的最好方法是什么?
我在文件accountTypes.js中有一个帐户类型
const { channelType } = require('../channel/channelTypes');
//Define Order type
const accountType = new GraphQLObjectType({
name: 'Account',
description: 'An account',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLInt),
description: 'The id of the account.'
},
name: {
type: new GraphQLNonNull(GraphQLString),
description: 'Name of the account.'
},
channel: {
type: channelType,
resolve: resolver(db.Account.Channel),
description: 'Channel'
},
etc...
我的频道类型是channelTypes.js
const { accountType } = require('../account/accountTypes');
// Define Channel type
const channelType = new GraphQLObjectType({
name: 'Channel',
description: 'A Channel',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLInt),
description: 'ID of the channel.'
},
name: {
type: new GraphQLNonNull(GraphQLString),
description: 'Name of the channel',
deprecationReason: 'We split up the name into two'
},
accounts: {
type: new GraphQLList(accountType),
description: 'accounts associated with this channel',
resolve: resolver(db.Channel.Account)
}
})
});
这个问题的代码在我的channelTypes.js文件中。由于某些原因,accountType被认为是未定义的。我使用module.exports导出各自文件中的accountType和channelType。当我注释掉通道文件中的accountType代码时,该帐户工作得很好。我试图能够从帐户或与渠道相关联的所有帐户获得渠道,但目前只有从帐户端渠道工作。
发布于 2018-06-04 04:34:21
我回答了一个非常相似的问题here,但我认为它们有点不同。我试图在那里解释一下模块系统,但基本上答案是,在处理递归类型时,只需将其中一个类型的fields
属性包装在函数中。
编辑:也不要破坏模块对象的结构。当你有循环依赖时,循环依赖的模块将获得对模块的引用,但不会被初始化。当您随后对它们进行解构时,该变量将获得undefined
,因为该模块还没有任何属性。
const accountTypes = require('../account/accountTypes');
// Define Channel type
const channelType = new GraphQLObjectType({
name: 'Channel',
description: 'A Channel',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLInt),
description: 'ID of the channel.'
},
name: {
type: new GraphQLNonNull(GraphQLString),
description: 'Name of the channel',
deprecationReason: 'We split up the name into two'
},
accounts: {
type: new GraphQLList(accountTypes.accountType),
description: 'accounts associated with this channel',
resolve: resolver(db.Channel.Account)
}
})
});
https://stackoverflow.com/questions/50669249
复制相似问题