gpt4 book ai didi

javascript - GraphQL 交叉引用引发错误 : field type must be Output Type but got: [object Object]

转载 作者:行者123 更新时间:2023-12-03 05:48:44 25 4
gpt4 key购买 nike

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

最佳答案

(代表 OP 发布)。

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

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

否则,ObjectType 会出现循环问题。当然,对于 ProfileType 也必须做同样的事情。

关于javascript - GraphQL 交叉引用引发错误 : field type must be Output Type but got: [object Object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40233996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com