gpt4 book ai didi

node.js - 相关的 Graphql 对象给出未定义

转载 作者:行者123 更新时间:2023-12-03 22:27:43 26 4
gpt4 key购买 nike

启动服务器时出现此错误

Error: Expected undefined to be a GraphQL type.



更新:我相信这与需要彼此的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...

我的 channel 类型在 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。当我注释掉 channel 文件中的 accountType 代码时,该帐户可以正常工作。我试图能够从帐户或与 channel 关联的所有帐户获取 channel ,但目前只有来自帐户端的 channel 有效。

最佳答案

我回答了一个非常相似的问题 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)
}
})
});

关于node.js - 相关的 Graphql 对象给出未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50669249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com