admin管理员组文章数量:1332865
I am using AWS Amplify GraphQL API to build my service. Assuming a model like this:
type ChatMessage @model {
userId: ID! @primaryKey(sortKeyFields: ["createdAt"])
createdAt: String!
status: String!
}
userId
and createdAt
are already in used, but status
can be changed.
I need to query items of a user with status == 'active'
, while items should have been sorted by createdAt
. And I need to paginate the query to ease the load.
I have few thoughts, but none of these seem perfect enough.
- Use filter in GraphQL Query
The good thing is I don't have to alter the model. The bad thing is the filter is applied AFTER items queried. I need to paginate the result with limit 10, and this method cannot guarantee that I get 10 items every time.
- Add a composite field and make it GSI
type ChatMessage @model {
userId: ID! @primaryKey(sortKeyFields: ["createdAt"])
createdAt: String!
userIdStatus: String! @index(sortKeyFields: ["createdAt"])
}
The format of userIdStatus
would be {userId}#{status}
.
I can do exactly what I want. But the composite field is not very intuitive, and it requires additional codes to maintain the field.
I am wondering is there a better solution?
I am using AWS Amplify GraphQL API to build my service. Assuming a model like this:
type ChatMessage @model {
userId: ID! @primaryKey(sortKeyFields: ["createdAt"])
createdAt: String!
status: String!
}
userId
and createdAt
are already in used, but status
can be changed.
I need to query items of a user with status == 'active'
, while items should have been sorted by createdAt
. And I need to paginate the query to ease the load.
I have few thoughts, but none of these seem perfect enough.
- Use filter in GraphQL Query
The good thing is I don't have to alter the model. The bad thing is the filter is applied AFTER items queried. I need to paginate the result with limit 10, and this method cannot guarantee that I get 10 items every time.
- Add a composite field and make it GSI
type ChatMessage @model {
userId: ID! @primaryKey(sortKeyFields: ["createdAt"])
createdAt: String!
userIdStatus: String! @index(sortKeyFields: ["createdAt"])
}
The format of userIdStatus
would be {userId}#{status}
.
I can do exactly what I want. But the composite field is not very intuitive, and it requires additional codes to maintain the field.
I am wondering is there a better solution?
Share edited Nov 22, 2024 at 7:02 misgood asked Nov 22, 2024 at 6:53 misgoodmisgood 213 bronze badges1 Answer
Reset to default 0Those are the best options. If you request it often you should use an index for efficiency. If it's more ad-hoc you can use a filter expression.
本文标签:
版权声明:本文标题:graphql - How to query a field in DynamoDB that is neither a partition key nor a sort key, while specifying the partition key an 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742293126a2448187.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论