Related Graphql objects giving undefined

流过昼夜 提交于 2021-02-04 06:50:44

问题


I am getting this error when I start the server

Error: Expected undefined to be a GraphQL type.

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

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...

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

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.


回答1:


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.

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


来源:https://stackoverflow.com/questions/50669249/related-graphql-objects-giving-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!