【发布时间】: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