【发布时间】:2017-10-06 17:38:56
【问题描述】:
我正在探索 graphql,困扰我的问题是在定义我的 graphql 服务器时是否可以进行某种类型组合。
假设我的收藏中有 Person 和 Company。我有这样的事情:
const Person = new GraphQLObjectType({
name: 'Person',
fields: {
firstName: {type: GraphQLString},
lastName: {type: GraphQLString}
}
});
const Company = new GraphQLObjectType({
name: 'Company',
fields: {
name: {type: GraphQLString},
website: {type: GraphQLString}
}
});
但是 Company 和 Person 都应该有如下字段:createdAt 和 id。所以假设我有:
const Entity = new GraphQLObjectType({
name: 'Entity',
fields: {
id: {type: GraphQLString},
createdAt: {type: GraphQLString}
}
});
所以我在想这样的事情:
new GraphQLObjectType({
...
extends: [Entity]
});
我知道有interfaces,但我认为这不是我想要的,因为无论如何我都需要实现接口,而我想要实现的是单独保留一些字段定义并在其中重复使用它们其他类型。
有什么想法吗?我是在尝试做一些完全没有意义的事情吗?
【问题讨论】:
标签: design-patterns graphql graphql-js apollo