npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

appsync-on-rails

v0.2.2

Published

## 今できること、使い方

Downloads

16

Readme

appsync-on-rails

今できること、使い方

前提

  • serverless appsync plugin および serverless framework の上で動くようになっているため、以下のような要領でアプリケーションを実装する必要がある。
  • ゆくゆくは rails new のようなコマンドでテンプレートから生やせるようにはしたい
.
├── package.json
├── serverless.yml
├── resources
│ ├── appsync
│ └── dynamodb
├── mapping-templates
└── schema.graphql

インストール

yarn add appsync-on-rails -D

schema.graphql から周辺リソースを生やす

yarn appsync-on-rails

すると、以下のディレクトリに設定ファイルを出力する。

  • resources/appsync 以下に、 *.mapping.yml (appsync に食わせる Type/field とデータソースのマッピング設定ファイル)
  • resources/dynamodb 以下に、 *.resource.yml (dynamodb のリソース定義ファイル)
  • schema 以下に、*.graphql (各タイプ固有の、CRUD オペレーションを表現する graphql スキーマ)
  • mapping-templates 以下に、*.*.request/response.vtl(appsync のリゾルバ定義)

あくまでも、「新しい設定ファイルを出力する」にすぎないため、
serverless.yml 側では適宜それらを読み込むよう設定が必要。

コマンドライン引数

  • appsync-on-rails --help で参照可能。
  • --append-only (既存ファイルを上書きしなくなる) や ---types (指定の graphql Type のみに処理を実行する) などは便利だと思う

利用可能な directive

directive @model on OBJECT # タイプ名と同じ名前のdynamodbテーブルを作る。
directive @connection on FIELD_DEFINITION # フィールドを、その型のタイプ名と同じdynamodbテーブルと紐づける。
directive @function on FIELD_DEFINITION # フィールドを、lambda関数と紐づける

model

type Customer @model {
  id: ID!
  name: String
}
  • @model をつけると、dynamodb のリソースおよび CRUD オペレーションのスキーマ定義、また CRUD オペレーションのリゾルバの実装が生えてくる。

connection

  • @connection をつけると、状況に応じて dynamodb の GSI のリソースおよび コネクションを表現する リゾルバの実装が生えてくる。
has_one (お互いに)
type Customer @model {
  defaultCard: Card @connection(name: "CustomerDefaultCard", myKey: "cardId")
}

type Card @model {
  owner: Customer @connection(name: "CustomerDefaultCard", myKey: "ownerId")
}

この場合、以下のように結合する。

  • Customer => Card
    Card テーブルの id キー(プライマリキー)に、 Customer テーブルの cardId フィールドの値を渡して GetItem する
  • Card => Customer Customer テーブルの id キー(プライマリキー)に、 Card テーブルの ownerId フィールドの値を渡して GetItem する
has_one(同じ ID)
type Customer @model {
  user: User @connection(name: "UserCustomer")
}

type User @model {
  customer: Customer @connection(name: "UserCustomer" )
}
  • @connection ディレクティブにキー名を何も渡さなかった場合、相互に ID が同じという前提で参照する。
has_many / belongs_to
type Customer @model {
  cards: [Card] @connection(name: "CustomerCard", yourKey: "ownerId")
}

type Card @model {
  owner: Customer @connection(name: "CustomerCard", myKey: "ownerId")
}
  • @connection ディレクティブで、myKey フィールドを指定した場合、自動で name フィールドに渡した名称の GSI が dynamodb に張られる。

  • Customer => Card
    Card テーブルの CustomerCard という GSI の ownerId キーに Customer テーブルの ID フィールドを渡し、Query でリストを取得する

  • Card => Customer Customer テーブルの ID キーに Card テーブルの ownerId フィールドを渡し、GetItem で取得する

function

# schema.graphql
type Mutation {
  fix(id: ID): Result @function(name: "fix")
}
# serverless.yml
functions:
  fix:
    handler: handler.fix
  • name フィールドに渡した名前と、serverless.yml の functions エントリ内のキーが同じ関数を紐づける

問題意識

serverless 界の Rails を作りたい

  • Amplify API が体験としてかなり近い
  • ただ、Amplify は Rails と違って本格的な web アプリをつくるのには不向き

フレームワークはどうあるべきか

  • スタンドアロン であること
    それ自体が依存する先は極力少なくあるべきで、ソースコードを読んだ他の開発者が想像できる範囲でのこと以外をしてはならない。

  • 面倒な作業をなくしてくれること
    この点においては Amplify は既にかなりの成果を上げているといってよい。
    逆にこれができていないとフレームワークとは呼べないそもそもの部分になる

  • いつでも剥がせること、開発者はその気になればすべてをコントロール下におけること
    Amplify やその他の FW/ツールが圧倒的に弱い部分。
    本 FW は、この部分を重視して開発する

周辺の状況(2020 年現在)

| | 楽 | 大変 | | :--------- | :-----------: | :-----------: | | 拡張性あり | ☆gql-on-rails | serverless FW | | 拡張性なし | Amplify | 手作業 |

いつでも剥がせる状態を実現するためには

  • ランタイムに干渉しない
  • プロジェクトルート外のものを管理しない