【问题标题】:Related Graphql objects giving undefined相关的 Graphql 对象给出未定义
【发布时间】:2018-11-13 02:52:58
【问题描述】:

我在启动服务器时收到此错误

错误:预期未定义为 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 代码时,该帐户可以正常工作。我正在尝试能够从帐户或与频道关联的所有帐户获取频道,但目前只有来自帐户端的频道有效。

【问题讨论】:

    标签: node.js sequelize.js graphql


    【解决方案1】:

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

    【讨论】:

    • 我尝试了您的建议,该建议以前在单个文件中有效,但在此场景中无效。是因为需要吗?
    • 我认为问题可能是所需模块的解构,因为它不像 ESM 那样工作。尝试改用accountTypes.accountType
    • 嘿@Herku,不知道你用那个代替是什么意思?
    • 我调整了答案。最好的办法是为此使用 EcmaScript 模块并使用转译器来帮助您。这样你就不会遇到那么多 JavaScript 陷阱...
    猜你喜欢
    • 1970-01-01
    • 2015-04-28
    • 2019-03-10
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多