我是 Javascript 新手,不知道如何解决这个问题。我正在创建一个 GraphQL 服务来提供对数据库的查询,我想定义三种类型:个人、公司和关系
type Relation: {
person: Person
company: Company
}
type Person: {
relationship: Relation
}
type Company: {
relationship: Relation
}
正如你所看到的,它们是相互依赖的,我将得到 Person 和 Companyundefined
,有办法解决这个问题吗?
数据库架构:
// -------------------- Object(company) --------------------
objectType = new GraphQLObjectType ({
name: 'Object',
description: 'Object could be a Company, FinancialOrg, Person or Product.',
fields: {
id: {
type: new GraphQLNonNull(GraphQLString),
},
entity_type: {
type: new GraphQLNonNull(GraphQLString),
description: 'could be Company, FinancialOrg, Person or Product.'
},
entity_id: {
type: new GraphQLNonNull(GraphQLInt),
},
parent_id: {
type: GraphQLString,
},
name: {
type: new GraphQLNonNull(GraphQLString),
},
normalized_name: {
type: new GraphQLNonNull(GraphQLString),
},
permalink: {
type: new GraphQLNonNull(GraphQLString),
}
},
resolveType: function(object) {
return dbHandler.findObjectById(object.id);
}
})
// -------------------- Relationship --------------------
var relationshipType = new GraphQLObjectType({
name: 'Relationship',
description: 'Describe the relationship between a person and a object(Corporation, fund)',
fields: {
id: {
type: new GraphQLNonNull(GraphQLInt),
},
relationship_id: {
type: new GraphQLNonNull(GraphQLInt),
},
person_object_id: {
type: new GraphQLNonNull(GraphQLString),
},
relationship_object_id: {
type: new GraphQLNonNull(GraphQLString),
description: 'A Corporation or a Fund',
},
}
});
// -------------------- Person Type --------------------
personType = new GraphQLObjectType ({
name: 'Person',
fields: {
id: {
type: new GraphQLNonNull(GraphQLInt),
},
object_id: {
type: new GraphQLNonNull(GraphQLString),
},
first_name: {
type: new GraphQLNonNull(GraphQLString),
},
last_name: {
type: new GraphQLNonNull(GraphQLString),
},
birthplace: {
type: GraphQLString
},
affiliation_name: {
type: GraphQLString
},
},
});