Fast JSON APIで記事一覧APIと記事詳細APIを作成

①gemをインストール。
gem 'fast_jsonapi'


②serializerファイルを作成。
$ rails g serializer Article title contents status
$ rails g serializer User name email

app配下のserializersディレクトリにファイルが作られる。

③serializersファイルを設定。
class ArticleSerializer
  include FastJsonapi::ObjectSerializer
  # 出力したいカラムを指定
  attributes :title, :contents, :status
  belongs_to :user
end
class UserSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :email
  has_many :articles
end


④コントローラーを記載。
# controllers/api/v1/articles_controller.rb

module Api
  module V1
    class ArticlesController < BaseController
      before_action :set_article, only: :show

      def index
        articles = Article.all
        json_string = ArticleSerializer.new(articles).serialized_json
        render json: json_string
      end

      def show
        options = { include: [:user, :'user.name', :'user.email'] }
        json_string = ArticleSerializer.new(@article, options).serialized_json
        render json: json_string
      end

      private

      def set_article
        @article = Article.find(params[:id])
      end
    end
  end
end


出力結果

記事一覧localhost:3000/api/v1/articles

{
    "data": [
        {
            "id": "1",
            "type": "article",
            "attributes": {
                "title": "MyString1",
                "contents": "MyText1",
                "status": "draft"
            },
            "relationships": {
                "user": {
                    "data": {
                        "id": "1",
                        "type": "user"
                    }
                }
            }
        },
        {
            "id": "2",
            "type": "article",
            "attributes": {
                "title": "MyString2",
                "contents": "MyText2",
                "status": "draft"
            },
            "relationships": {
                "user": {
                    "data": {
                        "id": "1",
                        "type": "user"
                    }
                }
            }
        }
    ]
}


記事詳細localhost:3000/api/v1/articles/1

{
    "data": {
        "id": "1",
        "type": "article",
        "attributes": {
            "title": "MyString1",
            "contents": "MyText1",
            "status": "draft"
        },
        "relationships": {
            "user": {
                "data": {
                    "id": "1",
                    "type": "user"
                }
            }
        }
    },
    "included": [
        {
            "id": "1",
            "type": "user",
            "attributes": {
                "name": "MyString1",
                "email": "MyString1"
            },
            "relationships": {
                "articles": {
                    "data": [
                        {
                            "id": "1",
                            "type": "article"
                        }
                    ]
                }
            }
        }
    ]
}



参考記事:
NetflixのFast JSON APIを使ってみた - クラシル開発ブログ
Active Model Serializer をざっくり使ってみた - Qiita
Railsのactive_model_serializerについて学ぶ_100DaysOfCodeチャレンジ10日目(Day_10:#100DaysOfCode) - Qiita