簡體   English   中英

GraphQL交叉引用引發錯誤:字段類型必須為輸出類型,但得到:[對象對象]

[英]GraphQL Cross References throws error: field type must be Output Type but got: [object Object]

我目前正在努力使用GraphQL API,但不幸的是它無法正常工作,因為我總是收到錯誤消息:

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

我仍然閱讀一些文章和帖子,例如GraphQL但有明確的錯誤:Query.example字段類型必須為Output Type,但得到:[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;

(代表OP發布)。

通過將所需的ObjectType移至fields函數來解決此問題:

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

否則,ObjectType的圓度有問題。 當然,必須對ProfileType做同樣的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM