0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SOULsでAPIを立ててみる

Last updated at Posted at 2021-12-09

SOULsとは

Active RecordとScaffoldやCI/CDが標準装備された、Rubyのサーバーレスフレームワークです。
GCP (Google Cloud Platform) 上で動作します。
オランダ政府より先端研究開発として認定されており、日本人の方が開発されているためドキュメントも丁寧です。

詳しくは別で記事を書いたのでそちらをご覧ください。

いざ、触ってみる

SOULsはGraphQL APIとWorkerが作れるので、GraphQL APIを作ってみます。

必要なパッケージ

ここのインストールの説明は省略します。

  • Ruby v3
  • Docker
  • Google Cloud SDK
  • PostgreSQL
  • Redis
  • Github CLI

準備

Github CLI AuthのログインとSOULs のインストールをします。

$ gh auth login

$ gem install souls

$ souls -v #インストール成功の確認

アプリケーションの作成

Railsでいうrails newのコマンドでプロジェクトを作成できます。

$ souls new souls-app

次にSOULs コマンドを利用して、PostgreSQL13 の Docker コンテナを起動します。

$ cd apps/api

$ souls docker psql

DBの準備ができたのでデータベースの作成及び Migration を行います。

$ souls db create
$ souls db migrate

ここまでできてるか動作確認です。
souls s コマンドで立ち上げます。

$ souls s

localhost:4000にアクセスして、以下の画面が表示されたらアプリを作る準備は完了です。

動作確認

モデルの作成

souls db create_migrationでマイグレーションファイルを作成します。

$ souls db create_migration user
db/migrate/20210930154336_create_users.rb
Created file! : sig/api/db/migrate/create_users.rbs
Created file! : ./app/models/user.rb
Created file! : ./spec/models/user_spec.rb
Created file! : sig/api/app/models/user_model.rbs

作成されたファイルを元にmigrationします。

$ souls db migrate

Scaffoldの実行

souls g scaffold_allコマンドは db/schema.rb のタイプをもとに CRUD に必要なファイルと、それに対応するテストを自動生成してくれます。

$ souls g scaffold_all
.
.
Created file! : ./app/graphql/types/edges/user_edge.rb
Created file! : ./sig/api/app/graphql/types/edges/user_edge.rbs
Created file! : ./app/graphql/types/connections/user_connection.rb
Created file! : ./sig/api/app/graphql/types/connections/user_connection.rbs
Created file! : ./app/graphql/resolvers/user_search.rb
Created file! : ./sig/api/app/graphql/resolvers/user_search.rbs
Created file! : ./spec/factories/users.rb
Created file! : ./spec/mutations/base/user_spec.rb
Created file! : ./spec/queries/user_spec.rb
Created file! : ./spec/resolvers/user_search_spec.rb
Created file! : ./spec/policies/user_policy_spec.rb
🎉  Generated SOULs CRUD Files

これで完成です。

動かしてみる

登録

クエリ

mutation {
  createUser(
    input: {
      uid: "uniq-id"
      username: "SOULs"
      email: "info@test.com"
      }
  ) {
    userEdge {
      node {
        uid
        id
        username
        email
      }
    }
  }
}

レスポンス

{
  "data": {
    "createUser": {
      "userEdge": {
        "node": {
          "uid": "uniq-id",
          "id": "VXNlcjo0",
          "username": "SOULs",
          "email": "info@test.com"
        }
      }
    }
  }
}

表示

クエリ

query {
  userSearch(filter: { isDeleted: false }) {
    edges {
      node {
        id
        uid
        username
        email
      }
    }
    nodes {
      id
    }
    pageInfo {
      hasNextPage
    }
  }
}

レスポンス

{
  "data": {
    "userSearch": {
      "edges": [
        {
          "node": {
            "id": "VXNlcjo0",
            "uid": "uniq-id",
            "username": "SOULs",
            "email": "info@test.com"
          }
        }
      ],
      "nodes": [
        {
          "id": "VXNlcjo0"
        }
      ],
      "pageInfo": {
        "hasNextPage": false
      }
    }
  }
}

更新

クエリ

mutation {
  updateUser(input: {
    id: "VXNlcjo0"
    username: "SOULs API"
    }) {
    userEdge {
      node {
        id
        username
        email
      }
    }
  }
}

レスポンス

{
  "data": {
    "updateUser": {
      "userEdge": {
        "node": {
          "id": "VXNlcjo0",
          "username": "SOULs API",
          "email": "info@test.com"
        }
      }
    }
  }
}

物理削除(論理削除もあります)

クエリ

mutation {
  destroyDeleteUser(input: { id: "VXNlcjo0" }) {
    user {
      id
      username
      email
    }
  }
}

レスポンス

{
  "data": {
    "destroyDeleteUser": {
      "user": {
        "id": "VXNlcjo0",
        "username": "SOULs API",
        "email": "info@test.com"
      }
    }
  }
}

最後に

簡単にGraphQL APIが作れました。
今回はテストは触っていませんが、ほぼ自動生成されているのでFactoryBot用意するだけでテストが用意できます。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?