LoginSignup
2
1

More than 1 year has passed since last update.

Amplifyで時系列順に投稿を取得しようとして詰まりました

Last updated at Posted at 2021-05-05

はじめに

ReactでAmplifyとGraphQLを用いてDynamoDBにあるデータを時系列順に取得しようとして詰まったので、その解決方法を残しておきます。

※ソートの方法は下記の記事を参考にしました。が私のミスでしっかりと実装できていなかったです。
それにより時系列順になるように実装しましたがデータが返ってこない事象が発生しました。
https://qiita.com/Genmaru/items/cde7f8a3accb2585e391

その際の原因と解決方法を残しておきたいと思います。

原因と解決策

GraphQLのスキーマ定義時に当初は以下のようにしてました。

type Post @model @searchable {
  id: ID!
  postBody: String!
  status: String
  createdAt: String!
  comments: [Comment] @connection(name: "PostComments") #relationship
  likes: [Like] @connection(name: "PostLikes")
}

このスキーマだと時系列順に取得できませんでした。
何が悪かったのか?それは『createdAt: String!』でした。
正しくは以下のように『 createdAt: AWSDateTime!』とするべきでした。

type Post @model @searchable {
  id: ID!
  postBody: String!
  status: String
  createdAt: AWSDateTime!
  comments: [Comment] @connection(name: "PostComments") #relationship
  likes: [Like] @connection(name: "PostLikes")
}

最後に

もし同じようなミスをしている人の助けになれば幸いです。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1