【问题标题】:GraphQL Cross References throws error: field type must be Output Type but got: [object Object]GraphQL 交叉引用抛出错误:字段类型必须是输出类型,但得到:[object Object]
【发布时间】:2017-03-07 03:36:27
【问题描述】:

我目前正在为我的 GraphQL API 苦苦挣扎,不幸的是它无法正常工作,因为我总是收到错误消息:

Error: Contact.other field type must be Output Type but got: [object Object].

我仍然阅读了一些文章和帖子,例如GraphQL with express error : Query.example field type must be Output Type but got: [object Object],但无论如何它都不起作用,因为答案并不能解决我的错误原因。我希望你能帮助我或者只是给我一个提示来解决这个问题。我在下面附上了我的代码的主要部分:

ProfileType.js:

const graphql = require('graphql');
const ContactType = require('./ContactType');
const ObjectType = graphql.GraphQLObjectType;
const List = graphql.GraphQLListType;
const ID = graphql.GraphQLID;
const NonNull = graphql.GraphQLNonNull;

const ProfileType = new ObjectType({
  name: 'Profile',
  fields: function () {
    return {
      id: {type: new NonNull(ID)},
      contacts: {type: new List(ContactType)},
    };
  },
});

module.exports = ProfileType;

ContactType.js:

const graphql = require('graphql');
const ProfileType = require('./ProfileType');
const ObjectType = graphql.GraphQLObjectType;
const EnumType = graphql.GraphQLEnumType;

const ContactType = new ObjectType({
  name: 'Contact',
  fields: function () {
    return {
      other: {
        type: ProfileType
      },
      status: {
        type: new EnumType({
          values: {
            REQUESTED: {value: 0},
            COMMITTED: {value: 1}
          },
          name: 'ContactStatus'
        })
      }
    };
  },
});

module.exports = ContactType;

【问题讨论】:

  • 你为什么用 ContactType 用 ProfileType 和 ContactType 用 ProfileType。这个循环引用不应该导致不一致的行为吗?
  • 我认为通常循环行为应该适用于 GraphQL。我想建模一个人与其他几个人有联系。这些人都应该有个人资料。最后我想得到所有例如一个人的联系人的所有 ID 和姓名。这些参数是 ProfileType 的一部分,因此不需要额外的 ObjectType(避免冗余)。还是 GraphQL 无法实现这样的循环?

标签: javascript graphql


【解决方案1】:

(代表 OP 发布)。

通过将所需的 ObjectType 移动到 fields 函数来解决它:

const ContactType = new ObjectType({
  name: 'Contact',
  fields: function () {
    const ProfileType = require('./ProfileType');
    // ...
  }
});

否则,ObjectType 的循环存在问题。当然,ProfileType 也必须这样做。

【讨论】:

    猜你喜欢
    • 2018-03-28
    • 2017-01-22
    • 2018-01-30
    • 2018-04-09
    • 2018-10-14
    • 2019-09-19
    • 2017-08-16
    • 2018-02-11
    • 2017-09-03
    相关资源
    最近更新 更多