hen*_*enk 5 javascript graphql graphql-js apollo-server
我正在使用带有mysql和sequelize的meteor的react-apollo,我仍然是JS的初学者.假设我的apollo-server上有以下解析器函数:
export default resolvers = {
Query: {
posts(_, args){
return Post.findAndCountAll({ where: args });
},
numberOfPosts(){
return /// the number of selected posts
}
}
Run Code Online (Sandbox Code Playgroud)
我想从数据库中选择满足某些条件的一些数据,然后计算所选行的数量并在"numberOfPosts"字段中返回它们.
findAndCountAll()
返回一个对象,其中包含选定的行和计数.我想让我post()
只返回选定的行,而我的numberOfPosts()只返回所选帖子的计数.现在,两者都是由posts()返回的.
我的架构是:
type Post {
id: Int
date: Float
text: String
}
type NumberOfPosts{
total: Int
filtered: Int
}
type Query {
posts(
id: Ind,
offset: Int,
limit: Int,
filter: String): [Post]
numberOfPosts:[NumberOfPosts]
}
schema {
query: Query
}
Run Code Online (Sandbox Code Playgroud)
目标是以下列格式接收数据:
{
"data": {
"numberOfPosts": [
{
"total": 1000,
"filtered": 21
}
],
"posts": [
{
"id": 4,
"date": 5105626122,
"text": "jzybiwutudi"
},
...
]
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我的工作:尝试1:
let selectedCount;
export default resolvers = {
Query: {
posts(_, args){
return Post.findAndCountAll({where: args}).then(
function (results) {
selectedCount = results.count;
return results.rows
});
},
numberOfPosts(){
return selectedCount
}
}}
Run Code Online (Sandbox Code Playgroud)
所以我在解析器之外定义一个帮助变量,并将其设置为所选行的数量,然后返回计数numberOfPosts()
,这有效,但问题是,return results.rows
导致错误,我不明白为什么.
另一个问题是,这selectedCount
始终是先前的行数
试试2
另一个似乎有用的解决方案是将参数传递两次到GraphQL查询中,如下所示:
{
numberOfPosts(filter: "example") {
total
filtered
}
posts(filter: "example") {
id
date
text
}
}
Run Code Online (Sandbox Code Playgroud)
然后两个解析器函数都知道相同的参数,所以我可以选择和计算相同的帖子.但这看起来不对我,因为我必须两次传递相同的args,它们也会被传输两次......
您应该更多地考虑设计以及每个查询应该做些什么.这些查询不应该改变数据库或全局状态.
您可以做的最好的事情就是简单地定义一个新类型,包括total
和filtered
您NumberOfPosts
在第一次尝试时所做的一样,以及帖子列表.
所以,你的架构将是这样的:
type Post {
id: Int
date: Float
text: String
}
type PostList {
total: Int
filtered: Int
posts: [Post]
}
type Query {
posts(
id: Ind,
offset: Int,
limit: Int,
filter: String): PostList
}
schema {
query: Query
}
Run Code Online (Sandbox Code Playgroud)
你解决posts
就像:
posts(_, args) {
return Post.findAndCountAll({ where: args }).then(result => {
return {
total: 1000,
filtered: result.count,
posts: result.rows
}
})
}
Run Code Online (Sandbox Code Playgroud)
请注意我如何将总数设为1000.您无法获得总行数findAndCountAll
.如果你这样做,需要你可以并行运行两个不同的查询并Promise.all
用来等待它们被解决.
posts(_, args) {
return Promise.all([
Post.count(),
Post.findAndCountAll({ where: args })
]).then(data => {
return {
total: data[0],
filtered: data[1].count,
posts: data[1].rows
}
})
}
Run Code Online (Sandbox Code Playgroud)
上述代码也可以从ES6的解构中受益:
posts(_, args) {
return Promise.all([
Post.count(),
Post.findAndCountAll({ where: args })
]).then(([totalCount, filteredData]) => {
return {
total: totalCount,
filtered: filteredData.count,
posts: filteredData.rows
}
})
}
Run Code Online (Sandbox Code Playgroud)
现在你可以运行:
query {
posts(filter:"example") {
total
filtered
posts {
id
date
text
}
}
}
Run Code Online (Sandbox Code Playgroud)
得到:
{
"data": {
"posts": {
"total": 1000,
"filtered": 21,
"posts": [
{
"id": 4,
"date": 5105626122,
"text": "jzybiwutudi"
},
...
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3571 次 |
最近记录: |