Reputation: 1798
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.
Upvotes: 0
Views: 627
Reputation: 7666
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)
}
})
});
Upvotes: 2