Reputation: 3591
I am getting following error:
Error: Can only create List of a GraphQLType but got: [object Object].
But I am passing a GraphQLType.
This is the file triggering the error.
```
var GraphQL = require('graphql');
var BotType = require('../types/bot-type');
var Datastore = require('../../datastores/memory-datastore')
const BotListType = new GraphQL.GraphQLList(BotType);
module.exports = new GraphQL.GraphQLObjectType({
type: BotListType,
resolve: function(object) {
return object.bots.map(Datastore.getBot)
}
})
```
This is the BotType it's complaining about
```
var GraphQL = require('graphql');
var RelayQL = require('graphql-relay');
var Node = require('../node');
var IntegrationListField = require('../fields/integration-list-field')
const BotType = new GraphQL.GraphQLObjectType({
name: 'Bot',
fields: {
id: RelayQL.globalIdField('Bot'),
name: { type: GraphQL.GraphQLString },
integrations: IntegrationListField
},
interfaces: [ Node.nodeInterface ]
});
module.exports = BotType
```
Upvotes: 1
Views: 1339
Reputation: 8052
I had this error and it was actually caused by a circular dependency. This is lightly documented in this issue https://github.com/graphql/graphql-js/issues/467
To fix this I had to move my require statement inside my fields definition to break the circular dependency. To be clear the two types still depend on each other, but the dont load the dependency until a request is made, at which point the types have been loaded by graphql.
Some (pseudo) code to demonstrate.
Before:
const aType = require('../../a/types/a.type')
const bType = new graphql.GraphQLObjectType({
name: 'Portfolio',
fields: () => {
return {
listOfAs: {
type: new graphql.GraphQLList(aType),
resolve: (portfolio, args, context) => {
return ...
}
}
}
}
})
After:
const bType = new graphql.GraphQLObjectType({
name: 'Portfolio',
fields: () => {
const aType = require('../../a/types/a.type')
return {
listOfAs: {
type: new graphql.GraphQLList(aType),
resolve: (portfolio, args, context) => {
return ...
}
}
}
}
})
Upvotes: 2
Reputation: 1128
I've tried to reproduce your schema on my localhost, and got no error about GraphQLList.
However, I get error (on first file)
Error: Type must be named.
since I've noticed that you have typed wrong GraphQLObjectType definition in your first file. It seems to me, that you've tried to define a field, instead of type.
module.exports = new GraphQL.GraphQLObjectType({
name: 'BotListType',
fields: () => ({
list: {
type: new GraphQL.GraphQLList(BotType),
resolve: function(object) {
return object.bots.map(Datastore.getBot)
}
}
})
});
I'm using GraphQL version 0.6.2
Upvotes: 2