【问题标题】:GraphQL Error - Error: Can only create List of a GraphQLType but got: [object Object]GraphQL 错误 - 错误:只能创建 GraphQLType 的列表,但得到:[object Object]
【发布时间】:2016-12-28 22:08:47
【问题描述】:

我收到以下错误:

错误:只能创建 GraphQLType 的列表,但得到:[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

```

【问题讨论】:

    标签: graphql relay


    【解决方案1】:

    我尝试在我的本地主机上重现您的架构,但没有收到关于 GraphQLList 的错误。

    但是,我收到错误(在第一个文件上)

    错误:必须命名类型。

    因为我注意到您在第一个文件中输入了错误的 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

    【讨论】:

    • 经过一番调查,似乎require 正在拉入一个空对象。
    • 尝试用函数替换你的 field 对象。而不是fields: { ... } 像这样定义fields: () => ({ ... })
    【解决方案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 ...
            }
          }
        }
      }
    })
    

    【讨论】:

      猜你喜欢
      • 2017-09-06
      • 2018-01-05
      • 1970-01-01
      • 2018-11-27
      • 2020-12-20
      • 1970-01-01
      • 2020-07-14
      • 2018-11-09
      • 1970-01-01
      相关资源
      最近更新 更多