たれぱんのびぼーろく

わたしの備忘録、生物学とプログラミングが多いかも

Amplify/GraphQL@keyのcomposite sort keyが動く仕組み

"key2nd#key3rd"という属性を自動生成し、この属性に対して文字列比較を提供する

Composite sort keyとは

Given that DynamoDB limits you to query by at most two attributes at a time, the @key directive helps by streamlining the process of creating composite sort keys such that you can support querying by more than two attributes at a time.

仕組み

The primary @key with 3 fields performs a bit more magic than the 1 and 2 field variants.
The first field orderId will be the HASH key as expected, but the SORT key will be a new composite key named status#createdAt that is made of the status and createdAt fields on the @model.
 The @key directive creates the table structures and also generates resolvers that inject composite key values for you during queries and mutations.

実装

VTLリゾルバの自動生成.
key2nd や key3rd に対するクエリ(eq、gt/ge、between、beginsWithなど)を可能にしている.

ハマりどころ

文字列比較をしている

VTLで"S"の比較をしている.
keyを"#"でつないだものを比較に用いるので、文字列しか使えない.
桁をあわせる(ゼロパディング)すれば文字列で数字比較もできるが、クセが強い.

key3rdのみのクエリ不可

key2ndが必要.
内部実装としてconcatしたもののクエリしているだけなので、無はダメ

使いどころ

  1. 文字列の階層構造を持っている
  2. ゼロパディングができる数字属性