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.

  1. 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.

  1. 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.

  1. 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.

  1. 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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

Those 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.

本文标签: