GraphQL Error - Error: Can only create List of a GraphQLType but got: [object Object]
GraphQL Error - Error: Can only create List of a GraphQLType but got: [object Object]
我收到以下错误:
Error: Can only create List of a GraphQLType but got: [object Object].
但是我传递的是 GraphQLType。
这是触发错误的文件。
```
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)
}
})
```
这是它抱怨的 BotType
```
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
```
我已尝试在我的本地主机上重现您的架构,但没有发现有关 GraphQLList 的错误。
但是,我收到错误(第一个文件)
Error: Type must be named.
因为我注意到您在第一个文件中键入了错误的 GraphQLObjectType 定义。在我看来,您试图定义一个字段,而不是类型。
module.exports = new GraphQL.GraphQLObjectType({
name: 'BotListType',
fields: () => ({
list: {
type: new GraphQL.GraphQLList(BotType),
resolve: function(object) {
return object.bots.map(Datastore.getBot)
}
}
})
});
我正在使用 GraphQL 版本 0.6.2
我有这个错误,它实际上是由循环依赖引起的。这在本期 https://github.com/graphql/graphql-js/issues/467
中有详细记录
为了解决这个问题,我不得不在我的字段定义中移动我的 require 语句来打破循环依赖。需要明确的是,这两种类型仍然相互依赖,但在发出请求之前不会加载依赖项,此时类型已由 graphql 加载。
一些(伪)代码来演示。
之前:
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 ...
}
}
}
}
})
之后:
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 ...
}
}
}
}
})
我收到以下错误:
Error: Can only create List of a GraphQLType but got: [object Object].
但是我传递的是 GraphQLType。
这是触发错误的文件。
```
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)
}
})
```
这是它抱怨的 BotType
```
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
```
我已尝试在我的本地主机上重现您的架构,但没有发现有关 GraphQLList 的错误。
但是,我收到错误(第一个文件)
Error: Type must be named.
因为我注意到您在第一个文件中键入了错误的 GraphQLObjectType 定义。在我看来,您试图定义一个字段,而不是类型。
module.exports = new GraphQL.GraphQLObjectType({
name: 'BotListType',
fields: () => ({
list: {
type: new GraphQL.GraphQLList(BotType),
resolve: function(object) {
return object.bots.map(Datastore.getBot)
}
}
})
});
我正在使用 GraphQL 版本 0.6.2
我有这个错误,它实际上是由循环依赖引起的。这在本期 https://github.com/graphql/graphql-js/issues/467
中有详细记录为了解决这个问题,我不得不在我的字段定义中移动我的 require 语句来打破循环依赖。需要明确的是,这两种类型仍然相互依赖,但在发出请求之前不会加载依赖项,此时类型已由 graphql 加载。
一些(伪)代码来演示。
之前:
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 ...
}
}
}
}
})
之后:
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 ...
}
}
}
}
})