【发布时间】:2016-08-24 10:43:42
【问题描述】:
我是 Javascript 新手,不知道如何解决这个问题。我正在创建一个 GraphQL 服务来提供对数据库的查询,我想定义三种类型:人员、公司和关系
type Relation: {
person: Person
company: Company
}
type Person: {
relationship: Relation
}
type Company: {
relationship: Relation
}
如您所见,它们是相互依赖的,我将得到个人和公司undefined,有没有办法解决这个问题?
数据库架构:
// -------------------- 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
},
},
});
【问题讨论】:
-
可能以不同的方式为您的类型建模。您能否提供更多有关您的数据库架构的详细信息?
-
谢谢@AhmadFerdousBinAlam 我有点想知道如何解决这个问题,我实际上将所有三种类型的关系、人员和公司都“取消链接”,但创建了另一个层,其中包含三个更复杂的版本关系”。
-
我在自己的项目中遇到过这个问题。答案是为 GraphQL 使用
Interfaces。 graphql.org/docs/api-reference-type-system/…
标签: javascript circular-dependency graphql