首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >提供未定义的相关Graphql对象

提供未定义的相关Graphql对象
EN

Stack Overflow用户
提问于 2018-06-04 01:43:36
回答 1查看 462关注 0票数 0

当我启动服务器时,我得到这个错误

错误:未定义应为GraphQL类型。

更新:我相信这与javascript的相互要求有关。解决这个问题的最好方法是什么?

我在文件accountTypes.js中有一个帐户类型

代码语言:javascript
复制
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

代码语言:javascript
复制
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代码时,该帐户工作得很好。我试图能够从帐户或与渠道相关联的所有帐户获得渠道,但目前只有从帐户端渠道工作。

EN

回答 1

Stack Overflow用户

发布于 2018-06-04 04:34:21

我回答了一个非常相似的问题here,但我认为它们有点不同。我试图在那里解释一下模块系统,但基本上答案是,在处理递归类型时,只需将其中一个类型的fields属性包装在函数中。

编辑:也不要破坏模块对象的结构。当你有循环依赖时,循环依赖的模块将获得对模块的引用,但不会被初始化。当您随后对它们进行解构时,该变量将获得undefined,因为该模块还没有任何属性。

代码语言:javascript
复制
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)
    }
  })
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50669249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档