相关的 Graphql 对象给出未定义 [英] Related Graphql objects giving undefined

查看:16
本文介绍了相关的 Graphql 对象给出未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting this error when I start the server

错误:预期未定义为 GraphQL 类型.

Error: Expected undefined to be a GraphQL type.

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

UPDATE: I believe this has to do with javascript requiring each other. What is the best way to solve this?

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

I have an account type in file 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 中

And my channel type is in 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 代码时,该帐户可以正常工作.我正在尝试能够从帐户或与频道关联的所有帐户获取频道,但目前只有来自帐户端的频道有效.

The code with the issue is in my channelTypes.js file. the accountType is coming over as undefined for some reason. I am using module.exports to export accountType and channelType in the respective files. The account works perfectly fine when i comment out the accountType code in the channel file. I am trying to be able to get the channel from account or all accounts associated with a channel but currently only the channel from account side works.

推荐答案

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

I answered a very similar question here but I think they are a bit different. I tried to explain the module system there a bit but basically the answer is that when dealing with recursive types simply wrap the fields property of one of the types in a function.

不要解构模块对象.当您有循环依赖时,循环依赖的模块将获得对该模块的引用,但不会被初始化.当你解构它们时,变量将得到 undefined 因为模块还没有属性.

Also don't destructure the module object. When you have cyclic dependencies the cyclicly dependent modules will get the reference to the module but will not be initialised. When you then destructure them the variable will get undefined since the module has no properties yet.

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)
    }
  })
});

这篇关于相关的 Graphql 对象给出未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆