【发布时间】:2017-06-16 16:01:04
【问题描述】:
我正在尝试为我的查询创建树状结构,以摆脱像
这样的查询peopleList, peopleSingle, peopleEdit, peopleAdd, peopleDelete companyList, companySingle, companyEdit, companyAdd, companyDelete etc.
最后我想发送这样的查询:
query test {
people {
list {
id
name
}
single(id: 123) {
id
name
}
}
company {
list {
id
name
}
single(id: 456) {
id
name
}
}
}
mutation test2 {
people {
create(data: $var) {
id
name
}
}
people {
edit(id: 123, data: $var) {
id
name
}
}
}
这是我在人员模块上的查询对象的一部分:
people: {
type: //What type this should be?
name: 'Root of People queries',
fields: () => ({
list: {
type: peopleType,
description: 'Returns all people in DB.',
resolve: () => {
// resolve method implementation
}
},
single: {
type: peopleType,
description: 'Single row from people table. Requires ID argument.',
args: {
id: { type: new GraphQLNonNull(GraphQLID) }
},
resolve: () => {
// resolve method implementation
}
}
})
}
我曾尝试将此 sn-p 放入 GraphQLObjectType,然后在 RootQuery 中将它们组合在一起(再次使用 GraphQLObjectType) - 没有用。
另一种方法可以是创建新类型——比如 peopleQueriesType,在这个类型中将我的所有查询指定为字段,然后为这个对象创建单个查询。但这对我来说似乎很奇怪 - 用不必要的对象污染我的代码只是为了将我的查询合并为树状形状。
我尝试查看 Apollo 服务器实现,是否可以执行这种查询结构,但在文档中找不到任何帮助。
我在我的服务器上使用 node.js + express + graphql-js。
【问题讨论】:
标签: javascript schema graphql graphql-js