首页 文章

GraphQL交叉引用抛出错误:字段类型必须是输出类型但得到:[object Object]

提问于
浏览
0

我目前正在使用我的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;

1 回答

  • 1

    (代表OP发布) .

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

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

    否则,ObjectType存在循环性问题 . 当然,必须使用ProfileType完成相同的操作 .

相关问题