Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
852 views
in Technique[技术] by (71.8m points)

node.js - Related Graphql objects giving undefined

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...